mapreduce中文分詞的寫法——wordcount

Word MapReduce HDFS 英語 有一種數據叫大數據 2017-03-28

英文的分詞,我們已經寫爛了,就是簡單的已空格為分隔符,然後按照wordcount的思路,很簡單的就能實現,然後,中文與英文的區別很大,一句話能產生很多的歧義,中文的分詞也不是簡單的已一個詞或者兩個詞作為分割,通常分詞需要一個分詞的庫,庫裡面記錄了常用的分詞斷詞還有每個單個的字,

mapreduce中文分詞的寫法——wordcount

詞庫

mapreduce中文分詞的寫法——wordcount

單個詞

喜歡就加群599601029一起學習吧!每天都有直播哦!

以上是ikanalyzer中文分詞包裡面的詞庫,文章裡面的每個詞都會掃描和詞庫匹配,根據正向最大匹配算法或者逆向最大匹配法來進行分詞。

Analyzer analyzer = new IKAnalyzer(true);

/*StringReader reader = new StringReader("樑蕭脫掉花曉霜外衣,撿起一根枯樹枝,將外衣覆在上面。"

+ "花曉霜恍然有悟,欲要喊叫,卻出不得聲,欲要阻攔,一根指頭也抬不起來。樑蕭深深看她一眼,蹲下身,笑道:"

+ "“乖乖地呆在這兒,穴道片刻就解啦!”忽見花曉霜臉上淚水縱橫滑落,也不覺眼眶酸熱,強笑道:“曉霜,你答應我"

+ "一件事好麼?”"

+" 花曉霜的淚水早已迷糊了雙眼,幾乎看不清樑蕭的形影,只是心中明白,此地一別,或許便成永訣,一時間,真恨不得死了才好。"

+ "隱約間,只聽樑蕭在自己耳邊低聲道:“不論如何,你都要好好愛惜身子,將來有空閒,我還來天機宮看你。”花曉霜每聽到一個字,"

+ "心都被撕裂一分,那般痛苦生平未有。只聽樑蕭又吃吃笑道:“不信麼,來。”說著伸出小指,與花曉霜小指拉鉤:“金鉤銀鉤,說話不"

+ "算是小狗。”花曉霜聽到此處,早已淚落如雨,但胸中q枉自百轉千回,卻吐不出一個字來。");*/

StringReader reader = new StringReader("我是非自有公論文");

// 分詞

TokenStream ts = analyzer.tokenStream("", reader);

ts.reset();

CharTermAttribute term = ts.getAttribute(CharTermAttribute.class);

// 遍歷分詞數據

while(ts.incrementToken()){

System.out.println(term.toString());

}

analyzer.close();

以上是一段小說的內容,經過分詞,得到

mapreduce中文分詞的寫法——wordcount

分詞結果

然而一些詞,比如女主人公的名字,花曉霜,這在小說裡出現的次數很多,但是,分詞並沒有把它作為一個詞,比較智能的算法可以根據上下文的語義中幾個字出現的次數來智能的進行詞庫的改進。

public class WordAnalyzer extends Configured implements Tool{

public static class WordAnalyzerMapper extends Mapper<LongWritable, Text, Text, IntWritable>{

private Text word = new Text();

private final static IntWritable one = new IntWritable(1);

@Override

protected void map(LongWritable key, Text value, Context context)

throws IOException, InterruptedException {

// 創建分詞對象

Analyzer analyzer = new IKAnalyzer(true);

StringReader reader = new StringReader(value.toString());

// 分詞

TokenStream ts = analyzer.tokenStream("", reader);

ts.reset();

CharTermAttribute term = ts.getAttribute(CharTermAttribute.class);

// 遍歷分詞數據

while(ts.incrementToken()){

word.set(term.toString());

context.write(word, one);

}

reader.close();

}

}

喜歡就加群599601029一起學習吧!每天都有直播哦!

public static class WordAnalyzerReducer extends Reducer<Text, IntWritable, Text, LongWritable>{

@Override

protected void reduce(Text key, Iterable<IntWritable> values,

Context context) throws IOException, InterruptedException {

int sum=0;

for (IntWritable intWritable : values) {

sum+=intWritable.get();

}

context.write(key, new LongWritable(sum));

}

}

@Override

public int run(String[] args) throws Exception {

Configuration conf=this.getConf();

Job job=Job.getInstance(conf, this.getClass()

.getSimpleName());

job.setJarByClass(this.getClass());

Path inpath=new Path(args[0]);

FileInputFormat.addInputPath(job, inpath);

Path outpath=new Path(args[1]);

FileOutputFormat.setOutputPath(job, outpath);

job.setMapperClass(WordAnalyzerMapper.class);

job.setMapOutputKeyClass(Text.class);

job.setMapOutputValueClass(IntWritable.class);

job.setReducerClass(WordAnalyzerReducer.class);

job.setOutputKeyClass(Text.class);

job.setOutputValueClass(LongWritable.class);

boolean ok=job.waitForCompletion(true);

return ok ? 1 :0;

}

喜歡就加群599601029一起學習吧!每天都有直播哦!

public static void main(String[] args) throws Exception {

Configuration conf=new Configuration();

args=new String[]{

"hdfs://java02.ibeifeng.com:8020/user/beifeng/xiaoshuo.txt",

"hdfs://java02.ibeifeng.com:8020/user/beifeng/xiaoshuooutput1"

};

int status =ToolRunner.run(conf, new WordAnalyzer(), args);

System.exit(status);

}

}

相關推薦

推薦中...