@@ -84,19 +84,7 @@ def _top_terms(
8484 terms .append (list (vocab [highest ]))
8585 return terms
8686
87- def name_topics (self , namer : TopicNamer ) -> list [str ]:
88- """Names topics with a topic namer in the model.
89-
90- Parameters
91- ----------
92- namer: TopicNamer
93- A Topic namer model to name topics with.
94-
95- Returns
96- -------
97- list[str]
98- List of topic names.
99- """
87+ def _rename_automatic (self , namer : TopicNamer ) -> list [str ]:
10088 self .topic_names_ = namer .name_topics (self ._top_terms ())
10189 return self .topic_names_
10290
@@ -366,22 +354,29 @@ def topic_names(self) -> list[str]:
366354 names .append (f"{ topic_id } _{ concat_words } " )
367355 return names
368356
369- def rename_topics (self , names : Union [list [str ], dict [int , str ]]) -> None :
370- """Rename topics in a model manually.
357+ def rename_topics (
358+ self , names : Union [list [str ], dict [int , str ], TopicNamer ]
359+ ) -> None :
360+ """Rename topics in a model manually or automatically, using a namer.
371361
372362 Examples:
373363 ```python
374364 model.rename_topics(["Automobiles", "Telephones"])
375365 # Or:
376366 model.rename_topics({-1: "Outliers", 2: "Christianity"})
367+ # Or:
368+ namer = OpenAITopicNamer()
369+ model.rename_topics(namer)
377370 ```
378371
379372 Parameters
380373 ----------
381374 names: list[str] or dict[int,str]
382375 Should be a list of topic names, or a mapping of topic IDs to names.
383376 """
384- if isinstance (names , dict ):
377+ if isinstance (names , TopicNamer ):
378+ self ._rename_automatic (names )
379+ elif isinstance (names , dict ):
385380 topic_names = self .topic_names
386381 for topic_id , topic_name in names .items ():
387382 try :
0 commit comments