当前位置:   article > 正文

NLPIR+Hadoop_nlpir的hadoop

nlpir的hadoop

最近在学习过程中,需要对文本进行分词,而且数据量比较大,在 Windows上使用NLPIR处理小文件基本上没有问题( 可以看这里),看NLPIR的开发文档是支持分布式的,因而考虑在Linux上实现hadoop+NLPIR对大量文本数据进行分词和标注。这个过程让我经历了焦头烂额,所以记录下来,便于自己查看,也可以帮助有需要的伙伴~

1.下载NLPIR

NLPIR原名 ICTCLAS,下载点这里,我下载的是2016-10-9发布的NLPIR2016,同时支持Windows和Linux,且有Java/C/C++/C# 多种语言,我这里使用的是Java语言。项目中需要两个文件:一是Data文件夹下的所有内容,二是libNLPIR.so文件

2.Hadoop+NLPIR 代码

NLPIR配置

  1. package com.katoa.segment;
  2. import com.katoa.util.CLibrary;
  3. import com.sun.jna.Native;
  4. public class NLPIR {
  5. CLibrary Instance = (CLibrary) Native.loadLibrary("<span style="color:#FF0000;">/usr/local/workspace/NLPIR2015/libNLPIR.so</span>", CLibrary.class);
  6. private boolean initFlag = false;
  7. public boolean init() {
  8. System.out.println("jna.library.path");
  9. String argu = "/usr/local/workspace/NLPIR2015/";
  10. // String system_charset = "GBK";//GBK----0
  11. int charset_type = 1; //UTF-8
  12. int init_flag = Instance.NLPIR_Init(argu, charset_type, "0");
  13. String nativeBytes = null;
  14. if (0 == init_flag) {
  15. nativeBytes = Instance.NLPIR_GetLastErrorMsg();
  16. System.err.println("初始化失败!fail reason is " + nativeBytes);
  17. return false;
  18. }
  19. initFlag = true;
  20. Instance.NLPIR_SetPOSmap(1); // 计算所一级标注
  21. return true;
  22. }
  23. public boolean unInit() {
  24. try {
  25. Instance.NLPIR_Exit();
  26. } catch (Exception e) {
  27. System.out.println(e);
  28. return false;
  29. }
  30. initFlag = false;
  31. return true;
  32. }
  33. public CLibrary getInstance() {
  34. return Instance;
  35. }
  36. public boolean isInitFlag() {
  37. return initFlag;
  38. }
  39. /**
  40. * 分词,1表示标注,0表示不标注
  41. *
  42. * @param context
  43. * 字符串
  44. * @param bPOSTagged
  45. * 是否标注
  46. * @return String
  47. */
  48. public String segment(String context, int bPOSTagged) {
  49. // 分词和标注处理,1表示标注,0表示不标注
  50. String result = Instance.NLPIR_ParagraphProcess(context, bPOSTagged);
  51. return result;
  52. }
  53. /**
  54. * 分词
  55. *
  56. * @param context
  57. * 字符串
  58. * @param reduce
  59. * 是否抽取
  60. * @return String
  61. */
  62. public String segment(String context, boolean reduce) {
  63. // 分词和标注处理,1表示标注,0表示不标注
  64. String result = Instance.NLPIR_ParagraphProcess(context, 1);
  65. String line = "";
  66. if (reduce) {
  67. String[] words = result.split(" ");
  68. for (String word : words) {
  69. word = word.replaceAll(" ", "");
  70. int index = word.lastIndexOf("/");
  71. if (word.substring(index + 1).equals("n") || word.substring(index + 1).equals("v")
  72. || word.substring(index + 1).equals("a") || word.substring(index + 1).equals("ad")
  73. || word.substring(index + 1).equals("d") || word.substring(index + 1).equals("o")
  74. || word.substring(index + 1).equals("other") || word.substring(index + 1).equals("xm")) {
  75. if (!word.substring(0, index).equals("")) {
  76. line = line + word.substring(0, index) + " ";
  77. }
  78. }
  79. }
  80. }
  81. return line;
  82. }
  83. }

Map 端

  1. package com.katoa.tuple;
  2. /**
  3. * @author 作者 : monkey
  4. * @version 创建时间:2016年10月9日 下午8:12:15
  5. * 类说明:语料预处理 mapper
  6. */
  7. import java.io.IOException;
  8. import java.util.ArrayList;
  9. import org.apache.commons.logging.Log;
  10. import org.apache.commons.logging.LogFactory;
  11. import org.apache.hadoop.io.LongWritable;
  12. import org.apache.hadoop.io.Text;
  13. import org.apache.hadoop.mapreduce.Mapper;
  14. import com.katoa.segment.NLPIR;
  15. public class FilterMapper extends Mapper<LongWritable, Text, Text, Text> {
  16. private static NLPIR nlpir = new NLPIR();
  17. <span style="color:#FF0000;">protected void setup(Context context) {
  18. nlpir.init();
  19. }</span>
  20. @Override
  21. public void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
  22. ArrayList<String> phraselist = new ArrayList<String>();
  23. String id = null;
  24. String v = null;
  25. String temp = value.toString();
  26. String[] contents = temp.split("\t");
  27. id= contents[0];
  28. v= contents[1];
  29. if (v!= null) {
  30. String str = posTagging(v);
  31. context.write(new Text(id), new Text(str));
  32. }
  33. }
  34. }
  35. <span style="color:#FF0000;">protected void cleanup(Context context) {
  36. nlpir.unInit();
  37. }</span>
  38. /**
  39. * POS Tagging
  40. *
  41. * @param text
  42. * The word to be marked
  43. * @return String
  44. */
  45. public static String posTagging(String text) {
  46. // text = Main.getNlpir().segment(text, 1);
  47. text = nlpir.segment(text, 1);
  48. String[] words = text.split(" ");
  49. String temp = "";
  50. int i = 0;
  51. for (String word : words) {
  52. if (!word.equals("")) { // Remove the extra spaces
  53. if (i == words.length - 1) {
  54. temp = temp + word;
  55. } else {
  56. temp = temp + word + " ";
  57. }
  58. }
  59. i++;
  60. }
  61. return temp;
  62. }
  63. }

将NLPIR的Data文件夹和libNLPIRso文件放到/usr/local/workspace/NLPIR2015下(路径根据自己的设置),然后使用fatjar将程序打成jar包并放到/usr/local/workspace目录下,使用hadoop jar 命令运行程序。


常见错误:

java.lang.UnsatisfiedLinkError:Unable to load library 'win64/NLPIR': Native library(linux-x86-64/libwin64/NLPIR.so) not found in resource path

java.lang.UnsatisfiedLinkError: Unable to load library 'libNLPIR.so':Can't obtain InputStream for linux-x86-64/libNLPIR.so

原因:Native.loadLibrary 加载的路径不正确,或没有使用libNLPIR.so,而是Windows下的NLPIR文件。

修改后建议重启Hadoop集群。


Exceptionfrom container-launch: ExitCodeException exitCode=134: /bin/bash: line 1: 41729已放弃

原因:MR程序问题,对于要加载其他配置文件的,如这里的libNLPIR.so文件,应该在MR中进行初始化,而不是在主程序中。这里用在mapper端,因此需要在mapper端使用setup()方法进行加载配置文件初始化,map做完任务后使用cleanup()方法结束。






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

闽ICP备14008679号