|
| 1 | +fromosimportpath |
| 2 | +fromwordcloudimportWordCloud |
| 3 | +importjieba |
| 4 | +importre |
| 5 | +# |
| 6 | +# special_character_removal = re.compile(r'[,。、【 】“”:;()《》‘’{}?!⑦%>℃.^-——=&#@¥『』]', re.IGNORECASE) |
| 7 | +# |
| 8 | +# # |
| 9 | +# # text ="" |
| 10 | +# fw=open("hlm_seg.txt","w",encoding="utf-8") |
| 11 | +# with open('hlm.txt',encoding="utf-8") as fp: |
| 12 | +# for line in fp: |
| 13 | +# l = special_character_removal.sub('', line.strip()) |
| 14 | +# words=jieba.cut(l) |
| 15 | +# |
| 16 | +# t=" ".join(words) |
| 17 | +# fw.write(t) |
| 18 | +# fw.write("\n") |
| 19 | +# fw.close() |
| 20 | + |
| 21 | + |
| 22 | +# import numpy as np |
| 23 | +# from PIL import Image, ImageDraw, ImageFont |
| 24 | +# background_image = np.array(Image.open("background.png")) |
| 25 | +d=path.dirname(__file__) |
| 26 | +# Read the whole text. |
| 27 | +text=open(path.join(d,'hlm_seg.txt'),encoding="utf-8").read() |
| 28 | +# Generate a word cloud image |
| 29 | +# font=path.join(d, "simkai.ttf") |
| 30 | +font='C:/Windows/Fonts/simkai.ttf' |
| 31 | +wordcloud=WordCloud(font_path=font,#设置中文字体,不指定就会出现中文不显示 |
| 32 | +width=1024,#宽 |
| 33 | +height=840,#高 |
| 34 | +background_color='white',#设置背景色 |
| 35 | +# mask=background_image#背景 |
| 36 | +# max_words=100,#最大词汇数 |
| 37 | +# max_font_size=100#最大号字体 |
| 38 | + ).generate(text) |
| 39 | + |
| 40 | +# Display the generated image: |
| 41 | +# the matplotlib way: |
| 42 | +importmatplotlib.pyplotasplt |
| 43 | + |
| 44 | +# lower max_font_size |
| 45 | +# wordcloud = WordCloud(max_font_size=40).generate(text) |
| 46 | + |
| 47 | +plt.figure() |
| 48 | +plt.imshow(wordcloud) |
| 49 | +plt.axis("off") |
| 50 | +plt.show() |