|
|
import json
|
|
|
import wordcloud
|
|
|
import matplotlib.pyplot as plt
|
|
|
import os
|
|
|
|
|
|
|
|
|
with open('./word_frequency.json', 'r', encoding='utf-8') as f:
|
|
|
word_freq = json.load(f)
|
|
|
|
|
|
|
|
|
def get_chinese_font():
|
|
|
|
|
|
font_paths = [
|
|
|
"C:/Windows/Fonts/simhei.ttf",
|
|
|
"C:/Windows/Fonts/simsun.ttc",
|
|
|
"C:/Windows/Fonts/msyh.ttc",
|
|
|
"C:/Windows/Fonts/SIMKAI.TTF",
|
|
|
"E:/Downloads/times+simsun.ttf",
|
|
|
]
|
|
|
|
|
|
for font_path in font_paths:
|
|
|
if os.path.exists(font_path):
|
|
|
print(f"使用字体: {font_path}")
|
|
|
return font_path
|
|
|
|
|
|
print("未找到合适的中文字体,将使用默认字体(可能无法正确显示中文)")
|
|
|
return None
|
|
|
|
|
|
|
|
|
chinese_font = get_chinese_font()
|
|
|
|
|
|
|
|
|
wc = wordcloud.WordCloud(
|
|
|
font_path=chinese_font,
|
|
|
background_color="white",
|
|
|
max_words=200,
|
|
|
max_font_size=255,
|
|
|
width=800,
|
|
|
height=600,
|
|
|
|
|
|
)
|
|
|
|
|
|
try:
|
|
|
|
|
|
wc = wc.generate_from_frequencies(word_freq)
|
|
|
|
|
|
plt.figure(figsize=(10, 8))
|
|
|
plt.imshow(wc, interpolation='bilinear')
|
|
|
plt.axis("off")
|
|
|
plt.tight_layout(pad=0)
|
|
|
plt.show()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
except Exception as e:
|
|
|
print(f"生成词云时出错: {e}")
|
|
|
print("尝试使用默认设置...")
|
|
|
|
|
|
|
|
|
wc_default = wordcloud.WordCloud(
|
|
|
background_color="white",
|
|
|
max_words=200,
|
|
|
max_font_size=255,
|
|
|
width=800,
|
|
|
height=600
|
|
|
)
|
|
|
|
|
|
wc_default = wc_default.generate_from_frequencies(word_freq)
|
|
|
plt.figure(figsize=(10, 8))
|
|
|
plt.imshow(wc_default, interpolation='bilinear')
|
|
|
plt.axis("off")
|
|
|
plt.tight_layout(pad=0)
|
|
|
plt.show()
|
|
|
|
|
|
"""Wordcloud详细参数设置
|
|
|
def __init__(self, font_path=None, width=400, height=200, margin=2,
|
|
|
ranks_only=None, prefer_horizontal=.9, mask=None, scale=1,
|
|
|
color_func=None, max_words=200, min_font_size=4,
|
|
|
stopwords=None, random_state=None, background_color='black',
|
|
|
max_font_size=None, font_step=1, mode="RGB",
|
|
|
relative_scaling='auto', regexp=None, collocations=True,
|
|
|
colormap=None, normalize_plurals=True, contour_width=0,
|
|
|
contour_color='black', repeat=False,
|
|
|
include_numbers=False, min_word_length=0):
|
|
|
""" |