Jieba库:主要是将字符串分割成单词
import jieba
txt = open("threekingdoms.txt", "r", encoding="utf-8").read()
# jieba.lcut(str) 将str分成片,不重复
words = jieba.lcut(txt)
counts = {}
for word in words:
if len(word) ==1:
continue
else:
# 如果word 不存在,则value默认为0
counts[word] = counts.get(word, 0)+1
# counts.items()是所有的键值对
items = list(counts.items())
items.sort(key=lambda x: x[1],reverse=True)
for i in range(15):
word, count = items[i]
print("{0:<10}{1:>5}".format(word, count))
使用Jieba进行文本分词统计
本文介绍如何利用Python中的Jieba库对《三国演义》文本进行分词处理,并统计出现频率较高的词汇。通过加载文本文件,使用jieba.lcut方法将文本切分为词语列表,接着统计每个词的出现次数并按出现频率从高到低排序。
1万+

被折叠的 条评论
为什么被折叠?



