中文分词
- 下载一中文长篇小说,并转换成UTF-8编码。
- 使用jieba库,进行中文词频统计,输出TOP20的词及出现次数。
- **排除一些无意义词、合并同一词。
- **使用wordcloud库绘制一个词云。
import jieba txt="E://novels.txt" book=open(txt,"r",encoding='utf-8').read() excepts={'前言','第一卷'} ls=[] words=jieba.lcut(book) counts={} for i in words: ls.append(i) if len(i)==1: continue else: counts[i]=counts.get(i,0)+1 for i in excepts: del(counts[i]) items=list(counts.items()) items.sort(key=lambda x:x[1],reverse=True) print('出现频率最高的词组前十:') for j in range(10): i , count=items[j] print("{:<10}{}".format(i,count))