Spaces:
Sleeping
Sleeping
| 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) |