1

以下は、Javaでスタンフォードnlpを使用して、単一のテキストファイルの解析されたツリーと依存関係を取得する私のプログラムです

public class Stanford {

public static void demoDP(LexicalizedParser lp, String filename) throws IOException {

    //FileOutputStream fos = new FileOutputStream("C://perl_java//Text-Parsed.txt");
      //ObjectOutputStream oos = new ObjectOutputStream(fos);

    //BufferedWriter bwriter = new BufferedWriter(fwriter);
    TreebankLanguagePack tlp = new PennTreebankLanguagePack();
    System.out.println("in method");
    System.out.println("lp in method"+lp);
    GrammaticalStructureFactory gsf = tlp.grammaticalStructureFactory();
    System.out.println(filename);
    for (List<HasWord> sentence : new DocumentPreprocessor(filename)) {
      Tree parse = lp.apply(sentence);
      parse.pennPrint();
      System.out.println();
      System.out.println("hiiiiii");
      GrammaticalStructure gs = gsf.newGrammaticalStructure(parse);
      Collection tdl = gs.typedDependenciesCCprocessed();
      System.out.println(tdl);
      PrintWriter pw = new PrintWriter("C://perl_java//Text-Parsed.txt");
      pw.print(tdl);
      pw.close();
       //oos.write(tdl.toString());
      //bwriter.write(tdl);
      System.out.println();
    }
    //oos.close();
  }


 public static void main(String args[])
 {
 LexicalizedParser lp =       LexicalizedParser.loadModel("edu/stanford/nlp/models/lexparser/englishPCFG.ser.gz");
demoDP(lp,"C://sample.txt")
 }
}

コンソールで出力を取得しています

使用したテキストファイルにこれを書き込む方法はありますがbufferwriterfileoutputstream書き込みに失敗しました。これを行う方法を教えてください。

ありがとう

4

2 に答える 2

1

このコードを試してください:

 File f=new File("Data/tree.txt");
      PrintWriter pw=new PrintWriter(f);
    // This option shows loading and using an explicit tokenizer
    String sent2 = "This is another sentence.";
    TokenizerFactory<CoreLabel> tokenizerFactory =PTBTokenizer.factory(new CoreLabelTokenFactory(), "");
    Tokenizer<CoreLabel> tok =tokenizerFactory.getTokenizer(new StringReader(sent2));
    List<CoreLabel> rawWords2 = tok.tokenize();
   Tree parse = lp.apply(rawWords2);

    TreebankLanguagePack tlp = new PennTreebankLanguagePack();
    GrammaticalStructureFactory gsf = tlp.grammaticalStructureFactory();
    GrammaticalStructure gs = gsf.newGrammaticalStructure(parse);
    List<TypedDependency> tdl = gs.typedDependenciesCCprocessed();
  System.out.println(tdl);
    System.out.println();
    // You can also use a TreePrint object to print trees and dependencies
    TreePrint tp = new TreePrint("penn,typedDependenciesCollapsed");
    tp.printTree(parse,pw);
于 2014-03-12T07:28:55.170 に答える
0

FileOutputStream などは、for ループの前に構築を行い、for ループに書き込み、for ループの後に閉じる必要があります。上記の例では、for ループで PrintWriter を閉じています。

于 2013-08-22T08:57:47.320 に答える