繁体   English   中英

如何从Java保存Open NLP解析器输出,以便我可以在Python中使用它?

[英]How can I save the Open NLP parser output from Java, so that I can use it in Python?

如何从Java保存Open NLP解析器输出,以便我可以在Python中使用它?

我需要使用Open NLP中的解析树来执行Python中的一些机器学习任务。 OpenNLP是用Java编写的,我不知道如何保存数据,所以我可以通过列表或Python中的树来使用它。

好吧,我认为你必须在parse对象中使用show(StringBuffer)方法,然后使用Java中的FileWriter之类的东西将其写入文件。 从那里你可以用Python来捡起它。

这样的事情应该这样做(未经测试)

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import opennlp.tools.parser.Parse;

/**
 *
 * @author mgiaconia
 */
public class ParseWriter {

  public static void main(String[] args) {
    String filePath = args[0];

    try (FileWriter outputFileWriter = new FileWriter(new File(args[0]))) {
      ///this string taken from the Parse's unit tests in the OpenNLP  source code
      Parse p1 = Parse.parseParse("(TOP  (S-CLF (NP-SBJ (PRP It)  )(VP (VBD was) "
          + " (NP-PRD (NP (DT the)  (NN trial)  )(PP (IN of) "
          + " (NP (NP (NN oleomargarine)  (NN heir)  )(NP (NNP Minot) "
          + " (PRN (-LRB- -LRB-) (NNP Mickey) "
          + " (-RRB- -RRB-) )(NNP Jelke)  )))(PP (IN for) "
          + " (NP (JJ compulsory)  (NN prostitution) "
          + " ))(PP-LOC (IN in)  (NP (NNP New)  (NNP York) "
          + " )))(SBAR (WHNP-1 (WDT that)  )(S (VP (VBD put) "
          + " (NP (DT the)  (NN spotlight)  )(PP (IN on)  (NP (DT the) "
          + " (JJ international)  (NN play-girl)  ))))))(. .)  ))");

      StringBuffer parseString = new StringBuffer();
      //pass this referece into the show method
      p1.show(parseString);
      outputFileWriter.write(parseString.toString());
      outputFileWriter.flush();

    } catch (IOException ex) {
      ex.printStackTrace();
    }
  }

}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM