Run the topic modeling.
(self, data, gnn_outputs: Tuple[np.ndarray, np.ndarray], key="query")
| 69 | return hierarchical_topics, tree |
| 70 | |
| 71 | def run(self, data, gnn_outputs: Tuple[np.ndarray, np.ndarray], key="query") -> dict: |
| 72 | """ |
| 73 | Run the topic modeling. |
| 74 | """ |
| 75 | # Prepare the data |
| 76 | if self.config["auto_cluster"]: |
| 77 | if self.config["representation_model"].split("(")[0] == "pipeline": |
| 78 | generator = self.config["representation_model"] |
| 79 | representation_model = [MaximalMarginalRelevance(diversity=0.3), TextGeneration(eval(generator))] |
| 80 | elif self.config["representation_model"].split("(")[0] == "OpenAI": |
| 81 | representation_model = [ |
| 82 | MaximalMarginalRelevance(diversity=0.3), |
| 83 | eval(self.config["representation_model"]), |
| 84 | ] |
| 85 | else: |
| 86 | lg.info("Using MaximalMarginalRelevance as representation model") |
| 87 | representation_model = MaximalMarginalRelevance(diversity=0.3) |
| 88 | |
| 89 | topic_model = BERTopic( |
| 90 | nr_topics="auto", |
| 91 | vectorizer_model=eval(self.config["vectorizer_model"]), |
| 92 | embedding_model=self.config["embedding_model"], |
| 93 | representation_model=representation_model, |
| 94 | n_gram_range=(1, 2), |
| 95 | min_topic_size=10, |
| 96 | top_n_words=10, |
| 97 | calculate_probabilities=False, |
| 98 | ) |
| 99 | else: |
| 100 | # Fit the BERTopic model |
| 101 | topic_model = BERTopic( |
| 102 | nr_topics="auto", |
| 103 | vectorizer_model=eval(self.config["vectorizer_model"]), |
| 104 | umap_model=eval(self.config["dimensionality_reduction"]), |
| 105 | hdbscan_model=eval(self.config["clustering_model"]), |
| 106 | embedding_model=eval(self.config["embedding_model"]), |
| 107 | representation_model=eval(self.config["representation_model"]), |
| 108 | n_gram_range=(1, 2), |
| 109 | min_topic_size=10, |
| 110 | top_n_words=10, |
| 111 | calculate_probabilities=False, |
| 112 | ) |
| 113 | topics, probabilities = topic_model.fit_transform(data[key].to_list(), gnn_outputs[1]) |
| 114 | freq = self.get_topic_info(topic_model) |
| 115 | rep_docs = self.get_representative_docs(topic_model) |
| 116 | hr, tree = self.compute_hierarchical_topic_tree(topic_model=topic_model, data=data) |
| 117 | # add dict with topic info |
| 118 | docs = [] |
| 119 | for k, v in rep_docs.items(): |
| 120 | docs.append((k, v)) |
| 121 | rep = pd.DataFrame(docs, columns=["Topic", "Rep_docs"]) |
| 122 | topic_info_freq = pd.merge(freq, rep, on="Topic") |
| 123 | data["topic"] = topics |
| 124 | data["prob"] = probabilities |
| 125 | return {"data": data, "topic_info": topic_info_freq, "tree": tree} |
nothing calls this directly
no test coverage detected