Skip to content

Commit d5d84d7

Browse files
Added pretty printing to TopicData
1 parent a8b511b commit d5d84d7

1 file changed

Lines changed: 22 additions & 0 deletions

File tree

turftopic/data.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99

1010
import joblib
1111
import numpy as np
12+
from rich.console import Console
13+
from rich.tree import Tree
1214

1315
from turftopic.container import TopicContainer
1416

@@ -157,6 +159,26 @@ def __iter__(self):
157159
def get_vocab(self) -> np.ndarray:
158160
return self.vocab
159161

162+
def __str__(self):
163+
console = Console()
164+
with console.capture() as capture:
165+
tree = Tree("TopicData")
166+
for key, value in self.items():
167+
if value is None:
168+
continue
169+
if hasattr(value, "shape"):
170+
text = f"{key} {value.shape}"
171+
elif hasattr(value, "__len__"):
172+
text = f"{key} ({len(value)})"
173+
else:
174+
text = key
175+
tree.add(text)
176+
console.print(tree)
177+
return capture.get()
178+
179+
def __repr__(self):
180+
return str(self)
181+
160182
def visualize_topicwizard(self, **kwargs):
161183
"""Opens the topicwizard web app with which you can interactively investigate your model.
162184
See [topicwizard's documentation](https://github.com/x-tabdeveloping/topicwizard) for more detail.

0 commit comments

Comments
 (0)