import re
f = open(r’C:/UsersYKL/Desktop/Walden.txt’,‘r’)
text = f.read()
f.close()
txt = txt.lower()
txt = re.sub(’[,.?:"’!-]’,’’,txt)
words = txt.split()
word_sq = {}
for i in words:
if i not in word_sq.keys():
word_sq[i] = 1
else:
word_sq[i] += 1
res = sorted(word_sq.items(),key=lambda x:x[1], reverse=True)
print(res)
小说《Walden.tet》单词词频统计
最新推荐文章于 2020-11-19 11:52:49 发布
该博客使用Python进行文本处理。通过读取文件内容,将文本转换为小写并去除标点符号,然后分割成单词。接着统计每个单词的出现次数,最后按出现次数对单词进行降序排序并输出结果。
2582

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



