Skip to content

Commit

Permalink
Joined rename_topics and name_topics methods together
Browse files Browse the repository at this point in the history
  • Loading branch information
x-tabdeveloping committed Nov 4, 2024
1 parent 841732a commit d743ed2
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 17 deletions.
27 changes: 11 additions & 16 deletions turftopic/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,19 +84,7 @@ def _top_terms(
terms.append(list(vocab[highest]))
return terms

def name_topics(self, namer: TopicNamer) -> list[str]:
"""Names topics with a topic namer in the model.
Parameters
----------
namer: TopicNamer
A Topic namer model to name topics with.
Returns
-------
list[str]
List of topic names.
"""
def _rename_automatic(self, namer: TopicNamer) -> list[str]:
self.topic_names_ = namer.name_topics(self._top_terms())
return self.topic_names_

Expand Down Expand Up @@ -366,22 +354,29 @@ def topic_names(self) -> list[str]:
names.append(f"{topic_id}_{concat_words}")
return names

def rename_topics(self, names: Union[list[str], dict[int, str]]) -> None:
"""Rename topics in a model manually.
def rename_topics(
self, names: Union[list[str], dict[int, str], TopicNamer]
) -> None:
"""Rename topics in a model manually or automatically, using a namer.
Examples:
```python
model.rename_topics(["Automobiles", "Telephones"])
# Or:
model.rename_topics({-1: "Outliers", 2: "Christianity"})
# Or:
namer = OpenAITopicNamer()
model.rename_topics(namer)
```
Parameters
----------
names: list[str] or dict[int,str]
Should be a list of topic names, or a mapping of topic IDs to names.
"""
if isinstance(names, dict):
if isinstance(names, TopicNamer):
self._rename_automatic(names)
elif isinstance(names, dict):
topic_names = self.topic_names
for topic_id, topic_name in names.items():
try:
Expand Down
2 changes: 1 addition & 1 deletion turftopic/models/decomp.py
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ def fit_transform(
console.log("Model fitting done.")
return doc_topic

def name_topics(self, namer: TopicNamer) -> list[str]:
def _rename_automatic(self, namer: TopicNamer) -> list[str]:
"""Names topics with a topic namer in the model.
Parameters
Expand Down

0 comments on commit d743ed2

Please sign in to comment.