首页 > 其他 > 详细

第四次作业——完整的中英文词频统计

时间:2018-10-15 13:01:31      阅读:120      评论:0      收藏:0      [点我收藏+]
fo=open(‘C:\\Users\\Administrator\\Desktop\\Novel.txt‘,‘r‘)
str=fo.read()
fo.close()

str=str.lower() #转化为小写

#替换符号
str=str.replace(‘;‘,‘ ‘)
str=str.replace(‘,‘,‘ ‘)
str=str.replace(‘-‘,‘ ‘)

#分割
str=str.split()

#设置列表
strset=set(str)
#排除语法型词汇,代词等
escape={‘a‘,‘the‘,‘and‘,‘it‘,‘i‘,‘you‘,‘we‘,‘of‘,‘to‘}
strset=strset-escape
print(strset)

#以字典形式输出
strdict={}
for word in strset:
    strdict[word]=str.count(word)

#按词频排序
wordlist=list(strdict.items())
wordlist.sort(key=lambda x:x[1],reverse=True)



for i in range(20):
    print(wordlist[i])

  技术分享图片

 

2

 

fo=open(C:\\Users\\Administrator\\Desktop\\星辰变.txt,r)
str=fo.read()


#替换符号
str=str.replace(:, )
str=str.replace(, )
str=str.replace(-, )
str=str.replace(, )
str=str.replace(, )

#分割
str=str.split()

#设置列表
strset=set(str)
#排除无意义词语
escape={,,,,}
strset=strset-escape
print(strset)

#设置字典
strdict={}
for word in strset:
    strdict[word]=str.count(word)

#按词频排序
wordlist=list(strdict.items())
wordlist.sort(key=lambda x:x[1],reverse=True)

for i in range(20):
    print(wordlist[i])

技术分享图片

第四次作业——完整的中英文词频统计

原文:https://www.cnblogs.com/yuxiang1212/p/9790133.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!