问题描述:现在有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. 【LOJ#6029】市场(线段树)

    [LOJ#6029]市场(线段树) 题面 LOJ 题解 看着就是一个需要势能分析的线段树. 不难发现就是把第二个整除操作化为减法. 考虑一下什么时候整除操作才能变成减法. 假设两个数为\(a,b\). ...

  2. 文艺平衡树 Splay 学习笔记(1)

    (这里是Splay基础操作,reserve什么的会在下一篇里面讲) 好久之前就说要学Splay了,结果苟到现在才学习. 可能是最近良心发现自己实在太弱了,听数学又听不懂只好多学点不要脑子的数据结构. ...

  3. Centos 7下下载和安装docker

    sudo yum install -y device-mapper sudo modprobe dm_mod ls -l /sys/class/misc/device-mapper sudo rpm ...

  4. string的基本用法

    #include <iostream> #include<string> #include<vector> #include<algorithm> us ...

  5. Spring 声明式事务

    事务的特性/概念 事务:一组操作要么都成功要么失败: 事务的四个关键属性(ACID): 原子性(atomicity):“原子”的本意是“不可再分”,事务的原子性表现为一个事务中涉及到的多个操作在逻辑上 ...

  6. docker cmd list

    436 wget -qO- https://get.docker.com/ | sh 437 sudo apt-get update 438 sudo apt-get install -y docke ...

  7. CSS解决字母不换行

    通过百度,查找解决方案 1. word-break:break-all;只对英文起作用,以字母作为换行依据2. word-wrap:break-word; 只对英文起作用,以单词作为换行依据

  8. C# winfrom容器布局与工具栏&&右键菜单栏&&隐藏显示小图标的的简单事件

    前两天的时候学习了winfrom,简单地说就是各种布局,然后给按钮,textbox等各种控件添加各种事件的操作,经过前天一晚上,昨天一天的练习操作的还算熟练,但是对构造函数传值还是不是很了解,由于各种 ...

  9. 浅谈 WebDriver如何应对不同浏览器

    selenium2 基于对象的测试,在selenium2中一共支持以下浏览器: Firefox(FirefoxDriver) IE(InternetExplorerDriver) Chrome(Chr ...

  10. c++ stl sort

    两者相等时,必须为false. 满足拟序. 群里大佬666.