可以下载一长篇的英文小说,进行词频的分析。

1.读入待分析的字符串

2.分解提取单词 

3.计数字典

4.排除语法型词汇

5.排序

6.输出TOP(20)

7.对输出结果的简要说明。

#读入待分析的字符串
fo=open('test.txt','r')
str=fo.read()
fo.close()

str=str.lower()

#分解提取单词 
for i in ',.?!"\n--':
    str=str.replace(i,' ')
words=str.split(' ')

#排除语法型词汇
dict={}
ecp=set(['','a','an','the','and','to','in','on','of','for','i','our','us','into','her','we',
         'when','their','my','from','them','with','after','would','was','had','that','while',
         'his','she','up','it','they','so','by'])
keys=set(words)-ecp 

#创建计数字典
for i in keys:
    dict[i]=words.count(i)
#将字典转换为列表,以便后序排序
items=list(dict.items()) 

#排序
items.sort(key=lambda x:x[1],reverse=True)

#输出TOP(20)
print('输出TOP20:')
for i in range(20):
   print(items[i])

这篇英语文章阐述了家庭内孩子与母亲之前出去游玩所发生的小趣事。

 

posted on 2017-09-27 17:32  077吴文欣  阅读(171)  评论(0编辑  收藏  举报