英语词频统计

str = '''A man was going to the house of some rich person. As he went along the road, he saw a box of good apples at the side of the road. He said, 
       I do not want to eat those apples;for the rich man will give me much food; he will give me very nice food to eat.Then he took the apples and 
       threw them away into the dust. He went on and came to a river. The river had become very big; so he could not go over it. He waited for some time; 
       then he said, I cannot go to the rich mans house today, for I cannot get over the river. He began to go home. He had eaten no food that day. 
       e began to want food. He came to the apples, and he was glad to take them out of the dust and eat them. Do not throw good things away; you may be glad to have them at some other time. 
'''

punctuation=['.',',',':',';']
prep=['of','to','a','the','and','some']

for i in range(len(punctuation)): #删除标点符号
    str = str.replace(punctuation[i],' ')

str=str.lower() #将大写转换成小写
str=str.split() #将字符串分成单词列表

d = dict()
for s in str:
    d[s] = d.get(s, 0) + 1                   #计数

for i in prep:
    if(d.get(i)!=None):                   #如果为介词,将其删去
        d.pop(i)

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

for i in range(10):  #输出前十个
    print(d[i])

运行截图:

 

posted @ 2018-03-22 10:32  146-王星宇  阅读(139)  评论(0编辑  收藏  举报