问题描述:现在有n个文本文件,使用MapReduce的方法实现词频统计。

附上统计词频的关键代码,首先是一个通用的MapReduce模块:

class MapReduce:
__doc__ = '''提供map_reduce功能''' @staticmethod
def map_reduce(i, mapper, reducer):
"""
map_reduce方法
:param i: 需要MapReduce的集合
:param mapper: 自定义mapper方法
:param reducer: 自定义reducer方法
:return: 以自定义reducer方法的返回值为元素的一个列表
"""
intermediate = [] # 存放所有的(intermediate_key, intermediate_value)
for (key, value) in i.items():
intermediate.extend(mapper(key, value)) # sorted返回一个排序好的list,因为list中的元素是一个个的tuple,key设定按照tuple中第几个元素排序
# groupby把迭代器中相邻的重复元素挑出来放在一起,key设定按照tuple中第几个元素为关键字来挑选重复元素
# 下面的循环中groupby返回的key是intermediate_key,而group是个list,是1个或多个
# 有着相同intermediate_key的(intermediate_key, intermediate_value)
groups = {}
for key, group in itertools.groupby(sorted(intermediate, key=lambda im: im[0]), key=lambda x: x[0]):
groups[key] = [y for x, y in group]
# groups是一个字典,其key为上面说到的intermediate_key,value为所有对应intermediate_key的intermediate_value
# 组成的一个列表
return [reducer(intermediate_key, groups[intermediate_key]) for intermediate_key in groups]

然后需要针对词频统计这个实际问题写好自己的mapper方法和reducer方法:

class WordCount:
__doc__ = '''词频统计''' def mapper(self, input_key, input_value):
"""
词频统计的mapper方法
:param input_key: 文件名
:param input_value: 文本内容
:return: 以(词,1)为元素的一个列表
"""
return [(word, 1) for word in
self.remove_punctuation(input_value.lower()).split()] def reducer(self, intermediate_key, intermediate_value_list):
"""
词频统计的reducer方法
:param intermediate_key: 某个词
:param intermediate_value_list: 出现记录列表,如[1,1,1]
:return: (词,词频)
"""
return intermediate_key, sum(intermediate_value_list) @staticmethod
def remove_punctuation(text):
"""
去掉字符串中的标点符号
:param text: 文本
:return: 去掉标点的文本
"""
return re.sub(u"\p{P}+", "", text)

用3个文本文件进行测试:

text\a.tex:

  The quick brown fox jumped over the lazy grey dogs.

text\b.txt:

  That's one small step for a man, one giant leap for mankind.

text\c.txt:

  Mary had a little lamb,

  Its fleece was white as snow;

  And everywhere that Mary went,

  The lamb was sure to go.

调用如下:

    filenames = ["text\\a.txt", "text\\b.txt", "text\\c.txt"]
i = {}
for filename in filenames:
f = open(filename)
i[filename] = f.read()
f.close() wc = WordCount()
print(MapReduce.map_reduce(i, wc.mapper, wc.reducer))

输出结果:

[('white', 1), ('little', 1), ('sure', 1), ('snow;', 1), ('went,', 1), ('as', 1), ('lamb,', 1), ('go.', 1), ('lamb', 1), ('its', 1), ('a', 1), ('was', 2), ('to', 1), ('fleece', 1), ('that', 1), ('the', 1), ('mary', 2), ('everywhere', 1), ('had', 1), ('and', 1)]

上面提出的方法只使用了最基本的MapReduce思想,所以不支持大数据量的测试,毕竟各种调度之类的内容没有考虑到。


参考资料

1:Write your first MapReduce program in 20 minutes

MapReduce实现词频统计的更多相关文章

  1. MapReduce词频统计

    自定义Mapper实现 import org.apache.hadoop.io.IntWritable; import org.apache.hadoop.io.LongWritable; impor ...

  2. Hadoop上的中文分词与词频统计实践 (有待学习 http://www.cnblogs.com/jiejue/archive/2012/12/16/2820788.html)

    解决问题的方案 Hadoop上的中文分词与词频统计实践 首先来推荐相关材料:http://xiaoxia.org/2011/12/18/map-reduce-program-of-rmm-word-c ...

  3. 【原创】大数据基础之词频统计Word Count

    对文件进行词频统计,是一个大数据领域的hello word级别的应用,来看下实现有多简单: 1 Linux单机处理 egrep -o "\b[[:alpha:]]+\b" test ...

  4. Hive简单编程实践-词频统计

    一.使用MapReduce的方式进行词频统计 (1)在HDFS用户目录下创建input文件夹 hdfs dfs -mkdir input 注意:林子雨老师的博客(http://dblab.xmu.ed ...

  5. hive进行词频统计

    统计文件信息: $ /opt/cdh-5.3.6/hadoop-2.5.0/bin/hdfs dfs -text /user/hadoop/wordcount/input/wc.input hadoo ...

  6. Hadoop的改进实验(中文分词词频统计及英文词频统计)(4/4)

    声明: 1)本文由我bitpeach原创撰写,转载时请注明出处,侵权必究. 2)本小实验工作环境为Windows系统下的百度云(联网),和Ubuntu系统的hadoop1-2-1(自己提前配好).如不 ...

  7. 初学Hadoop之中文词频统计

    1.安装eclipse 准备 eclipse-dsl-luna-SR2-linux-gtk-x86_64.tar.gz 安装 1.解压文件. 2.创建图标. ln -s /opt/eclipse/ec ...

  8. 初学Hadoop之WordCount词频统计

    1.WordCount源码 将源码文件WordCount.java放到Hadoop2.6.0文件夹中. import java.io.IOException; import java.util.Str ...

  9. Hadoop之词频统计小实验

    声明:    1)本文由我原创撰写,转载时请注明出处,侵权必究. 2)本小实验工作环境为Ubuntu操作系统,hadoop1-2-1,jdk1.8.0. 3)统计词频工作在单节点的伪分布上,至于真正实 ...

随机推荐

  1. nagios 配置 check_traffic 流量监控模块(被监控端)

    安装软件包yum -y install net-snmp*chkconfig nrpe onchkconfig snmpd on 使用SCP命令拷贝 check_traffic.sh 到 / usr/ ...

  2. 添加 [DataContract] 到 Entity Framework 6.0 POCO Template

    1. 添加using System.Runtime.Serialization; 找到这行 includeCollections ? (Environment.NewLine + "usin ...

  3. django从零开始-模型

    1.设置统计表 配置models.py from django.db import models # Create your models here. # 发布会 class Event(models ...

  4. sql语句循环截取字符串

    测试环境 : mssql2016 express 需求 : 拆分字符串执行insert 思路 : 在循环中截取分隔符之间的字符串.起止点位置计算    起点从0开始startIndex,查找第一个分隔 ...

  5. One Person Game ZOJ - 3329(期望dp, 数学)

    There is a very simple and interesting one-person game. You have 3 dice, namely Die1, Die2 and Die3. ...

  6. Spring 整合 Hibernate 时启用二级缓存实例详解

    写在前面: 1. 本例使用 Hibernate3 + Spring3: 2. 本例的查询使用了 HibernateTemplate: 1. 导入 ehcache-x.x.x.jar 包: 2. 在 a ...

  7. 2019.4.1考试&2019.4.2考试&2019.4.4考试

    4.1:T1原题,T2码农板子题,T3板子题 4.2 好像是三个出题人分别出的 以及#define *** 傻逼 T1 思维好题 转成树形DP,$dp[i][j]$表示点i值为j的方案数,记录前缀和转 ...

  8. bzoj4566 找相同字符

    题意:给定两个字符串,从中各取一个子串使之相同,有多少种取法.允许本质相同. 解:建立广义后缀自动机,对于每个串,分别统计cnt,之后每个点的cnt乘起来.记得开long long #include ...

  9. 【洛谷P3917】异或序列

    题目大意:给定一个长度为 N 的序列,每个位置有一个权值,求 \[\sum\limits_{1\le i\le j\le n}(a_i\oplus a_{i+1}...\oplus a_j)\] 的值 ...

  10. Django 异步化库celery和定时任务

    首先要了解Django其实是个同步框架,那么多个用户发送请求时就会发生排队的情况上一个用户的请求完成后在进行下一个,这样会对影响用户体验,所有就要用到异步方法来解决. 首先我们要安装celery库 p ...