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中使用Neo4j数据库的教程
Apr 16 Python
python 调用c语言函数的方法
Sep 29 Python
Python使用requests发送POST请求实例代码
Jan 25 Python
Python3多进程 multiprocessing 模块实例详解
Jun 11 Python
Python面向对象类的继承实例详解
Jun 27 Python
Python使用Selenium爬取淘宝异步加载的数据方法
Dec 17 Python
Python实现简单查找最长子串功能示例
Feb 26 Python
python中时间、日期、时间戳的转换的实现方法
Jul 06 Python
Python对接支付宝支付自实现功能
Oct 10 Python
Python print不能立即打印的解决方式
Feb 19 Python
python用pip install时安装失败的一系列问题及解决方法
Feb 24 Python
pytorch查看网络参数显存占用量等操作
May 12 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 获取完整url地址
2008/12/20 PHP
PHP cdata 处理(详细介绍)
2013/07/05 PHP
PHP curl实现抓取302跳转后页面的示例
2014/07/04 PHP
PHP5全版本绕过open_basedir读文件脚本漏洞详细介绍
2015/01/20 PHP
php实现通过ftp上传文件
2015/06/19 PHP
Laravel 5.1 on SAE环境开发教程【附项目demo源码】
2016/10/09 PHP
PHP网站常见安全漏洞,及相应防范措施总结
2021/03/01 PHP
csdn 博客中实现运行代码功能实现
2009/08/29 Javascript
JavaScript Event学习第六章 事件的访问
2010/02/07 Javascript
jquery 插件开发 extjs中的extend用法小结
2013/01/04 Javascript
关于全局变量和局部变量的那些事
2013/01/11 Javascript
js借助ActiveXObject实现创建文件
2013/09/29 Javascript
js鼠标滑轮滚动事件绑定的简单实例(兼容主流浏览器)
2014/01/14 Javascript
jquery获取当前日期的方法
2015/01/14 Javascript
JavaScript按值删除数组元素的方法
2015/04/24 Javascript
jquery获取下拉框中的循环值
2017/02/08 Javascript
JavaScript队列的应用实例详解【经典数据结构】
2017/04/12 Javascript
node文件上传功能简易实现代码
2017/06/16 Javascript
Vue.use源码学习小结
2018/06/20 Javascript
详解JavaScript实现动态的轮播图效果
2019/04/29 Javascript
用Python编程实现语音控制电脑
2014/04/01 Python
Python获取运行目录与当前脚本目录的方法
2015/06/01 Python
Python设计模式之抽象工厂模式
2016/08/25 Python
Python 字符串与数字输出方法
2018/07/16 Python
Python随机生成身份证号码及校验功能
2018/12/04 Python
详解用Python练习画个美队盾牌
2019/03/23 Python
纽约现代艺术博物馆商店:MoMA STORE(室内家具和杂货商品)
2016/08/02 全球购物
全球知名旅游社区法国站点:TripAdvisor法国
2016/08/03 全球购物
倩碧香港官方网站:Clinique香港
2017/11/13 全球购物
酒店管理专业毕业生求职自荐信
2014/04/28 职场文书
小学生环保倡议书
2014/05/15 职场文书
离职告别感言
2015/08/04 职场文书
运动会广播稿100字
2015/08/19 职场文书
《三国志》赏析
2019/08/27 职场文书
导游词之包公祠
2019/11/25 职场文书
MySQL常用慢查询分析工具详解
2022/08/14 MySQL