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实现忽略大小写对字符串列表排序的方法
Sep 25 Python
Python模拟登录12306的方法
Dec 30 Python
python实现神经网络感知器算法
Dec 20 Python
Python测试模块doctest使用解析
Aug 10 Python
线程安全及Python中的GIL原理分析
Oct 29 Python
python3+selenium获取页面加载的所有静态资源文件链接操作
May 04 Python
使用keras实现densenet和Xception的模型融合
May 23 Python
详细分析Python可变对象和不可变对象
Jul 09 Python
python字典按照value排序方法
Dec 28 Python
python 数据类型强制转换的总结
Jan 25 Python
requests在python中发送请求的实例讲解
Feb 17 Python
Python绘制词云图之可视化神器pyecharts的方法
Feb 23 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修改Linux或Unix口令的方法分享
2012/01/30 PHP
php实现读取手机客户端浏览器的类
2015/01/09 PHP
php基于GD库画五星红旗的方法
2015/02/24 PHP
php轻松实现文件上传功能
2016/03/03 PHP
PHP微信开发之文本自动回复
2016/06/23 PHP
php使用include 和require引入文件的区别
2017/02/16 PHP
PHP获取redis里不存在的6位随机数应用示例【设置24小时过时】
2017/06/07 PHP
PHP对象的浅复制与深复制的实例详解
2017/10/26 PHP
phpwind放自动注册方法
2006/12/02 Javascript
JavaScript 学习笔记之语句
2015/01/14 Javascript
C#中使用迭代器处理等待任务
2015/07/13 Javascript
jQuery简单实现验证邮箱格式
2015/07/15 Javascript
jQuery使用animate创建动画用法实例
2015/08/07 Javascript
jquery插件方式实现table查询功能的简单实例
2016/06/06 Javascript
详解获取jq ul第一个li定位的四种解决方案
2016/11/23 Javascript
基于BootStrap multiselect.js实现的下拉框联动效果
2017/07/28 Javascript
使用svg实现动态时钟效果
2018/07/17 Javascript
JS复杂判断的更优雅写法代码详解
2018/11/07 Javascript
Vue可自定义tab组件用法实例
2019/10/24 Javascript
JavaScript ECMA-262-3 深入解析(一):执行上下文实例分析
2020/04/25 Javascript
在Webpack中用url-loader处理图片和字体的问题
2020/04/28 Javascript
微信小程序清空输入框信息与实现屏幕往上滚动的示例代码
2020/06/23 Javascript
Webpack5正式发布,有哪些新特性
2020/10/12 Javascript
Django实现网页分页功能
2019/10/31 Python
python实现while循环打印星星的四种形状
2019/11/23 Python
Python文本处理简单易懂方法解析
2019/12/19 Python
win10下python3.8的PIL库安装过程
2020/06/08 Python
教你使用Sublime text3搭建Python开发环境及常用插件安装另分享Sublime text3最新激活注册码
2020/11/12 Python
问卷调查计划书
2014/01/10 职场文书
办公室文员工作职责
2014/01/31 职场文书
园艺师求职信
2014/03/10 职场文书
个人合作协议书范本
2014/04/18 职场文书
大学同学会活动方案
2014/08/20 职场文书
2015年推广普通话演讲稿
2015/03/20 职场文书
学校社团活动总结
2015/05/07 职场文书
团支部组织委员竞选稿
2015/11/21 职场文书