java - 如何保存 Java 中的 Open NLP 解析器输出,以便在 Python 中使用它?

标签 java python opennlp

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

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

最佳答案

嗯,我认为您必须在解析对象中使用 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();
    }
  }

}

关于java - 如何保存 Java 中的 Open NLP 解析器输出,以便在 Python 中使用它?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43049224/

相关文章:

java - 如何让 Java 在我的场景中检测按键?

python - 如何使用所有 xticks 绘制 pandas 多索引数据帧

elasticsearch - Elasticsearch中的命名实体提取

elasticsearch - 在Elasticsearch中映射openNLP或StanfordNLP

tags - OpenNLP 的德国 maxent 模型中使用了哪些标签集?

java - 网格布局 View Java Swing 组件位置

java - 如果不使用由以下人员实现的 Google guice 则无法工作

java - 当我使用 @collectionId 映射集合时,如何获取 Hibernate 生成的 key ?

python - 如何使用 PyQGIS 在 auth-manager 中获取加密密码?

python - Tensorflow FailedPreconditionError,但所有变量都已初始化