jieba库对中文文本进行切割 python
jieba是中文文本用于分词的库,有3种模式:精确模式、全模式(所有可能的分割)、搜索引擎模式(在精确模式基础上再进行分割) 具体参考 PYPI # coding=utf-8 import jieba #txt = open(u"D:\data\ebook\红楼梦.txt","r").read() #,encoding='utf-8' txt = open("D:\\data\\ebook\\1.txt","r",encoding='utf-8').read() words = jieba.lcut_for_search(txt) # 使用搜索引擎模式对文本进行分词 counts = {} # 通过键值对的形式存储词语及其出现的次数 for word in words: print(word) if len(word) == 1: # 长度为1的词语不计算在内 continue else: counts[word] = counts.get(word, 0) + 1 # 遍历所有词语,每出现一次其对应的值加 1 items = list(counts.items())#将键值对转换成列表 items.sort(key=lambda x: x[1], reverse=True) # 根据词语出现的次数进行从大到小排序 for i in range(5): word, count =