斯坦福核心nlp java输出

斯坦福核心nlp java输出,java,nlp,stanford-nlp,Java,Nlp,Stanford Nlp,我是Java和Stanford NLP工具包的新手,尝试将它们用于一个项目。具体来说,我正在尝试使用StanfordCorenlpToolkit来注释文本(使用Netbeans而不是命令行),并尝试使用上提供的代码(使用StanfordCorenlpAPI)。。问题是:有人能告诉我如何在文件中获得输出,以便进一步处理它吗 我试着将图表和句子打印到控制台上,只是为了查看内容。这很有效。基本上我需要的是返回带注释的文档,这样我就可以从我的主类调用它并输出一个文本文件(如果可能的话)。我试图查看斯坦福

我是Java和Stanford NLP工具包的新手,尝试将它们用于一个项目。具体来说,我正在尝试使用StanfordCorenlpToolkit来注释文本(使用Netbeans而不是命令行),并尝试使用上提供的代码(使用StanfordCorenlpAPI)。。问题是:有人能告诉我如何在文件中获得输出,以便进一步处理它吗


我试着将图表和句子打印到控制台上,只是为了查看内容。这很有效。基本上我需要的是返回带注释的文档,这样我就可以从我的主类调用它并输出一个文本文件(如果可能的话)。我试图查看斯坦福corenlp的API,但鉴于我缺乏经验,我真的不知道返回此类信息的最佳方式是什么

代码如下:

Properties props = new Properties();
    props.put("annotators", "tokenize, ssplit, pos, lemma, ner, parse, dcoref");
    StanfordCoreNLP pipeline = new StanfordCoreNLP(props);

    // read some text in the text variable
    String text = "the quick fox jumps over the lazy dog";

    // create an empty Annotation just with the given text
    Annotation document = new Annotation(text);

    // run all Annotators on this text
    pipeline.annotate(document);

    // these are all the sentences in this document
    // a CoreMap is essentially a Map that uses class objects as keys and has values with custom types
    List<CoreMap> sentences = document.get(SentencesAnnotation.class);

    for(CoreMap sentence: sentences) {
      // traversing the words in the current sentence
      // a CoreLabel is a CoreMap with additional token-specific methods
      for (CoreLabel token: sentence.get(TokensAnnotation.class)) {
        // this is the text of the token
        String word = token.get(TextAnnotation.class);
        // this is the POS tag of the token
        String pos = token.get(PartOfSpeechAnnotation.class);
        // this is the NER label of the token
        String ne = token.get(NamedEntityTagAnnotation.class);       
      }

      // this is the parse tree of the current sentence
      Tree tree = sentence.get(TreeAnnotation.class);

      // this is the Stanford dependency graph of the current sentence
      SemanticGraph dependencies = sentence.get(CollapsedCCProcessedDependenciesAnnotation.class);
    }

    // This is the coreference link graph
    // Each chain stores a set of mentions that link to each other,
    // along with a method for getting the most representative mention
    // Both sentence and token offsets start at 1!
    Map<Integer, CorefChain> graph = 
      document.get(CorefChainAnnotation.class);
Properties=newproperties();
props.put(“注释器”、“标记化、ssplit、pos、引理、ner、解析、dcoref”);
StanfordCoreNLP管道=新的StanfordCoreNLP(道具);
//读取文本变量中的一些文本
String text=“敏捷的狐狸跳过了懒狗”;
//仅使用给定文本创建空注释
注释文档=新注释(文本);
//在此文本上运行所有注释器
管道注释(文件);
//这些是本文件中的所有句子
//CoreMap本质上是一个使用类对象作为键并具有自定义类型值的映射
列出句子=document.get(SentencesAnnotation.class);
for(CoreMap句子:句子){
//遍历当前句子中的单词
//CoreLabel是一个CoreMap,具有额外的令牌特定方法
for(CoreLabel标记:句子.get(TokensAnnotation.class)){
//这是令牌的文本
String word=token.get(TextAnnotation.class);
//这是令牌的POS标记
String pos=token.get(speechannotation.class的一部分);
//这是令牌的NER标签
字符串ne=token.get(NamedEntityTagAnnotation.class);
}
//这是当前句子的解析树
Tree-Tree=句子.get(TreeAnnotation.class);
//这是当前句子的斯坦福依存关系图
SemanticGraph dependencies=句子.get(CollapsedCCProcessedDependenciesAnnotation.class);
}
//这是共指链接图
//每个连锁店都存储一组相互链接的提及,
//以及获得最具代表性提及的方法
//句子和标记偏移量都从1开始!
映射图=
get(CorefChainAnnotation.class);

一旦您完成了代码示例中显示的任何或所有自然语言分析,您所需要做的就是以正常的Java方式将它们发送到文件中,例如,使用FileWriter进行文本格式输出。具体来说,这里有一个简单完整的示例,显示发送到文件的输出(如果您为其提供适当的命令行参数):

import java.io.*;
导入java.util.*;
导入edu.stanford.nlp.io.*;
导入edu.stanford.nlp.ling.*;
导入edu.stanford.nlp.pipeline.*;
导入edu.stanford.nlp.trees.*;
导入edu.stanford.nlp.util.*;
公共类StanfordCoreNlpDemo{
公共静态void main(字符串[]args)引发IOException{
打印输出;
如果(参数长度>1){
out=新的PrintWriter(args[1]);
}否则{
out=新的PrintWriter(System.out);
}
PrintWriter xmlOut=null;
如果(参数长度>2){
xmlOut=新的PrintWriter(args[2]);
}
StanfordCoreNLP管道=新的StanfordCoreNLP();
注释;
如果(args.length>0){
annotation=新注释(IOUtils.slurpFileNoExceptions(args[0]);
}否则{
注释=新注释(“Kosgi Santosh给斯坦福大学发了一封电子邮件,但没有得到回复。”);
}
管道注释(注释);
管道预打印(注释,输出);
如果(xmlOut!=null){
管道.xmlPrint(注释,xmlOut);
}
//注释是一张地图,您可以单独获取并使用各种分析。
//例如,它获取文本中第一个句子的解析树。
列出句子=annotation.get(coreanotations.SentencesAnnotation.class);
if(句子!=null&&句子.size()>0){
CoreMap语句=语句。获取(0);
Tree-Tree=句子.get(TreeCoreAnnotations.TreeAnnotation.class);
out.println();
println(“解析的第一句是:”);
树。打印(输出);
}
}
}

我试着将图表和句子打印到控制台,只是为了查看内容。这很有效。基本上我需要的是返回带注释的文档,这样我就可以从我的主类调用它并输出一个文本文件(如果可能的话)。我试图查看斯坦福corenlp的API,但鉴于我缺乏经验,我真的不知道返回此类信息的最佳方式是什么。。谢谢你的光临advance@SophieM我已经在问题中添加了这些信息。将来,你可以自己编辑(你甚至可以得到一个徽章!)@SophieM你能发布代码吗?@SophieM:你能告诉我你是如何在netbeans上执行的吗?非常感谢你,@Christopher Manning
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);
    }
  }

}