How to get all docoments per topic in bertopic modeling

Viewed 1731

I have a dataset and trying to convert it to topics using berTopic modeling but the problem is, i cant get all the docoments of a topic. berTopic is only return 3 docoments per topic.

topic_model  = BERTopic(verbose=True, embedding_model=embedding_model,
                                nr_topics = 'auto',
                                n_gram_range = (3,3),
                                top_n_words = 10,
                               calculate_probabilities=True, 
                              seed_topic_list = topic_list,
                              )
topics, probs = topic_model.fit_transform(docs_test)
representative_doc = topic_model.get_representative_docs(topic#1)
representative_doc

this topic contain more then 300 documents but bertopic only shows 3 of them with .get_representative_docs

1 Answers

There are probably solutions that are more elegant because I am not an expert, but I can share what worked for me (as there are no answers yet):

"topics, probs = topic_model.fit_transform(docs_test)" returns the topics.

Therefore, you can combine this output and the documents. For example, combine them into a (pandas.)dataframe using

df = pd.DataFrame({'topic': topics, 'document': docs_test})

Now you can filter this dataframe for each topic to identify the referring documents.

topic_0 = df[df.topic == 0]
Related