Hadoop的word co-occurrence实现
Posted Slaytanic
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Hadoop的word co-occurrence实现相关的知识,希望对你有一定的参考价值。
Word Co-occurrence一直不知道该怎么正确翻译, 单词相似度?还是共生单词?还是单词的共生矩阵?
这在统计里面是很常用的文本处理算法,用来度量一组文档集中所有出现频率最接近的词组.嗯,其实是上下文词组,不是单词.算是一个比较常用的算法,可以衍生出其他的统计算法.能用来做推荐,因为它能够提供的结果是"人们看了这个,也会看那个".比如做一些协同过滤之外的购物商品的推荐,信用卡的风险分析,或者是计算大家都喜欢什么东西.
比如 I love you , 出现 "I love" 的同时往往伴随着 "love you" 的出现,不过中文的处理跟英文不一样,需要先用分词库做预处理.
按照Mapper, Reducer和Driver的方式拆分代码
Mapper程序:
package wco; import java.io.IOException; import org.apache.hadoop.io.IntWritable; import org.apache.hadoop.io.LongWritable; import org.apache.hadoop.io.Text; import org.apache.hadoop.mapreduce.Mapper; public class WCoMapper extends Mapper<LongWritable, Text, Text, IntWritable> { @Override public void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException { /* * 将行内容全部转换为小写格式. */ String line_lc = value.toString().toLowerCase(); String before = null; /* * 将行拆分成单词 * 并且key是前一个单词加上后一个单词 * value 是 1 */ for (String word : line_lc.split("\\\\W+")) { //循环行内容,按照空格进行分割单词 if (word.length() > 0) { if (before != null) { //如果前词不为空,则写入上下文(第一次前词一定是空,直接跳到下面的before = word) context.write(new Text(before + "," + word), new IntWritable(1)); } before = word; //将现词赋值给前词 } } } }
Reducer程序:
package wco; import java.io.IOException; import org.apache.hadoop.io.IntWritable; import org.apache.hadoop.io.Text; import org.apache.hadoop.mapreduce.Reducer; public class WCoReducer extends Reducer<Text, IntWritable, Text, IntWritable> { @Override public void reduce(Text key, Iterable<IntWritable> values, Context context) throws IOException, InterruptedException { int wordCount = 0; for (IntWritable value : values) { wordCount += value.get(); //单纯计算word count } context.write(key, new IntWritable(wordCount)); } }
Driver程序就不解释了,天下的Driver都一样:
package wco; import org.apache.hadoop.fs.Path; import org.apache.hadoop.io.IntWritable; import org.apache.hadoop.io.Text; import org.apache.hadoop.mapreduce.lib.input.FileInputFormat; import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat; import org.apache.hadoop.mapreduce.Job; import org.apache.hadoop.conf.Configured; import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.util.Tool; import org.apache.hadoop.util.ToolRunner; public class WCo extends Configured implements Tool { @Override public int run(String[] args) throws Exception { if (args.length != 2) { System.out.printf("Usage: hadoop jar wco.WCo <input> <output>\\n"); return -1; } Job job = new Job(getConf()); job.setJarByClass(WCo.class); job.setJobName("Word Co Occurrence"); FileInputFormat.setInputPaths(job, new Path(args[0])); FileOutputFormat.setOutputPath(job, new Path(args[1])); job.setMapperClass(WCoMapper.class); job.setReducerClass(WCoReducer.class); job.setOutputKeyClass(Text.class); job.setOutputValueClass(IntWritable.class); boolean success = job.waitForCompletion(true); return success ? 0 : 1; } public static void main(String[] args) throws Exception { int exitCode = ToolRunner.run(new Configuration(), new WCo(), args); System.exit(exitCode); } }
算法的核心其实就是把前词和后词同时取出来作为key加上一个value做word count,统计单词的共生频率来对文本进行聚类.看网上说k-means的很多,其实很多时候算法是根据需求走的,k-means或者模糊k均值不一定就高大上,wordcount也不一定就穷矮矬.
以上是关于Hadoop的word co-occurrence实现的主要内容,如果未能解决你的问题,请参考以下文章
[0012] Hadoop 版hello word mapreduce wordcount 运行