
(cmd命令行)pip install wordcloud












- import wordcloud
- txt=open("hamlet.txt","r").read()
- txt=txt.lower()
- for ch in '!"#$%&()*+,-./:;<=>?@[\\]^_‘{|}~':
- txt=txt.replace(ch," ")
-
- w=wordcloud.WordCloud(background_color="white",width=600,height=400,min_font_size=10,max_font_size=100,max_words=30)
- w.generate(txt)
- w.to_file("hamlet.png")


- import jieba
- import wordcloud
- txt=open("C:\\Users\\吉祥瑞\\Desktop\\python\\UNIT 6\\threekingdoms.txt","r",\
- encoding="UTF-8").read()
- txts=jieba.lcut(txt)
- w=wordcloud.WordCloud(background_color="white",width=600,height=400,\
- min_font_size=10,max_font_size=100,max_words=100,\
- font_path="msyh.ttc")
- w.generate(" ".join(txts))
- w.to_file("threekingdoms.png")

去掉一些词汇
- import jieba
- import wordcloud
- txt=open("C:\\Users\\吉祥瑞\\Desktop\\python\\UNIT 6\\threekingdoms.txt","r",\
- encoding="UTF-8").read()
- txt=jieba.lcut(txt)#txt是list
- a=[]
- w=wordcloud.WordCloud(background_color="white",width=600,height=400,\
- min_font_size=10,max_font_size=100,max_words=100,\
- font_path="msyh.ttc",\
- stopwords={"我","了","到","曰","又","吾","去","来","为","将"\
- "将军","却说","荆州","二人","不可","不能","如此"})
- w.generate(" ".join(txt))
- w.to_file("threekingdoms.png")
-
