#文本词频统计.py
def getFileText(): #定义函数:
filTxt=open('../Stu_pack/file/See You Again.txt','r').read()
filTxt=filTxt.lower() #将文本里所有字母都转换成小写
for ch in '!"#$%&()*+-*/,.<=>?@[]\\^_{}|~"':#将所有的标点符号都用空格代替
fillTxt=filTxt.replace(ch,' ')
return filTxt
letterTxt=getFileText()
words=letterTxt.split() #将只有带空格的文本按空格分成每个单词
wdCountDict={} #定义字典变量以备记录单词出现的次数和最后输出内容
excludes={'the','you','your','that','will'}
for word in words: #获取某单词的拼写和累加出现的次数
wdCountDict[word]=wdCountDict.get(word,0)+1
for word in excludes:
Python——文本词频统计
最新推荐文章于 2023-10-25 22:16:54 发布