Spaces:
Sleeping
Sleeping
Download src/topic_modeling.py from bano1/LitReviewAI: direct link, hf CLI and curl.
- Browser
- Download file 1.01 kB
-
https://huggingface.co/spaces/bano1/LitReviewAI/resolve/main/src/topic_modeling.py
- Command line
-
hf download hf://spaces/bano1/LitReviewAI/src/topic_modeling.py
-
curl -L -o topic_modeling.py https://huggingface.co/spaces/bano1/LitReviewAI/resolve/main/src/topic_modeling.py
1.01 kB
| import pandas as pd | |
| from gensim import corpora, models | |
| from gensim.utils import simple_preprocess | |
| import nltk | |
| from nltk.corpus import stopwords | |
| nltk.download("stopwords") | |
| stop_words = set(stopwords.words("english")) | |
| def lda_topic_modeling(papers, num_topics=3): | |
| # Preprocess abstracts with stopword removal | |
| texts = [ | |
| [word for word in simple_preprocess(p["abstract"]) if word not in stop_words] | |
| for p in papers if p.get("abstract") | |
| ] | |
| # Guard: if no texts or empty docs | |
| if not texts or all(len(t) == 0 for t in texts): | |
| return pd.DataFrame([]) | |
| dictionary = corpora.Dictionary(texts) | |
| corpus = [dictionary.doc2bow(text) for text in texts] | |
| lda_model = models.LdaModel(corpus, num_topics=num_topics, id2word=dictionary, passes=10) | |
| topics = lda_model.print_topics(num_words=6) | |
| topic_data = [] | |
| for topic_id, words in topics: | |
| topic_data.append({"Topic ID": topic_id, "Keywords": words}) | |
| df = pd.DataFrame(topic_data) | |
| return df | |