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

Posted 152陈斯璐

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了文件方式实现完整的英文词频统计实例相关的知识,希望对你有一定的参考价值。

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])

 

以上是关于文件方式实现完整的英文词频统计实例的主要内容,如果未能解决你的问题,请参考以下文章

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

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

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

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

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

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