1.简单字符串分割计算词频
import re
lyric='The night begin to shine,the night begin to shine'
words=lyric.split()#字符分割
words1=[word.lower() for word in words]#转小写字母
words2=[re.sub(',','',word) for word in words1]#去掉标点
words3=set(words2)#列表去重
for word in words3:
print(word+'频次:',end='')
print(words2.count(word))
输出结果:
night频次:2
shinethe频次:1
begin频次:2
shine频次:1
to频次:2
the频次:1
2.文件中字符串计算词频
import re
file=open('C:/Users/John/Desktop/Walden1.txt','r')
str=file.read()
#print(str)
words=str.split()
words1=[word.lower() for word in words]
words2=[re.sub(',','.',word) for word in words1]
words_index=set(words2)
dic={index:words2.count(index) for index in words_index}
sorted(dic.items(),key=lambda asd:asd[1],reverse=True)