java - Stanford CoreNLP- 文本到 XML

标签 java stanford-nlp

有人可以向我提供 Stanford CoreNLP 的 Java 实现以将文本文件转换为 XML 文件。我可以用

做同样的事情
java -cp stanford-corenlp-2012-05-22.jar;stanford-corenlp-2012-05-22-models.jar;xom.jar;joda-time.jar -Xmx3g edu.stanford.nlp.pipeline.StanfordCoreNLP -annotators tokenize,ssplit,pos,lemma,ner,parse,dcoref -file input.txt

在命令行中。

最佳答案

Joop 的答案当然有效,但如果您想更深入地挖掘而不是使用 main 方法作为 API,这里有一个完整的示例,展示了如何将句子分析写入 XML 文件。

import java.io.*;
import java.util.*;

import edu.stanford.nlp.io.*;
import edu.stanford.nlp.ling.*;
import edu.stanford.nlp.pipeline.*;
import edu.stanford.nlp.trees.*;
import edu.stanford.nlp.util.*;

public class StanfordCoreNlpDemo {

  public static void main(String[] args) throws IOException {
    PrintWriter out;
    if (args.length > 1) {
      out = new PrintWriter(args[1]);
    } else {
      out = new PrintWriter(System.out);
    }
    PrintWriter xmlOut = null;
    if (args.length > 2) {
      xmlOut = new PrintWriter(args[2]);
    }

    StanfordCoreNLP pipeline = new StanfordCoreNLP();
    Annotation annotation;
    if (args.length > 0) {
      annotation = new Annotation(IOUtils.slurpFileNoExceptions(args[0]));
    } else {
      annotation = new Annotation("Kosgi Santosh sent an email to Stanford University. He didn't get a reply.");
    }

    pipeline.annotate(annotation);
    pipeline.prettyPrint(annotation, out);
    if (xmlOut != null) {
      pipeline.xmlPrint(annotation, xmlOut);
    }
    // An Annotation is a Map and you can get and use the various analyses individually.
    // For instance, this gets the parse tree of the first sentence in the text.
    List<CoreMap> sentences = annotation.get(CoreAnnotations.SentencesAnnotation.class);
    if (sentences != null && sentences.size() > 0) {
      CoreMap sentence = sentences.get(0);
      Tree tree = sentence.get(TreeCoreAnnotations.TreeAnnotation.class);
      out.println();
      out.println("The first sentence parsed is:");
      tree.pennPrint(out);
    }
  }

}

关于java - Stanford CoreNLP- 文本到 XML,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11306772/

相关文章:

java - 为什么斯坦福 CoreNLP 服务器将命名实体拆分为单个标记?

java - RMI 服务器自行关闭

java - 为什么 Java Math 库没有返回 float 的函数?

java - 无法解析 'import org.openqa.selenium.android.AndroidDriver'

python - 如何根据 python 和 NLPK 中 CSV 文件的训练数据预测位置

stanford-nlp - 管道定义上的类型初始化异常

java - 在java代码中嵌入数据文件

java - 如何使用 Eclipse 从相同代码自动构建不同的 JAR?

java正则表达式返回false

java - 如何使用 stanford-parser 从文本文件中提取每个句子?