当前位置:   article > 正文

hadoop学习【7】——基于hadoop的分布式分词程序_hadoop的分词使用

hadoop的分词使用

一、使用的分词包——庖丁分词器介绍

1.1、简介:

庖丁系统是个完全基于lucene中文分词系统,它就是重新建了一个analyzer,叫做PaodingAnalyzer,这个analyer的核心任务就是生成一个可以切词TokenStream。

1.2、优点:

这里之所以使用庖丁主要考虑到庖丁的分词效率比其他的分词器要高

1.3、缺点:

其分词有一个缺点

例如下面一段文字:

“发展社区老年活动场所和服务设施”
如果想搜索日本的和服相关资料,输入关键字“和服”的时候,上面的资料也会被搜索出来
搜索引擎是第一步搜索:
在浩瀚的信息中,快速集结最后可能是所想要的结果, 按照可能是最好的顺序展现出来。
人的眼睛是第二步搜索:
找寻最符合要求的结果,同时将机器无法轻易识别的少数“无效”结果过滤
“和服”问题,涉及了汉语语义的问题,几乎不可完全解决(可作为“特例”解决,或通过排序方法,将他排到相对靠后等价解决)。

但是这同时也是庖丁分词的一个有点,因为对于一般的分词而言要确定出到底是怎样的一种组合也只是选择概率较大的,将可能的组合全部给出给了我们选择的机会。

——摘自百度百科


这里我把我用的版本和词典文件共享到网盘欢迎下载:http://pan.baidu.com/s/1eQekI9k

二、分词程序单机测试:

2.1、导入相应的分词包、词典

需要的包:

字典:将整个dic文件拷贝到src目录下

2.2、java分词程序:(注意:这里并没有去除噪音和停用词,只是简单的测试庖丁分词的可用性)

  1. public class TestPaoding {
  2. public static void main(String[] args) {
  3. String line = "亚洲:中华人民共和国";
  4. PaodingAnalyzer analyzer = new PaodingAnalyzer();
  5. StringReader sr = new StringReader(line);
  6. TokenStream ts = analyzer.tokenStream("", sr);
  7. try{
  8. while(ts.incrementToken()){
  9. CharTermAttribute ta = ts.getAttribute(CharTermAttribute.class);
  10. System.out.println(ta.toString());
  11. }
  12. }catch(Exception e){
  13. }
  14. }
  15. }

2.3、单机程序测试结果:


三、单机程序mapreduce化:

3.1、开发环境:

伪分布式系统

hadoop1.1.2平台

开发环境eclipse

需要的包跟上面写的一样,这里就不赘余了

3.2、mapreduce程序:

mapreduce计算框架浅析:

这里因为只是进行简单的分词,没有必要多个mapper,也不需要reducer。

这里的inputformat没有加控制,如果文件很多时,就一定要对inputformat进行控制,控制分片的大小,这样才能保证mapreduce的计算效率最高

  1. package test;
  2. import java.io.IOException;
  3. import java.io.StringReader;
  4. import net.paoding.analysis.analyzer.PaodingAnalyzer;
  5. import org.apache.hadoop.conf.Configuration;
  6. import org.apache.hadoop.conf.Configured;
  7. import org.apache.hadoop.fs.Path;
  8. import org.apache.hadoop.io.LongWritable;
  9. import org.apache.hadoop.io.Text;
  10. import org.apache.hadoop.mapreduce.Job;
  11. import org.apache.hadoop.mapreduce.Mapper;
  12. import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
  13. import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
  14. import org.apache.hadoop.util.Tool;
  15. import org.apache.hadoop.util.ToolRunner;
  16. import org.apache.lucene.analysis.tokenattributes.CharTermAttribute;
  17. public class TestTOkenizer extends Configured implements Tool {
  18. public static class Map extends Mapper<LongWritable,Text,Text,Text>{
  19. public static void main(String[] args)throws Exception{
  20. int res = ToolRunner.run(new Configuration(), new TestTOkenizer(), args);
  21. System.exit(res);
  22. }
  23. //计数器,当在跑数据的时候出错,计数器就起作用了,能统计出多少数据是成功完成了的
  24. enum Counter
  25. {
  26. LINESKIP,
  27. }
  28. //Map程序
  29. public void map(LongWritable key,Text value,Context context) throws IOException, InterruptedException{
  30. //获取单行文本内容
  31. //1 这里是长江大学石油软件实验班,我是张三
  32. //这里为了模仿微博分词过程中前面可能都有一个ID号,所以在输入的要进行分词的String中也加入了序号
  33. String line = value.toString();
  34. try
  35. {
  36. //根据空格进行字符串拆分
  37. String[] lineSplit = line.split("[\\s]+");
  38. //将第一个序号放在anum变量中
  39. String anum = lineSplit[0];
  40. //将文本内容放到bnum变量中
  41. String bnum = lineSplit[1];
  42. //初始化庖丁分词器
  43. PaodingAnalyzer analyzer = new PaodingAnalyzer();
  44. //根据文本内容构造StringReader
  45. StringReader sr = new StringReader(bnum);
  46. //用来整合分好的单个词
  47. StringBuilder sb = new StringBuilder();
  48. //去停用词后的文本
  49. String resultString="";
  50. //分词
  51. org.apache.lucene.analysis.TokenStream ts = analyzer.tokenStream("", sr);
  52. try{
  53. while(ts.incrementToken()){//将单个分好的词组装成一个StringBuilder,方便后面的去噪和去停用词
  54. CharTermAttribute ta = ts.getAttribute(CharTermAttribute.class);
  55. sb.append(ta.toString()+" ");
  56. }
  57. //构造去停用词对象
  58. FileExcludeStopWord fileExcludeStopWord = new FileExcludeStopWord();
  59. //去停用词,返回最终的分词结果
  60. resultString = fileExcludeStopWord.fileExcludeStopWord(sb.toString());
  61. }catch(Exception e){
  62. }
  63. //将结果输出到hdfs中
  64. context.write(new Text(anum), new Text(resultString));
  65. }
  66. catch(java.lang.ArrayIndexOutOfBoundsException e){
  67. context.getCounter(Counter.LINESKIP).increment(1);
  68. return;
  69. }
  70. }
  71. }
  72. //配置、提交任务
  73. public int run(String[] args) throws Exception {
  74. Configuration conf = getConf();
  75. Job job = new Job(conf, "TestTOkenizer");//任务名
  76. job.setJarByClass(TestTOkenizer.class);
  77. FileInputFormat.addInputPath(job, new Path(args[0]));//配置输入路径
  78. FileOutputFormat.setOutputPath(job, new Path(args[1]));//配置输出路径
  79. job.setMapperClass(Map.class);//指定Map类的位置
  80. job.setOutputFormatClass(org.apache.hadoop.mapreduce.lib.output.TextOutputFormat.class);
  81. job.setOutputKeyClass(Text.class);
  82. job.setOutputValueClass(Text.class);
  83. job.waitForCompletion(true);
  84. //接下来的两句,是自己测试玩的,查看下job的状态
  85. System.out.println("job's name"+job.getJobName());
  86. System.out.println("job status"+(job.isSuccessful()?"yes":"no"));
  87. return job.isSuccessful()?0:1;
  88. }
  89. }

3.3、去停用词类(这个类写的还不是很完美,停用词表也还不够完善,微博数据中,有一些没包含在停用词表中)

  1. package test;
  2. import java.io.BufferedReader;
  3. import java.io.FileInputStream;
  4. import java.io.InputStreamReader;
  5. import java.util.HashSet;
  6. import java.util.Set;
  7. public class FileExcludeStopWord {
  8. public static final String stopWordFile = "/home/jonsen/workspace/testPaoding/src/test/stopword.txt";
  9. public String fileExcludeStopWord(String splitResultStr) {
  10. try {
  11. BufferedReader stopWordFileBR = new BufferedReader(
  12. new InputStreamReader(new FileInputStream(stopWordFile)));
  13. // 用来存放停用词的集合
  14. Set<String> stopWordSet = new HashSet<String>();
  15. // 初始化停用词表
  16. String stopWord = null;
  17. while ((stopWord = stopWordFileBR.readLine()) != null) {
  18. stopWordSet.add(stopWord);
  19. }
  20. if(stopWordFileBR!=null){
  21. stopWordFileBR.close();
  22. }
  23. // 得到分词后的词汇数组,以便后续比较
  24. String[] resultArray = splitResultStr.split("[\\s]+");
  25. // 过滤停用词
  26. for (int i = 0; i < resultArray.length; i++) {
  27. if (stopWordSet.contains(resultArray[i])) {
  28. resultArray[i] = null;
  29. }
  30. }
  31. // 把过滤后的字符串数组存入到一个字符串中
  32. StringBuilder finalStr = new StringBuilder();
  33. for (int j = 0; j < resultArray.length; j++) {
  34. if (resultArray[j] != null) {
  35. finalStr = finalStr.append(resultArray[j]).append(" ");
  36. }
  37. }
  38. return finalStr.toString();
  39. } catch (Exception e) {
  40. e.printStackTrace();
  41. return "";
  42. }
  43. }
  44. }

3.4、上传文件到hdfs中:(直接右键上传文件即可)

文本文件内容:

3.5、配置输入输出路径运行程序:

注意:如果输出路径已经存在,一定要预先删除,否则会报错:

3.6、分词结果:

四、总结

到这里整个基于分布式的分词程序就完成了,但是只能处理文件数量不多的文本,大文件处理是没有问题的,如果都是单个的小文件,就需要自定义inputformat了,自己控制分片大小,才能提高效率!从分词的结果来看,很显然很多停用词是没有去掉的,很多没有意义的词语,噪音较大,还有一个问题就是庖丁的分词器,给出的所有可能的组合,这个虽好但是还需要花精力去分辨哪些是无用的,一般情况下,最长的是可能的组合的可能性比较大!

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Guff_9hys/article/detail/851394
推荐阅读
相关标签
  

闽ICP备14008679号