Download topic_modeling.py from CGIAR/hierarchical-text-classification: direct link, hf CLI and curl.
- Browser
- Download file 445 Bytes
-
https://huggingface.co/spaces/CGIAR/hierarchical-text-classification/resolve/main/topic_modeling.py
- Command line
-
hf download hf://spaces/CGIAR/hierarchical-text-classification/topic_modeling.py
-
curl -L -o topic_modeling.py https://huggingface.co/spaces/CGIAR/hierarchical-text-classification/resolve/main/topic_modeling.py
445 Bytes
| import pandas as pd | |
| from turftopic import KeyNMF | |
| def get_topics(sample, text_col, k:int=None): | |
| sample_queries = sample[text_col].tolist() | |
| # Initialize KeyNMF with the desired number of topics | |
| model = KeyNMF(n_components='auto') if k is None else KeyNMF(n_components=k) | |
| out = model.fit_transform(sample_queries) | |
| return pd.concat([sample.reset_index(drop=True), pd.DataFrame(out).reset_index(drop=True)], axis=1), model | |