Python 统计文章单词出现频率
2014-11-26 22:57
483 查看
近来学习Python,Python在科学计算中有着较强的优势。练习文章处理的初级代码,共享出来希望高手指点。
任务目标:统计英文文章中出现频率较高的单词,画出频率图并显示频率较高的单词。
基本要求已完成。应改变显示结果的条件,适应长短相差较大的文章。
可将单词频率统计的几行代码改成自己需要的功能,文章存储在article.txt中。
任务目标:统计英文文章中出现频率较高的单词,画出频率图并显示频率较高的单词。
基本要求已完成。应改变显示结果的条件,适应长短相差较大的文章。
import pylab import numpy import string def linetoword(line): for ch in line: if ch not in string.lowercase and ch not in string.uppercase and not ch == ' ': line.replace(ch,' ') wordlist = line.split(" ") newlist = [] for word in wordlist: if len(word)>3: word = word.lower() newlist.append(word) return newlist def readarticle(title): file = open(title,"r") wordlist = [] line = file.readline() while not line == "": wordlist.extend(linetoword(line)) line = file.readline() file.close() return wordlist wordlist = readarticle("article.txt") uniqueword = dict() for word in wordlist: if word in uniqueword.keys(): uniqueword[word] = uniqueword[word]+1 else: uniqueword[word] = 1 for key,val in uniqueword.items(): if val<5: uniqueword.pop(key) word = [word for word in uniqueword.keys()] count = [val for val in uniqueword.values()] width = 0.2 xval = numpy.arange(len(uniqueword)) pylab.xticks(xval+width/2.0,word,rotation=45) pylab.bar(xval,count,width = width,color = 'r') pylab.title("Frequency of an article") pylab.show()
可将单词频率统计的几行代码改成自己需要的功能,文章存储在article.txt中。
相关文章推荐
- [python]使用Counter统计文章中出现频率最高的单词
- python学习之文章中单词出现频率统计
- 黄聪:Python查看文章中每个单词的出现频率
- python统计文本字符串里单词出现频率的方法
- python实现统计文本中单词出现的频率
- 统计一篇文章中单词出现的频率
- Linux作业(三)-shell统计某文章中出现频率最高的N个单词并排序输出出现次数
- 统计一篇英文文章内每个单词出现频率,并返回出现频率最高的前10个单词及其出现次数
- python统计文本字符串里单词出现频率的方法
- python---在文本/文章中统计字符串中出现单词次数
- 统计文章中单词的字数并按照出现的频率排序(treeSet)
- 统计一篇英语文章每个单词出现的频率
- 统计分析文章中英文单词出现次数及频率(C++实现)
- 统计一篇文章中单词出现的频率(java 版)
- 【转】统计一篇文章中单词出现的频率(java 版)
- 单词统计_统计文章中每个单词出现的频率
- Linux作业(三)-shell统计某文章中出现频率最高的N个单词并排序输出出现次数
- python---字符串的拼接、去重、反转、字母花样排序、单词出现判断、统计文件特定单词频率lambda、硬盘容量、列表转字符串
- Hadoop:使用原生python编写MapReduce来统计文本文件中所有单词出现的频率功能
- python3 实现统计单词表中每个字母的出现频率