统计文本中的词频,如何将下满代码封装成函数调用

143 阅读1分钟

统计文本中的词频,如何将下满代码封装成函数调用

import jieba
 
def getText():
# 读取整个文件的内容
txt = open('dou.txt','r',encoding='utf-8').read()
# 精确模式分词,保存在列表中
words = jieba.lcut(txt)
counts = {}
# 词频统计
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(20):
    word,count = items[i]
    print('{0:<10}{1:>5}'.format(word,count))