用python生成云词汇_用python生成词云wordcloud
代碼如下:
import wordcloud
import jieba
excludes={"將軍","卻說","二人","不可","荊州","不能",
"如此","商議","如何","主公","軍士","左右","軍馬","次日",
"大喜","引兵","天下","東吳","于是","今日","不敢","魏兵","陛下",
"一人","都督","人馬","不知","漢中","只見","眾將","后主",
"蜀兵","上馬","大叫","太守","此人","夫人","先主","后人",
"背后","城中","天子","一面","何不","大軍","孔明曰","肅曰","權曰","玄德曰","操曰","布曰","懿曰","關公曰"}
def getText():
txt = open("threekingdoms.txt", "r", encoding="utf-8").read()
for ch in '! ,!。(;)、:一一、?“”"@ #$%^&*()_+=-?/<>,.;:`~[]{}|\\':
txt=txt.replace(ch," ")
for tyc in excludes:
txt=txt.replace(tyc,"")
return txt
Text=getText()
w=wordcloud.WordCloud(width=1000,\
font_path="simfang.ttf",\
height=700,background_color="white")
w.generate(''.join(jieba.cut(Text)))
w.to_file("jeba1.png")
剛開始的時候用中文會出現以下的情況
然后在代碼中調用了
這個字體之后正常顯示
總結
以上是生活随笔為你收集整理的用python生成云词汇_用python生成词云wordcloud的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 文件标识符无效。使用 fopen 生成有
- 下一篇: linux git还原文件,Gitlab