我们常常看到很多英语培训方面的书籍, 告诉你考研, 雅思, 四六级等听力, 阅读的高频单词有哪些. 他们是怎么做到的呢?
其实通过 Python, 通过简单几行代码就可以实现.
- import re #导入正则表达式
- from collections import Counter #导入 python 的统计模块
- txt =open('test.txt', 'r').read().lower() #打开要统计的文本
- def word_count(num):
- word_pattern =r'[a-zA-Z-]+' #正则表达式, 搜集文本
- words = re.findall(word_pattern, txt) #查询文本, 筛选符合单词特征的, 去除不符合的汉字图表等
- return Counter(words).most_comm(num) #返回你向统计的前几名的高频单词, num 输入你的想定义的前几名, num = 10, 代表统计前十名的高频单词
很神奇, 简单几行代码就实现了统计高频单词. 通过延伸应用, 还可以统计哪些文字是该文章的核心关键词.
来源: http://www.jianshu.com/p/b35da95e08b9