在大数据的今天,世界上任何一台单机都无法处理大数据,无论cpu的计算能力或者内存的容量。必须采用分布式来实现多台单机的资源整合,来进行任务的处理,包括离线的批处理和在线的实时处理。

鉴于上次开会讲了语言模型的发展,从规则到后来的NNLM。本章的目的就是锻炼动手能力,在知道原理的基础上,通过采用MR范式,自己实现一个ngram语言模型。

首先通过maven来管理相关包的依赖。

 <?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion> <groupId>com.dingheng</groupId>
<artifactId>nragmMR</artifactId>
<version>1.0-SNAPSHOT</version> <packaging>jar</packaging> <dependencies>
<dependency>
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-client</artifactId>
<version>2.7.2</version>
</dependency>
<dependency>
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-core</artifactId>
<version>1.2.1</version>
</dependency>
<dependency>
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-common</artifactId>
<version>2.7.2</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.12</version>
</dependency>
</dependencies>
</project>

然后直接上代码:

1.首先是driver,作为程序的启动文件。

  

 package com.dingheng;

 import java.io.IOException;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.NullWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.lib.db.DBConfiguration;
import org.apache.hadoop.mapreduce.lib.db.DBOutputFormat;
import org.apache.hadoop.mapreduce.lib.input.TextInputFormat;
import org.apache.hadoop.mapreduce.lib.output.TextOutputFormat; public class Driver { public static void main(String[] args) throws ClassNotFoundException, IOException, InterruptedException { // inputDir
// outputDir
// NumOfGram
// topK String inputDir = args[0];
String outputDir = args[1];
String numOfGram = args[2];
String threshold = args[3];
String topK = args[4]; // first mapreduce
Configuration configurationNGram = new Configuration();
configurationNGram.set("textinputformat.recode.delimiter", ".");
configurationNGram.set("numOfGram", numOfGram); Job jobNGram = Job.getInstance(configurationNGram);
jobNGram.setJobName("NGram");
jobNGram.setJarByClass(Driver.class); jobNGram.setMapperClass(NGram.NGramMapper.class);
jobNGram.setReducerClass(NGram.NGramReducer.class); jobNGram.setOutputKeyClass(Text.class);
jobNGram.setMapOutputValueClass(IntWritable.class); jobNGram.setInputFormatClass(TextInputFormat.class);
jobNGram.setOutputFormatClass(TextOutputFormat.class); TextInputFormat.addInputPath(jobNGram, new Path(inputDir));
TextOutputFormat.setOutputPath(jobNGram, new Path(outputDir));
jobNGram.waitForCompletion(true); // second mapreduce
Configuration configurationLanguage = new Configuration();
configurationLanguage.set("threshold", threshold);
configurationLanguage.set("topK", topK); DBConfiguration.configureDB(configurationLanguage,
"com.mysql.jdbc.Driver",
"jdbc:mysql://localhost:3306/test",
"root",
"123456"); Job jobLanguage = Job.getInstance(configurationLanguage);
jobLanguage.setJobName("LanguageModel");
jobLanguage.setJarByClass(Driver.class); jobLanguage.setMapperClass(LanguageModel.Map.class);
jobLanguage.setReducerClass(LanguageModel.Reduce.class); jobLanguage.setMapOutputKeyClass(Text.class);
jobLanguage.setMapOutputValueClass(Text.class);
jobLanguage.setOutputKeyClass(DBOutputWritable.class);
jobLanguage.setOutputValueClass(NullWritable.class); jobLanguage.setInputFormatClass(TextInputFormat.class);
jobLanguage.setOutputFormatClass(DBOutputFormat.class); DBOutputFormat.setOutput(
jobLanguage,
"output",
new String[] { "starting_phrase", "following_word", "count"}); TextInputFormat.setInputPaths(jobLanguage, new Path(args[1])); jobLanguage.waitForCompletion(true); }
}

Driver

2.然后是自己的定制类,自己定制了output

 

 package com.dingheng;

 import org.apache.hadoop.mapreduce.lib.db.DBWritable;

 import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException; public class DBOutputWritable implements DBWritable{ private String starting_phrase;
private String following_word;
private int count; public DBOutputWritable(String starting_phrase, String following_word, int count) {
this.starting_phrase = starting_phrase;
this.following_word = following_word;
this.count = count;
} public void write(PreparedStatement arg0) throws SQLException {
arg0.setString(1, starting_phrase);
arg0.setString(2, following_word);
arg0.setInt(3, count);
} public void readFields(ResultSet arg0) throws SQLException {
this.starting_phrase = arg0.getString(1);
this.following_word = arg0.getString(2);
this.count = arg0.getInt(3);
}
}

DBOutputWritable

3.之后自己的mapper和reducer。我试用了两个MR迭代,每一个迭代写在文件中

 package com.dingheng;

 import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.mapreduce.Reducer; import java.io.IOException; public class NGram { public static class NGramMapper extends Mapper<LongWritable, Text, Text, IntWritable> { int numOfGram; @Override
public void setup(Context context) {
Configuration conf = context.getConfiguration();
numOfGram = conf.getInt("numOfGram", 5);
} @Override
public void map(LongWritable key,
Text value,
Context context) throws IOException, InterruptedException {
/*
input: read sentence
I love data n=3
I love -> 1
love data -> 1
I love data -> 1
*/ String line = value.toString().trim().toLowerCase().replaceAll("[^a-z]", " ");
String[] words = line.split("\\s+"); if (words.length < 2) {
return;
} StringBuilder sb;
for (int i = 0; i < words.length; i++) {
sb = new StringBuilder();
sb.append(words[i]);
for (int j = 1; i + j < words.length && j < numOfGram; j++) {
sb.append(" ");
sb.append(words[i + j]);
context.write(new Text(sb.toString()), new IntWritable(1));
}
}
}
} public static class NGramReducer extends Reducer<Text, IntWritable, Text, IntWritable> { @Override
public void reduce(Text key,
Iterable<IntWritable> values,
Context context) throws IOException, InterruptedException {
int sum = 0;
for (IntWritable value: values) {
sum = sum + value.get();
}
context.write(key, new IntWritable(sum));
}
}
}

NGram

 package com.dingheng;

 import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.NullWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.mapreduce.Reducer; import java.io.IOException;
import java.util.*; public class LanguageModel { public static class Map extends Mapper<LongWritable, Text, Text, Text> { // input: I love big data\t10
// output: key: I love big value: data = 10 int threshold; @Override
protected void setup(Context context) throws IOException, InterruptedException {
Configuration configuration = context.getConfiguration();
threshold = configuration.getInt("threshold", 20);
} @Override
public void map(LongWritable key,
Text value,
Context context) throws IOException, InterruptedException { if ((value == null) || (value.toString().trim().length() == 0)) {
return;
} String line = value.toString().trim(); String[] wordsPlusCount = line.split("\t");
String[] words = wordsPlusCount[0].split("\\s+");
int count = Integer.valueOf(wordsPlusCount[wordsPlusCount.length - 1]); if (wordsPlusCount.length < 2 || count < threshold) {
return;
} StringBuilder sb = new StringBuilder();
for (int i = 0; i < words.length - 1; i++) {
sb.append(words[i]);
sb.append(" ");
} String outputKey = sb.toString().trim();
String outputValue = words[words.length - 1];
if (!(outputKey.length() < 1)) {
context.write(new Text(outputKey), new Text(outputValue + "=" + count));
}
}
} public static class Reduce extends Reducer<Text, Text, DBOutputWritable, NullWritable> { int topK; @Override
protected void setup(Context context) throws IOException, InterruptedException {
Configuration configuration = context.getConfiguration();
topK = configuration.getInt("topK", 5);
} @Override
public void reduce(Text key,
Iterable<Text> values,
Context context) throws IOException, InterruptedException {
// key: I love big
// value: <data = 10, girl = 100, boy = 1000 ...>
TreeMap<Integer, List<String>> tm = new TreeMap<Integer, List<String>>(Collections.<Integer>reverseOrder());
// <10, <data, baby...>>, <100, <girl>>, <1000, <boy>> for (Text val : values) {
// val: data = 10
String value = val.toString().trim();
String word = value.split("=")[0].trim();
int count = Integer.parseInt(value.split("=")[1].trim()); if (tm.containsKey(count)) {
tm.get(count).add(word);
} else {
List<String> list = new ArrayList<String>();
list.add(word);
tm.put(count, list);
}
} Iterator<Integer> iter = tm.keySet().iterator();
for (int j = 0; iter.hasNext() && j < topK; ) {
int keyCount = iter.next();
List<String> words = tm.get(keyCount);
for (String curWord: words) {
context.write(new DBOutputWritable(key.toString(), curWord, keyCount), NullWritable.get());
j++;
}
}
}
}
}

LanguageModel

基于MR实现ngram语言模型的更多相关文章

  1. NLP系列(5)_从朴素贝叶斯到N-gram语言模型

    作者: 龙心尘 && 寒小阳 时间:2016年2月. 出处: http://blog.csdn.net/longxinchen_ml/article/details/50646528 ...

  2. N-gram语言模型简单介绍

    N-gram语言模型 考虑一个语音识别系统,假设用户说了这么一句话:"I have a gun",因为发音的相似,该语音识别系统发现如下几句话都是可能的候选:1.I have a ...

  3. NLP中的用N-gram语言模型做英语完型填空的环境搭建

    本文是对xing_NLP中的用N-gram语言模型做完型填空这样一个NLP项目环境搭建的一个说明,本来想写在README.md中.第一次用github中的wiki,想想尝试一下也不错,然而格式非常的混 ...

  4. OCR技术浅探:基于深度学习和语言模型的印刷文字OCR系统

    作者: 苏剑林 系列博文: 科学空间 OCR技术浅探:1. 全文简述 OCR技术浅探:2. 背景与假设 OCR技术浅探:3. 特征提取(1) OCR技术浅探:3. 特征提取(2) OCR技术浅探:4. ...

  5. 通俗理解N-gram语言模型。(转)

    从NLP的最基础开始吧..不过自己看到这里,还没做总结,这里有一篇很不错的解析,可以分享一下. N-gram语言模型 考虑一个语音识别系统,假设用户说了这么一句话:“I have a gun”,因为发 ...

  6. N-gram语言模型与马尔科夫假设关系(转)

    1.从独立性假设到联合概率链朴素贝叶斯中使用的独立性假设为 P(x1,x2,x3,...,xn)=P(x1)P(x2)P(x3)...P(xn) 去掉独立性假设,有下面这个恒等式,即联合概率链规则 P ...

  7. 用CNTK搞深度学习 (二) 训练基于RNN的自然语言模型 ( language model )

    前一篇文章  用 CNTK 搞深度学习 (一) 入门    介绍了用CNTK构建简单前向神经网络的例子.现在假设读者已经懂得了使用CNTK的基本方法.现在我们做一个稍微复杂一点,也是自然语言挖掘中很火 ...

  8. 语言模型(N-Gram)

    问题描述:由于公司业务产品中,需要用户自己填写公司名称,而这个公司名称存在大量的乱填现象,因此需要对其做一些归一化的问题.在这基础上,能延伸出一个预测用户填写的公司名是否有效的模型出来. 目标:问题提 ...

  9. 基于N-Gram判断句子是否通顺

    完整代码实现及训练与测试数据:click me 一.任务描述         自然语言通顺与否的判定,即给定一个句子,要求判定所给的句子是否通顺. 二.问题探索与分析         拿到这个问题便开 ...

随机推荐

  1. cogs 182. [USACO Jan07] 均衡队形 线段树

    182. [USACO Jan07] 均衡队形 ★★☆   输入文件:lineup.in   输出文件:lineup.out   简单对比时间限制:4 s   内存限制:128 MB 题目描述 农夫约 ...

  2. .NET使用FastDBF写入DBF

    FastDBF源代码地址:https://github.com/SocialExplorer/FastDBF 第一步在解决方案中新建一个类库的项目:取名为SocialExplorer.FastDBF ...

  3. 定时器之Quart.net(2)

    第一步:Install-Package Quartz namespace ProjectEdb { class Program { static void Main(string[] args) { ...

  4. Maven 基础环境搭建 项目依赖jar包导入

    一.创建一个Maven工程 不清楚的话请查阅其它文档. 二.引入项目依赖的jar包 1.Spring 2.SpringMvc 3.Mybatis 4. 数据库连接池,驱动 5.其它(jstl.serv ...

  5. Asp.Net Core 已支持 gRPC-Web !!

    grpc-dotnet 项目在 PR #695 完成了 ASP.NET Core 服务与 .NET Core gRPC 客户端的 gRPC-Web 实现. 虽然目前还是实验性项目,但是并不阻碍我们为之 ...

  6. python property()函数:定义属性

    正常情况下,类包含的属性应该是隐藏的,只允许通过类提供的方法来间接的实现对类属性的访问和操作. class Person: #构造函数 def __init__(self, name): self.n ...

  7. [bzoj1005] [洛谷P2624] 明明的烦恼

    Description 自从明明学了树的结构,就对奇怪的树产生了兴趣-- 给出标号为1到N的点,以及某些点最终的度数,允许在任意两点间连线,可产生多少棵度数满足要求的树? Input 第一行为N(0 ...

  8. Chrome浏览器所有页面全部崩溃解决办法。

    今晚写代码的时候更新了一下Chrome,结果打开所有网页都提示 喔唷 崩溃了,而且找到c盘内没有bd0001.sys文件,电脑内也无任何百度系的软件,此解决办法pass. 折腾了半天从google中找 ...

  9. mysql数据库的备份与数据恢复

    一.定时备份数据库  前段时间工作中搭建了HttpRunnerManager的接口自动化测试平台,由于平台中没有提供用例下载的功能及权限管理功能,自己也不会写前端,于是就想了办法,那就是备份数据库,如 ...

  10. Python 判断小数的函数

    需求分析:1.小数点个数可以使用.count()方法2.按照小数点进行分割 例如: 1.98 [1,98]3.正小数:小数点左边是整数,右边也是整数 可以使用.isdigits()方法4.负小数:小数 ...