Spaces:
Sleeping
Sleeping
Download topic_model.py from maman056/Customer_Review: direct link, hf CLI and curl.
- Browser
- Download file 595 Bytes
-
https://huggingface.co/spaces/maman056/Customer_Review/resolve/main/topic_model.py
- Command line
-
hf download hf://spaces/maman056/Customer_Review/topic_model.py
-
curl -L -o topic_model.py https://huggingface.co/spaces/maman056/Customer_Review/resolve/main/topic_model.py
595 Bytes
| def get_review_topic(review_text, vectorizer, nmf_model, top_n_words=5): | |
| # Transform the review into TF-IDF space | |
| review_vect = vectorizer.transform([review_text]) | |
| # Get topic distribution | |
| topic_probs = nmf_model.transform(review_vect) # shape (1, num_topics) | |
| topic_index = topic_probs.argmax() # pick topic with highest score | |
| # Get top words for that topic | |
| words = vectorizer.get_feature_names_out() | |
| topic_words = [words[j] for j in nmf_model.components_[topic_index].argsort()[-top_n_words:][::-1]] | |
| return ", ".join(topic_words) |