python英文文本词频统计_python编程:英文小说词频统计

一、编程目的:

1、英文小说(The Myths )分词,并进行词频统计

2、绘制词频统计直方图

3、为小说情感分析做准备

二、编程环境:

1、pyhon3+pycharm

2、需安装模块:string、matplotlib.pyplot

三、编程效果:v2-219d8094e33cd8c058567fb95501ba7a_720w.jpg效果图二

四、编程代码:

#!/usr/bin/env python

# -*- coding: utf-8 -*-

#1导入模块

import string #导入字符串模块

import matplotlib.pyplot as plt #导入matplotlib模块,取别名plt

#from matplotlib import pyplot as plt #导入matplotlib模块,取别名plt

#2读取文件,并分词

hist = {} #创建一个空字典,放词频与单词,无序排列

data = [] #创建一个空列表,放词频与单词,有序:从多到少

f = open('The Myths(神话).txt','r') #打开文件

content = f.read() #读取文件

f.close() #关闭文件

content = content.replace('-',' ') #连字符—用空格代替

words = content.split() #字符串按空格分割--分词

#迭代处理:将字典变列表,存入数据

for i in range(len(words)):

words[i] = words[i].strip(string.punctuation) #去掉标点符号,去掉首尾

words[i] = words[i].lower() #统一大小写

if words[i] in hist: #统计词频与单词

hist[words[i]] = hist[words[i]] + 1 #不是第一次

else:

hist[words[i]] = 1 #第一次

#print(hist) #打印字典(词频与单词,无序)

#遍历字典

for key, value in hist.items(): #遍历字典

temp = [value,key] #变量,变量值

data.append(temp) #添加数据

data.sort(reverse=True) #排序

#print(data) #打印列表(词频与单词,有序,从多到少)

#绘制直方图(词频TOP1-10)

plt.rcParams['font.sans-serif']=['SimHei'] #直方图正常显示中文字体

for i in range(0,10):

plt.bar((data[i][1],),(data[i][0],))

plt.title('小说"The Myths(神话)"词频(TOP1-10)') #显示标题

plt.xlabel('单词') # 显示x轴名称

plt.ylabel('词频') # 显示y轴名称

plt.legend('词频直方图') #显示图例

plt.show() #显示作图结果

#绘制直方图(词频TOP11-20)

for i in range(10,20):

plt.bar((data[i][1],),(data[i][0],))

plt.legend('直方图')

plt.xlabel('单词')

plt.ylabel('词频')

plt.title('小说"The Myths(神话)"词频(Top11-20)')

plt.show()复制代码

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,以下是一个简单的 Hadoop WordCount 词频统计程序示例: Mapper 类: ```java import java.io.IOException; import org.apache.hadoop.io.LongWritable; import org.apache.hadoop.io.Text; import org.apache.hadoop.mapreduce.Mapper; public class WordCountMapper extends Mapper<LongWritable, Text, Text, LongWritable> { @Override public void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException { String line = value.toString(); // 将每一行按空格分隔成多个单词 String[] words = line.split(" "); // 遍历每个单词,发送到 Reducer 进行统计 for (String word : words) { context.write(new Text(word), new LongWritable(1)); } } } ``` Reducer 类: ```java import java.io.IOException; import org.apache.hadoop.io.LongWritable; import org.apache.hadoop.io.Text; import org.apache.hadoop.mapreduce.Reducer; public class WordCountReducer extends Reducer<Text, LongWritable, Text, LongWritable> { @Override public void reduce(Text key, Iterable<LongWritable> values, Context context) throws IOException, InterruptedException { long sum = 0; // 遍历同一个单词的所有出现次数,求和 for (LongWritable value : values) { sum += value.get(); } // 将单词出现次数发送到输出 context.write(key, new LongWritable(sum)); } } ``` Main 方法: ```java import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.fs.Path; import org.apache.hadoop.io.LongWritable; import org.apache.hadoop.io.Text; import org.apache.hadoop.mapreduce.Job; import org.apache.hadoop.mapreduce.lib.input.FileInputFormat; import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat; public class WordCount { public static void main(String[] args) throws Exception { Configuration conf = new Configuration(); Job job = Job.getInstance(conf, "word count"); job.setJarByClass(WordCount.class); job.setMapperClass(WordCountMapper.class); job.setCombinerClass(WordCountReducer.class); job.setReducerClass(WordCountReducer.class); job.setOutputKeyClass(Text.class); job.setOutputValueClass(LongWritable.class); FileInputFormat.addInputPath(job, new Path(args[0])); FileOutputFormat.setOutputPath(job, new Path(args[1])); System.exit(job.waitForCompletion(true) ? 0 : 1); } } ``` 在执行该程序时,需要在命令行中输入以下命令: ```bash hadoop jar WordCount.jar WordCount /input /output ``` 其中,第一个参数 WordCount.jar 是打包好的程序文件,第二个参数 WordCount 表示程序的类名,后面的 /input 和 /output 分别是输入和输出的路径。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值