Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/tensorflow/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Stanford nlp 如何解析句子列表?_Stanford Nlp - Fatal编程技术网

Stanford nlp 如何解析句子列表?

Stanford nlp 如何解析句子列表?,stanford-nlp,Stanford Nlp,我想用斯坦福NLP解析器解析一系列句子。 我的列表是一个ArrayList,如何使用LexicalizedParser解析所有列表 我想从表格中的每一句话中得到: Tree parse = (Tree) lp1.apply(sentence); 实际上,斯坦福NLP的文档提供了如何解析句子的示例 您可以找到文档尽管可以深入研究文档,但我将在这里提供代码,尤其是在链接移动和/或消亡的情况下。这个特定的答案使用了整个管道。如果对整个管道不感兴趣,我将在一秒钟内提供另一个答案 下面的示例是使用斯坦

我想用斯坦福NLP解析器解析一系列句子。 我的列表是一个
ArrayList
,如何使用
LexicalizedParser
解析所有列表

我想从表格中的每一句话中得到:

Tree parse =  (Tree) lp1.apply(sentence);

实际上,斯坦福NLP的文档提供了如何解析句子的示例


您可以找到文档

尽管可以深入研究文档,但我将在这里提供代码,尤其是在链接移动和/或消亡的情况下。这个特定的答案使用了整个管道。如果对整个管道不感兴趣,我将在一秒钟内提供另一个答案

下面的示例是使用斯坦福管道的完整方法。如果对共指解析不感兴趣,请从第三行代码中删除
dcoref
。因此,在下面的示例中,如果您只将管道输入文本体(文本变量),管道将为您(ssplit注释器)执行句子拆分。你只有一句话吗?好的,没关系,你可以把它作为文本变量输入

   // creates a StanfordCoreNLP object, with POS tagging, lemmatization, NER, parsing, and coreference resolution 
    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 = ... // Add your text here!

    // 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);
//创建一个StanfordCoreNLP对象,该对象具有词性标记、lemmatization、NER、解析和共指解析
Properties props=新属性();
props.put(“注释器”、“标记化、ssplit、pos、引理、ner、解析、dcoref”);
StanfordCoreNLP管道=新的StanfordCoreNLP(道具);
//读取文本变量中的一些文本
字符串文本=…//在这里添加您的文本!
//仅使用给定文本创建空注释
注释文档=新注释(文本);
//在此文本上运行所有注释器
管道注释(文件);
//这些是本文件中的所有句子
//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);

如承诺的那样,如果您不想访问完整的斯坦福管道(尽管我认为这是推荐的方法),您可以直接使用LexicalizedParser类。在本例中,您将下载最新版本的Stanford Parser(而另一个版本将使用CoreNLP工具)。确保除了解析器jar之外,您还拥有要使用的适当解析器的模型文件。示例代码:

LexicalizedParser lp1 = new LexicalizedParser("englishPCFG.ser.gz", new Options());
String sentence = "It is a fine day today";
Tree parse = lp.parse(sentence);

请注意,这适用于解析器的3.3.1版。

还可以查看解析器附带的ParserDemo示例。可以直接对作为句子的字符串调用apply()。