创建词云图片 Args: file_path (str): Markdown 文件路径 season_no (int): 季度编号
(file_path, season_no)
| 100 | |
| 101 | |
| 102 | def create_wordcloud_img(file_path, season_no): |
| 103 | """ |
| 104 | 创建词云图片 |
| 105 | |
| 106 | Args: |
| 107 | file_path (str): Markdown 文件路径 |
| 108 | season_no (int): 季度编号 |
| 109 | """ |
| 110 | # 确保输出目录存在 |
| 111 | output_dir = 'resources/img' |
| 112 | os.makedirs(output_dir, exist_ok=True) |
| 113 | |
| 114 | img_name = f'{output_dir}/weekly_wordcloud_{season_no}.png' |
| 115 | if os.path.exists(img_name): |
| 116 | print(f'{img_name} already exists, skip creating wordcloud.') |
| 117 | return |
| 118 | |
| 119 | words = read_markdown_and_count_words(file_path) |
| 120 | word_freq = {word: freq for word, freq in words if freq > 5} |
| 121 | |
| 122 | # 使用系统字体 |
| 123 | font_path = '/System/Library/Fonts/Hiragino Sans GB.ttc' # macOS 系统自带中文字体 |
| 124 | |
| 125 | wc = WordCloud( |
| 126 | font_path=font_path, |
| 127 | background_color='white', |
| 128 | width=800, |
| 129 | height=520, |
| 130 | max_font_size=120, |
| 131 | max_words=180 |
| 132 | ) |
| 133 | wc.generate_from_frequencies(word_freq) |
| 134 | wc.to_file(img_name) |
| 135 | |
| 136 | |
| 137 | def main(): |
no test coverage detected