2017-03-27 10 views
0

Open NLPパーサーの出力をJavaから保存して、Pythonで使用できるようにするにはどうすればよいですか?JavaからOpen NLPパーサの出力を保存して、Pythonで使用できるようにするにはどうすればよいですか?

Open NLPの構文解析ツリーを使用して、Pythonでいくつかの機械学習タスクを実行する必要があります。 OpenNLPはJavaであり、データを保存する方法がわからないので、Pythonのリストやツリーを使ってデータを使用することができます。

答えて

0

さて、あなたは、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(); 
    } 
    } 

} 
+0

おかげでそれを行う必要があります!私はこれを試してみましょう。 –

+0

@CyrusDsouza fyi Maven Shadeのようなものを使ってjarファイルを構築すると、ファイルパスを引数としてPythonでコマンドラインを呼び出すことができます。ただ大声で考えている。 – markg

+0

はい、ちょっとした助けを借りて仕事をしました。迅速な対応に感謝します! –

関連する問題