下面是我在 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
但没有写进去,有人能建议我怎么做吗
谢谢