@@ -327,21 +327,18 @@ def probabilities_(self) -> list[int]:
327327 @property
328328 def topic_representations_ (self ) -> dict [int , list [tuple [str , float ]]]:
329329 """For backwards compatibility."""
330- representations = {}
331- for topic in self ._topics :
332- representations [topic .id ] = topic .representations ["Main" ].data
333- return representations if representations else None
330+ return {topic .id : topic .representations ["Main" ].word_scores for topic in self ._topics } or None
334331
335332 @property
336333 def topic_aspects_ (self ) -> dict [int , dict [str , list [tuple [str , float ]]]]:
337334 """For backwards compatibility."""
338335 first_topic = next (iter (self ._topics ), None )
339- aspect_names = list (first_topic .representations ) if first_topic else []
340- aspects = {
341- aspect_name : {topic .id : topic .representations [aspect_name ].data for topic in self ._topics }
342- for aspect_name in aspect_names
336+ aspects = first_topic .representations if first_topic else []
337+ return {
338+ name : {topic .id : topic .representations [name ].word_scores for topic in self ._topics }
339+ for name in aspects
340+ if name != "Main"
343341 }
344- return aspects
345342
346343 @property
347344 def topic_sizes_ (self ) -> dict [int , int ]:
@@ -364,9 +361,9 @@ def topic_labels_(self) -> dict[int, str]:
364361 return self ._topics .labels
365362
366363 @property
367- def custom_labels_ (self ) -> list [str ]:
368- """For backwards compatibility."""
369- return [ topic . label for topic in self ._topics ]
364+ def custom_labels_ (self ) -> list [str ] | None :
365+ """For backwards compatibility. None until `set_topic_labels` is called. """
366+ return self ._topics . custom_labels
370367
371368 @property
372369 def representative_docs_ (self ) -> dict [int , str ]:
@@ -1129,6 +1126,29 @@ def get_topic(self, topic: int, full: bool = False) -> dict[str, tuple[str, floa
11291126 else :
11301127 return False
11311128
1129+ def get_representation (self , topic : int , aspect : str = "Main" ) -> TopicRepresentation :
1130+ """Return a topic's representation with its structure intact.
1131+
1132+ `get_topic` flattens every representation to words and scores, which loses the shape
1133+ of anything richer than keywords, such as the dictionary an LLM returns. This returns
1134+ the representation itself.
1135+
1136+ Arguments:
1137+ topic: A specific topic for which you want its representation
1138+ aspect: Which representation to return, defaulting to the main one
1139+
1140+ Returns:
1141+ The `TopicRepresentation` for that topic and aspect
1142+
1143+ Examples:
1144+ ```python
1145+ representation = topic_model.get_representation(12)
1146+ representation.words
1147+ ```
1148+ """
1149+ check_is_fitted (self )
1150+ return self ._topics [topic ].representations [aspect ]
1151+
11321152 def get_topic_info (self , topic : int | None = None ) -> pl .DataFrame :
11331153 """Get information about each topic including its ID, frequency, and name.
11341154
0 commit comments