@@ -235,6 +235,20 @@ def _normalize_case_token_counts(self, counts: Counter) -> Counter:
235235 normalized_counts = Counter (dict (zip (df_counts_agg .token_majority_case , df_counts_agg ["sum" ])))
236236 return normalized_counts
237237
238+ def _save_chart (self , fig : plt .figure ) -> BytesIO :
239+ """Private method to save chart as a bytes stream
240+
241+ Args:
242+ fig (plt.figure): matplotlib figure to save
243+
244+ Returns:
245+ BytesIO: bytes stream containing the chart's data
246+ """
247+ temp = BytesIO ()
248+ fig .savefig (temp , bbox_inches = self .bbox_inches , pad_inches = self .pad_inches , dpi = fig .dpi )
249+ plt .close ()
250+ return temp
251+
238252 @time_logging (log_message = "Counting tokens" )
239253 def _count_tokens (self , docs : List [Doc ]) -> List [Tuple [AnyStr , Dict ]]:
240254 """Private method to count tokens for each document in corpus
@@ -303,19 +317,15 @@ def generate_wordclouds(self, counts: List[Tuple[AnyStr, Dict]]) -> Generator[Tu
303317 else :
304318 fig = self ._generate_wordcloud (frequencies = count , language = self .language , title = wordcloud_title )
305319 # Return chart
306- temp = BytesIO ()
307- fig .savefig (temp , bbox_inches = self .bbox_inches , pad_inches = self .pad_inches , dpi = fig .dpi )
308- plt .close ()
320+ temp = self ._save_chart (fig )
309321 yield (temp , output_file_name )
310322
311323 else :
312324 # Generate chart
313325 count = counts [0 ][1 ]
314326 fig = self ._generate_wordcloud (frequencies = count , language = self .language )
315327 # Return chart
316- temp = BytesIO ()
317- fig .savefig (temp , bbox_inches = self .bbox_inches , pad_inches = self .pad_inches , dpi = fig .dpi )
318- plt .close ()
328+ temp = self ._save_chart (fig )
319329 yield (temp , "wordcloud.png" )
320330
321331 def tokenize_and_count (self , df : pd .DataFrame ) -> List [Tuple [AnyStr , Dict ]]:
0 commit comments