作业练习P194,jieba应用,读取,分词,存储,生成词云,排序,保存
import jieba #第一题 txt='Python是最有意思的编程语言' words=jieba.lcut(txt) #精确分词 words_all=jieba.lcut(txt,cut_all=True) #全分词 words_sh=jieba.lcut_for_search(txt) #搜索分词 print(words) print(words_all) print(words_sh) #第二题 txt1="今天晚上我吃了意大利面" jieba.add_word("意大利面") #增加分词 words=jieba.lcut(txt1) print(words) #第三题 filepath=r'D:/python/test-py/水浒传.txt' f=open(filepath,'r',encoding='utf-8') txt=f.read() f.close() words=jieba.lcut(txt) counts={} keylist=[] for word in words: if len(word)==1: continue else: counts[word]=counts.get(word,0)+1 items=list(counts.items()) items.sort(key=lambda x:x[1],reverse=True) for i in range(35): word,count=items[i] keylist.append(word) print("{0:^15}--{1:>5}".format(word,count)) print('\n排名前15个词为:',keylist) #对统计结果进行保存 f=open(r'D:/python/test-py/shuihuzhuang.txt','w') for i in range(35): f.write(str(items[i])) ##for i in str(items): ## f.write(i) ##f.write(str(keylist)) f.close() #接上面程序,使用wordcloud输出 def pt(ls): print(ls) from wordcloud import WordCloud #前15个生成新的列表 ##pt('\n新的列表为:') ##for a,b in items[1:16]: #取15个元素 ## keylist.append(a) ##pt(keylist) newtxt=' '.join(keylist) #空格替换 print('\n空格替换后生成:',newtxt) excludes={'一个'} wordcloud=WordCloud(background_color='white',\ width=800,\ height=600,\ font_path='msyhbd.ttf',\ max_words=200,\ max_font_size=80,\ stopwords=excludes,\ ).generate(newtxt) #生成词云 wordcloud.to_file(r'D:/python/test-py/水浒传.png') ###z最后执行打包合作,在cmd.exe命令行程序 切换到py存放文件夹输入:PyInstaller -F p194zuoye.py ##newlist=[a[i] for a in items] ##pt(newlist)