Set custom topic labels in your fitted BERTopic model. Arguments: topic_labels: If a list of topic labels, it should contain the same number of labels as there are topics. This must be ordered from the topic with the lowest ID
(self, topic_labels: Union[List[str], Mapping[int, str]])
| 1977 | return get_tree(start, tree) |
| 1978 | |
| 1979 | def set_topic_labels(self, topic_labels: Union[List[str], Mapping[int, str]]) -> None: |
| 1980 | """Set custom topic labels in your fitted BERTopic model. |
| 1981 | |
| 1982 | Arguments: |
| 1983 | topic_labels: If a list of topic labels, it should contain the same number |
| 1984 | of labels as there are topics. This must be ordered |
| 1985 | from the topic with the lowest ID to the highest ID, |
| 1986 | including topic -1 if it exists. |
| 1987 | If a dictionary of `topic ID`: `topic_label`, it can have |
| 1988 | any number of topics as it will only map the topics found |
| 1989 | in the dictionary. |
| 1990 | |
| 1991 | Examples: |
| 1992 | First, we define our topic labels with `.generate_topic_labels` in which |
| 1993 | we can customize our topic labels: |
| 1994 | |
| 1995 | ```python |
| 1996 | topic_labels = topic_model.generate_topic_labels(nr_words=2, |
| 1997 | topic_prefix=True, |
| 1998 | word_length=10, |
| 1999 | separator=", ") |
| 2000 | ``` |
| 2001 | |
| 2002 | Then, we pass these `topic_labels` to our topic model which |
| 2003 | can be accessed at any time with `.custom_labels_`: |
| 2004 | |
| 2005 | ```python |
| 2006 | topic_model.set_topic_labels(topic_labels) |
| 2007 | topic_model.custom_labels_ |
| 2008 | ``` |
| 2009 | |
| 2010 | You might want to change only a few topic labels instead of all of them. |
| 2011 | To do so, you can pass a dictionary where the keys are the topic IDs and |
| 2012 | its keys the topic labels: |
| 2013 | |
| 2014 | ```python |
| 2015 | topic_model.set_topic_labels({0: "Space", 1: "Sports", 2: "Medicine"}) |
| 2016 | topic_model.custom_labels_ |
| 2017 | ``` |
| 2018 | """ |
| 2019 | unique_topics = sorted(set(self.topics_)) |
| 2020 | |
| 2021 | if isinstance(topic_labels, dict): |
| 2022 | if self.custom_labels_ is not None: |
| 2023 | original_labels = {topic: label for topic, label in zip(unique_topics, self.custom_labels_)} |
| 2024 | else: |
| 2025 | info = self.get_topic_info() |
| 2026 | original_labels = dict(zip(info.Topic, info.Name)) |
| 2027 | custom_labels = [ |
| 2028 | topic_labels.get(topic) if topic_labels.get(topic) else original_labels[topic] |
| 2029 | for topic in unique_topics |
| 2030 | ] |
| 2031 | |
| 2032 | elif isinstance(topic_labels, list): |
| 2033 | if len(topic_labels) == len(unique_topics): |
| 2034 | custom_labels = topic_labels |
| 2035 | else: |
| 2036 | raise ValueError( |