Python实现统计英文文章词频的方法分析


Posted in Python onJanuary 28, 2019

本文实例讲述了Python实现统计英文文章词频的方法。分享给大家供大家参考,具体如下:

应用介绍:

统计英文文章词频是很常见的需求,本文利用python实现。

思路分析:

1、把英文文章的每个单词放到列表里,并统计列表长度;
2、遍历列表,对每个单词出现的次数进行统计,并将结果存储在字典中;
3、利用步骤1中获得的列表长度,求出每个单词出现的频率,并将结果存储在频率字典中;
4、以字典键值对的“值”为标准,对字典进行排序,输出结果(也可利用切片输出频率最大或最小的特定几个,因为经过排序sorted()函数处理后,单词及其频率信息已经存储在元组中,所有元组再组成列表。)

代码实现:

fin = open('The_Magic_Skin _Honore_de_Balzac.txt') #the txt is up
#to you
lines=fin.readlines()
fin.close()
'''transform the article into word list
'''
def words_list():
  chardigit='ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789 '
  all_lines = ''
  for line in lines:
    one_line=''
    for ch in line:
      if ch in chardigit:
        one_line = one_line + ch
    all_lines = all_lines + one_line
  return all_lines.split()
'''calculate the total number of article list
s is the article list
'''
def total_num(s):
  return len(s)
'''calculate the occurrence times of every word
t is the article list
'''
def word_dic(t):
  fre_dic = dict()
  for i in range(len(t)):
    fre_dic[t[i]] = fre_dic.get(t[i],0) + 1
  return fre_dic
'''calculate the occurrence times of every word
w is dictionary of the occurrence times of every word
'''
def word_fre(w):
  for key in w:
    w[key] = w[key] / total
  return w
'''sort the dictionary
v is the frequency of words
'''
def word_sort(v):
  sort_dic = sorted(v.items(), key = lambda e:e[1])
  return sort_dic
'''This is entrance of functions
output is the ten words with the largest frequency
'''
total = total_num(words_list())
print(word_sort(word_fre(word_dic(words_list())))[-10:])
Python 相关文章推荐
详解Python的Django框架中的通用视图
May 04 Python
Python制作数据导入导出工具
Jul 31 Python
详解Python中for循环是如何工作的
Jun 30 Python
python按综合、销量排序抓取100页的淘宝商品列表信息
Feb 24 Python
pycharm修改文件的默认打开方式的步骤
Jul 29 Python
python3实现带多张图片、附件的邮件发送
Aug 10 Python
Python 在OpenCV里实现仿射变换—坐标变换效果
Aug 30 Python
tensorflow对图像进行拼接的例子
Feb 05 Python
Python中SQLite如何使用
May 27 Python
使用python-cv2实现Harr+Adaboost人脸识别的示例
Oct 27 Python
搭建pypi私有仓库实现过程详解
Nov 25 Python
用python画城市轮播地图
May 28 Python
Python3实现统计单词表中每个字母出现频率的方法示例
Jan 28 #Python
Python判断变量名是否合法的方法示例
Jan 28 #Python
Python使用while循环花式打印乘法表
Jan 28 #Python
Python实现程序判断季节的代码示例
Jan 28 #Python
Python后台管理员管理前台会员信息的讲解
Jan 28 #Python
Python之列表实现栈的工作功能
Jan 28 #Python
Python中常用的内置方法
Jan 28 #Python
You might like
PHP实现即时输出、实时输出内容方法
2015/05/27 PHP
PHP实现的网站目录扫描索引工具
2016/09/08 PHP
php实现微信公众号企业转账功能
2018/10/01 PHP
PHP implode()函数用法讲解
2019/03/08 PHP
PHP远程连接oracle数据库操作实现方法图文详解
2019/04/11 PHP
javascript window.opener的用法分析
2010/04/07 Javascript
js如何调用qq互联api实现第三方登录
2014/03/28 Javascript
JS去除iframe滚动条的方法
2015/04/01 Javascript
javascript转换静态图片,增加粒子动画效果
2015/05/28 Javascript
关于JS中的apply,call,bind的深入解析
2016/04/05 Javascript
js获取新浪天气接口的实现代码
2016/06/06 Javascript
Jquery组件easyUi实现手风琴(折叠面板)示例
2016/08/23 Javascript
基于vue.js实现侧边菜单栏
2017/03/20 Javascript
详解JS构造函数中this和return
2017/09/16 Javascript
node使用Koa2搭建web项目的方法
2017/10/17 Javascript
浅谈super-vuex使用体验
2018/06/25 Javascript
详解JS实现系统登录页的登录和验证
2019/04/29 Javascript
nodejs简单抓包工具使用详解
2019/08/23 NodeJs
在vue中使用防抖和节流,防止重复点击或重复上拉加载实例
2019/11/13 Javascript
element-ui 弹窗组件封装的步骤
2021/01/22 Javascript
[58:12]Ti4第二日主赛事败者组 LGD vs iG 3
2014/07/21 DOTA
ptyhon实现sitemap生成示例
2014/03/30 Python
python多线程编程中的join函数使用心得
2014/09/02 Python
详解Django框架中用户的登录和退出的实现
2015/07/23 Python
python实现读取excel写入mysql的小工具详解
2017/11/20 Python
Python新手学习标准库模块命名
2020/05/29 Python
英国休闲奢华的缩影:Crew Clothing
2019/05/05 全球购物
简述synchronized和java.util.concurrent.locks.Lock的异同
2014/12/08 面试题
《小小竹排画中游》教学反思
2014/02/26 职场文书
大学课外活动总结
2014/07/09 职场文书
党校学习心得体会范文
2014/09/09 职场文书
2014年幼儿园后勤工作总结
2014/11/10 职场文书
评先进个人材料
2014/12/29 职场文书
创先争优活动个人总结
2015/03/04 职场文书
2015年计算机教学工作总结
2015/07/22 职场文书
小公司融资,商业计划书的8切记
2019/07/15 职场文书