def getTxt():
txt=open("hamlet.txt","r").read()
txt=txt.lower()
for ch in '!"#$%&()*+,-./:;<=>?@[\\]^_`{}|~':
txt=txt.replace(ch," ")
return txt
hamletTxt=getTxt()
words=hamletTxt.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)
excludes=['the','and','to','of','you','i','a','my','in',\
'it','that','is',' not','his','this','but',\
'with','for','not','your','me','be','as','he',\
'what','him','so','have','will','do','no','we',\
'are&#
文本文件的词频统计(包含excludes排除库)
最新推荐文章于 2023-10-25 22:16:54 发布
def getTxt(): txt=open("hamlet.txt","r").read() txt=txt.lower() for ch in '!"#$%&()*+,-./:;?@[\\]^_`{}|~': txt=txt.replace(ch," ") return txthamletTxt=getTxt()words=hamletTxt
摘要由CSDN通过智能技术生成