下面是我在java中使用stanford 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")
}
}
我在控制台中获得输出
如何将此写入我使用bufferwriter
,fileoutputstream
但未能写入的文本文件,任何人都可以建议我如何执行此操作
由于
答案 0 :(得分: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);
答案 1 :(得分:0)
FileOutputStream或类似工作应该在for循环之前进行构造,在for循环中写入并在for循环之后关闭。在上面的示例中,您将在for循环中关闭PrintWriter。