使用Java+MapReduce实现热词发现
hot-words-detection-with-MapReduce
使用Java MapReduce实现热词发现
代码库Github链接为:https://github.com/Resulte/hot-words-detection-with-MapReduce (希望您去Github的时候可以顺手给个Star)
介绍
热词发现(hot words detection)是通过统计给定数据中的单词出现的频数,找到最常用的单词。
本项目使用Java + MapRedcue 计算框架,处理给定文本集,挖掘其中使用最频繁的 200 个词汇。
在大数据计算的过程中实现的操作有:
- 将所有大小字母都变成小写 字母;
- 过滤掉所有标点符号和常见的停用词;
- 过滤掉所有数字。
输入的数据为本目录下的text.txt文件。(暮光之城英文文本)
算法流程
第一步 词频统计
首先,构建一个常用词的哈希表,用于过滤文本中的所有常用停用词:
// 常见的停用词
final private static String[] stopList = new String[]{"very", "ourselves", "am", "doesn", "through", "me", "against", "up", "just", "her", "ours","couldn", "because", "is", "isn", "it", "only", "in", "such", "too", "mustn", "under", "their","if", "to", "my", "himself", "after", "why", "while", "can", "each", "itself", "his", "all", "once","herself", "more", "our", "they", "hasn", "on", "ma", "them", "its", "where", "did", "ll", "you","didn", "nor", "as", "now", "before", "those", "yours", "from", "who", "was", "m", "been", "will","into", "same", "how", "some", "of", "out", "with", "s", "being", "t", "mightn", "she", "again", "be","by", "shan", "have", "yourselves", "needn", "and", "are", "o", "these", "further", "most", "yourself","having", "aren", "here", "he", "were", "but", "this", "myself", "own", "we", "so", "i", "does", "both","when", "between", "d", "had", "the", "y", "has", "down", "off", "than", "haven", "whom", "wouldn","should", "ve", "over", "themselves", "few", "then", "hadn", "what", "until", "won", "no", "about","any", "that", "for", "shouldn", "don", "do", "there", "doing", "an", "or", "ain", "hers", "wasn","weren", "above", "a", "at", "your", "theirs", "below", "other", "not", "re", "him", "during", "which"};
// 停用词哈希表
private static Set<String> stopWordsSet;
// 构建停用词哈希表
stopWordsSet = new HashSet<>();
for (String stop : stopList) {stopWordsSet.add(stop);
}
然后写一个Job用于统计文本中所有的词频:
// step1:词频统计
Job job = Job.getInstance(conf, "word count");
该Job的Mapper在处理分词的时候可以将所有文本全部转换为小写字母,并过滤掉所有标点符号和数字等,同时根据单词是否在停用词哈希表中过滤停用词:
public static class TokenizerMapper extends Mapper<Object, Text, Text, IntWritable> {public void map(Object key, Text value, Context context) throws IOException, InterruptedException {Text word = new Text();IntWritable one = new IntWritable(1);String line = value.toString().toLowerCase(); // 全部转换为小写字母StringTokenizer itr = new StringTokenizer(line, " \t\n\f\"\r\\/.,:;?!@#$%^&*`~|<>()[]{}'+-=1234567890");while (itr.hasMoreTokens()) {word.set(itr.nextToken());// 根据单词是否在停用词哈希表中过滤停用词if (stopWordsSet.contains(word.toString())) {continue;}context.write(word, one);}}
}
该Job的Reducer用于统计词频:
public static class IntSumReducer extends Reducer<Text,IntWritable,Text,IntWritable> {public void reduce(Text key, Iterable<IntWritable> values, Context context) throws IOException, InterruptedException {int sum = 0;for (IntWritable val : values)sum += val.get();IntWritable result = new IntWritable();result.set(sum);context.write(key, result);}
}
最后将该Job的输出结果输出到一个临时的文件中:
Path tempDir = new Path("hotWordDect-temp-" + Integer.toString( new Random().nextInt(Integer.MAX_VALUE))); //用于输出词频统计的临时目录
FileOutputFormat.setOutputPath(job, tempDir); //设置输出文件路径
第二步 词频排序
写一个Job用于排序,并且这个Job的输入是上一步的输出:
// step2:词频排序
Job sortJob = Job.getInstance(conf, "sort");
FileInputFormat.addInputPath(sortJob, tempDir);
然后创建一个自定义的 Comparator 类对输出结果进行降序排序:
private static class IntWritableDecreasingComparator extends IntWritable.Comparator {public int compare(WritableComparable a, WritableComparable b) {return -super.compare(a, b);}public int compare(byte[] b1, int s1, int l1, byte[] b2, int s2, int l2) {return -super.compare(b1, s1, l1, b2, s2, l2);}
}
然后将key 和 value交换,使用这个自定义的 Comparator 类对输出结果中的 key (词频)进行降序排序
// key和value交换
sortJob.setMapperClass(InverseMapper.class);
// 使用这个自定义的 Comparator 类对输出结果中的 key (词频)进行降序排序
sortJob.setSortComparatorClass(IntWritableDecreasingComparator.class);
最后将该Job的输出结果也输出到另外一个临时的文件中:
Path tempDir2 = new Path("hotWordDect-temp2-" + Integer.toString(new Random().nextInt(Integer.MAX_VALUE))); //用于输出排序后的临时目录
FileOutputFormat.setOutputPath(sortJob, tempDir2);
sortJob.setOutputKeyClass(IntWritable.class);
sortJob.setOutputValueClass(Text.class);
第三步 选出前200个高频词汇
写一个Job选出前200个高频词汇,并且这个Job的输入是上一步的输出:
// step3:选出前200个高频词汇
Job topJob = Job.getInstance(conf, "topK");
FileInputFormat.addInputPath(topJob, tempDir2);
这个Job的Mapper需要筛选出前200个单词:
private static final int K = 200;public static class TopKMapper extends Mapper<Object, Text, IntWritable, Text>{ int count=0; public void map(Object key, Text value, Context context) throws IOException, InterruptedException { StringTokenizer itr = new StringTokenizer(value.toString()); IntWritable a = new IntWritable(Integer.parseInt(itr.nextToken())); Text b = new Text(itr.nextToken()); if(count < K){ context.write(a, b); count++; } }}
最后,安装词频降序排序后输出即可:
// 使用这个自定义的 Comparator 类对输出结果中的 key (词频)进行降序排序
topJob.setSortComparatorClass(IntWritableDecreasingComparator.class);
// 输出
FileOutputFormat.setOutputPath(topJob, new Path(otherArgs[1]));
topJob.setOutputKeyClass(IntWritable.class);
topJob.setOutputValueClass(Text.class);
实验结果
首先,将输入文本放入HDFS中:
./bin/hdfs dfs -put text.txt /input
然后,执行Jar包:
./bin/hadoop jar ./HotWordsDetection-1.0-SNAPSHOT.jar /input /output
详细输出结果请看当前目录下的output.txt
使用Java+MapReduce实现热词发现相关推荐
- 练习题︱基于今日头条开源数据的词共现、新热词发现、短语发现
最近笔者在做文本挖掘项目时候,写了一些小算法,不过写的比较重,没有进行效率优化,针对大数据集不是特别好用,不过在小数据集.不在意性能的情况下还是可以用用的. 本次练习题中可以实现的功能大致有三个: 短 ...
- 基于今日头条开源数据的词共现、新热词发现、短语发现
向AI转型的程序员都关注了这个号
- 热词抽取与话题发现系列(1):郝晓玲研究
1. 背景 社区内容的数据挖掘方面主要可分为两大类:内容关联挖掘和用户关系挖掘, 热词/热点话题发现属于社区内容挖掘范畴,是指从大量的UGC文本中检测出用户广泛讨论的话题.涉及两个关键性技术:中文分词 ...
- NLPChina_ansj_seg JAVA 实现热词及分词统计
前言: 笔者最近遇到一个需求:将文章输入后输出文章中的高频词,这是个简短的需求,但细分下便会出现许多细节重点.笔者细化需求后确定了这几个步骤:1. 文章分词(包括中英文混词)--> 2. 分词统 ...
- java 热词推荐搜索实现,Flink 热词统计(1): 基础功能实现
本系列文章将从热词统计需求讲起,讲解flink在实际项目中的应用.部署.容错.项目中的代码为scala所编写,没有用 java 的原因是scala语言看起来更加简练,同时与java语言的兼容性较好,可 ...
- Java 8备受宠爱,HarmonyOS冲刺全球第三大操作系统,全民热议元宇宙|2021十大技术热词
有没有觉得 2021 年过得特别快?如果你的回答是有,那么你的感觉可能是对的.因为在物理世界中,与上个世纪相比,地球自转的速度正在加快,这导致我们现在一天的时间比以前短了一些:在理想丰满现实骨感的现实 ...
- java热词_生成热词
根据CVPR论文生成热点词汇云图 用怕python 爬取论文到数据库中: 分析.查找关键词,对他排序: 生成热词汇云图: 一.python爬取数据 import requests import pym ...
- Java 8备受宠爱,HarmonyOS冲刺全球第三大操作系统,全民热议元宇宙|2021十大技术热词...
整理 | 苏宓,于轩 出品 | CSDN(ID:CSDNnews) 有没有觉得 2021 年过得特别快?如果你的回答是有,那么你的感觉可能是对的.因为在物理世界中,与上个世纪相比,地球自转的速度正在加 ...
- java 搜索热词插件_SpringBoot结合内嵌Redis实现热词搜索功能
需求说明 需要实现一个检索功能,需要查询到最近所有的所有热词,自定需求为所有一个月内检索数量最多的10个热词:这里使用Redis的内存数据库功能,其中Redis的ZSet格式提供的功能完全贴合该需求: ...
- java 热词推荐搜索实现,一个热词推荐的简单实现
原标题:一个热词推荐的简单实现 为什么想做这个东西 一直好奇像亚马逊这类网站的搜索是如何做到推荐的,最近刚好看到一篇文章< Redis 与搜索热词推荐 >,然而只写了思路.所以,就是想自己 ...
最新文章
- 神器!Pytorch结构化神经网络修剪工具包
- 一系列视频教程 收藏
- 使用DX 一些知识点整理(随时添加)
- Android学习笔记(七):多个Activity和Intent
- linux环境valgrind 安装
- mysql dba系统学习(20)mysql存储引擎MyISAM
- CodeForces - 353E Antichain(贪心+思维)
- java cssselector类_CSS 元素选择器
- 前端学习(1179):vue概述
- POJ-1062-昂贵的聘礼 (最短路)
- English Note_1_传统学习英语的误区
- Tomcat部分目录作用
- 数学建模与数学实验P49第四题解答
- 我国《个人信息保护法》立法背景与制度详解
- 彻底解决连上了网却不能上网问题:未连接到互联网
- EDI Capability 表示什么?
- linux 1000 ask(转)
- 安装ubuntu16.4.7系统
- 写了一个疫苗信息管理系统!(附源码)
- 用MATLAB进行大地测量学上的子午线弧长计算
热门文章
- 【机器学习】 - 决策树(西瓜数据集)
- 联想笔记本BIOS设置中文详解
- 阿铭Linux第二章笔记
- CSS加粗知识与案例
- Linux网络服务--DHCP原理与配置 理论+实验(DHCP的分配方式,工作原理详解,配置文件修改和客户端的使用方式DHCP中继的配置命令)
- Visual Studio 2019上安装AnkhSVN2019
- 解决css font-size设置小字体不生效
- Dorado7 DataGrid变颜色 自定义渲染
- java后台解析json并保存到数据库_java解析json格式文件,再保存在数据库怎么做?...
- EasyUI 中combobox利用拼音进行检索