Python进行词频统计

基础python统计词频,未考虑到删除停用词

# 词频统计
def getText():#处理文件
    txt=open("English.txt","r").read()
    txt = txt.lower()#将英文全部变为小写
    for ch in '!"#$&*+,-./:;<=>?@[\\]^_{}|':
        txt = txt.replace(ch," ")
    return txt

myTxt = getText()

words = myTxt.split()#将字符分割为列表

counts={} #新建一个空字典

for word in words:
    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):#打印词频在前20位的单词
    word,count = items[i]
    print("{0:<10}{1:>5}".format(word,count))

你可能感兴趣的:(其他)