@@ -850,6 +850,7 @@ def topics_over_time(
850850 datetime_format : str | None = None ,
851851 evolution_tuning : bool = True ,
852852 global_tuning : bool = True ,
853+ use_representation_model : bool = False ,
853854 ) -> IntoDataFrame :
854855 return variations .topics_over_time (
855856 topic_model = self ,
@@ -860,6 +861,7 @@ def topics_over_time(
860861 datetime_format = datetime_format ,
861862 evolution_tuning = evolution_tuning ,
862863 global_tuning = global_tuning ,
864+ use_representation_model = use_representation_model ,
863865 )
864866
865867 @wraps (variations .topics_per_class )
@@ -868,12 +870,14 @@ def topics_per_class(
868870 docs : list [str ],
869871 classes : list [int | str ],
870872 global_tuning : bool = True ,
873+ use_representation_model : bool = False ,
871874 ) -> IntoDataFrame :
872875 return variations .topics_per_class (
873876 topic_model = self ,
874877 docs = docs ,
875878 classes = classes ,
876879 global_tuning = global_tuning ,
880+ use_representation_model = use_representation_model ,
877881 )
878882
879883 @wraps (variations .approximate_distribution )
@@ -1593,15 +1597,12 @@ def generate_topic_labels(
15931597 check_is_fitted (self )
15941598 topic_labels = []
15951599 for topic in self ._topics :
1596- if aspect :
1597- representation = topic .representations .get (aspect )
1598- else :
1599- representation = topic .representations .get ("Main" )
1600+ representation = topic .representations .get (aspect or "Main" )
16001601
1601- # For non-keyword representations (Label, StructuredJSON), use the
1602- # topic label directly since word-level operations don't apply.
1602+ # For non-keyword representations (Label, StructuredJSON), use their
1603+ # label directly since word-level operations don't apply.
16031604 if representation and not isinstance (representation , Keywords ):
1604- label = topic .label
1605+ label = representation .label
16051606 if topic_prefix :
16061607 topic_label = f"{ topic .id } { separator } { label } "
16071608 else :
@@ -2974,8 +2975,8 @@ def _extract_words_per_topic(
29742975 indices = np .take_along_axis (indices , sorted_indices , axis = 1 )
29752976 scores = np .take_along_axis (scores , sorted_indices , axis = 1 )
29762977
2977- # Get top 30 words per topic based on c-TF-IDF score
2978- topic_representations = {
2978+ # Get top 30 words per topic based on c-TF-IDF score, the candidates every representation starts from
2979+ candidates = {
29792980 topic_id : Keywords (
29802981 [
29812982 (words [word_index ], score ) if word_index is not None and score > 0 else ("" , 0.00001 )
@@ -2993,7 +2994,7 @@ def _extract_words_per_topic(
29932994 topic_representations = self ._apply_representation_models (
29942995 representation_models = self .representation_model ,
29952996 corpus = corpus ,
2996- topic_representations = topic_representations ,
2997+ topic_representations = candidates . copy () ,
29972998 c_tf_idf = c_tf_idf ,
29982999 name = "Main" ,
29993000 )
@@ -3002,10 +3003,10 @@ def _extract_words_per_topic(
30023003 # so cut the extra candidates back to the `top_n_words` that were asked for
30033004 topic_representations = {
30043005 topic_id : Keywords (keywords .data [: self .top_n_words ])
3005- for topic_id , keywords in topic_representations .items ()
3006+ for topic_id , keywords in candidates .items ()
30063007 }
30073008
3008- # Extract topic representations other than the Main one (called aspects)
3009+ # Extract topic representations other than the Main one (called aspects), each from the candidates
30093010 if calculate_aspects and isinstance (self .representation_model , dict ):
30103011 if aspect_to_return is not None :
30113012 if aspect_to_return not in self .representation_model :
@@ -3016,20 +3017,21 @@ def _extract_words_per_topic(
30163017 return self ._apply_representation_models (
30173018 representation_models = self .representation_model [aspect_to_return ],
30183019 corpus = corpus ,
3019- topic_representations = topic_representations .copy (),
3020+ topic_representations = candidates .copy (),
30203021 c_tf_idf = c_tf_idf ,
30213022 name = aspect_to_return ,
30223023 )
30233024 else :
30243025 for name , aspect_model in self .representation_model .items ():
3025- aspect = self ._apply_representation_models (
3026- representation_models = aspect_model ,
3027- corpus = corpus ,
3028- topic_representations = topic_representations .copy (),
3029- c_tf_idf = c_tf_idf ,
3030- name = name ,
3031- )
3032- self ._topics .set_data (representations = {name : aspect })
3026+ if name != "Main" :
3027+ aspect = self ._apply_representation_models (
3028+ representation_models = aspect_model ,
3029+ corpus = corpus ,
3030+ topic_representations = candidates .copy (),
3031+ c_tf_idf = c_tf_idf ,
3032+ name = name ,
3033+ )
3034+ self ._topics .set_data (representations = {name : aspect })
30333035
30343036 return topic_representations
30353037
0 commit comments