文件方式实现完整的英文词频统计实例

1.读入待分析的字符串

2.分解提取单词 

3.计数字典

4.排除语法型词汇

5.排序

6.输出TOP(20)

str='''For Chinese people, eating mooncakes with families is one of the important ways to celebrate the Mid-Autumn Festival, which falls on Oct 4 this year, or the 15th day of the 8th Chinese lunar month.

As mass-produced mooncakes sweep market with different flavors mooncakes and various stuffing, customers are inclined to turn to traditional taste, which makes hand-made mooncakes a hot commodity in the market.

Wei Quanfa's hand-made mooncakes is one such case. The 72-year-old baker inherited the traditional craft from his family and started his new business in Zhengzhou, Central China's Henan province.

He carries out every procedure by himself, mooncakes including making stuffing and the mooncake mould, boiling oil, and baking. Because of that, Wei is able to make no more than 40 kg a day in the last month before the festival, and a 30-centimeter-diameter mooncake is priced at over 200 yuan ($30).

With the festival just around the corner, reserving Wei's mooncakes even two days in advance is next to impossible.'''
fo=open('C:\\Users\\Administrator\\Desktop\\news.txt','w')
fo.write(str)
fo.close


fo=open('C:\\Users\\Administrator\\Desktop\\news.txt','r')
news=fo.read()
fo.close()

news=str.lower()  #小写

for i in ",.()-$":
    news=news.replace(i," ")  #空格替换


news=news.split(" ")  #分割字段

exc={'the','one','which','his',' ','a','with','for','to','is','of','on','in','out','no','at','over''such','that','and','or'}



words=set(news)
words=words-exc

dt={}

for i in words:
    dt[i]=news.count(i)  #统计


item=list(dt.items())

item.sort(key=lambda x:x[1],reverse=True)

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

 

posted on 2017-09-26 11:53  152陈斯璐  阅读(94)  评论(0编辑  收藏  举报