如何将StanfordNLPCore的带注释输出保存为字符串?

时间:2017-04-17 17:53:12

标签: java nlp stanford-nlp printwriter

我的代码接受一个字符串,相应地注释该字符串,然后将输出发送到OutputStream(prettyPrint将带注释的字符串发送到System.out)。我想把它写成一个字符串而不是System.out,但我似乎无法让它太工作。

public void NERprint(String s) throws IOException {


  String output = "";
  PrintWriter out = new PrintWriter(System.out);

// Add in sentiment
  Properties props = new Properties();
// parse, dcoref, sentiment
  props.setProperty("annotators", "tokenize, ssplit, pos, lemma, ner");

  StanfordCoreNLP pipeline = new StanfordCoreNLP(props);

// Initialize an Annotation with some text to be annotated. The text is the argument to the constructor.
  Annotation annotation = new Annotation(s);

// run all the selected Annotators on this text
  pipeline.annotate(annotation);
 // this prints out the results of sentence analysis to file(s) in good formats

  pipeline.prettyPrint(annotation, out);

}

1 个答案:

答案 0 :(得分:1)

以下是一些示例代码:

pipeline.annotate(annotation);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
pipeline.prettyPrint(annotation, baos);
String stringOutput = baos.toString();