【问题标题】:How to get a parse in a bracketed format (without POS tags)?如何获得括号格式的解析(没有 POS 标签)?
【发布时间】:2018-09-16 00:34:31
【问题描述】:

我想把一个句子解析成这种形式的二进制解析(SNLI语料库中使用的格式):

句子:“一个骑着马的人跳过一架坏掉的飞机。”

解析: ( ( ( 一个人 ) ( on (a horse ) ) ) ( ( jumps ( over (a (broken (down plane ) ) ) ) ) .) )

我找不到执行此操作的解析器。

注意:这个问题之前已经问过(How to get a binary parse in Python)。但答案没有帮助。而且我无法发表评论,因为我没有所需的声誉。

【问题讨论】:

    标签: python nlp stanford-nlp


    【解决方案1】:

    我分析了接受的版本,因为我需要 python 中的一些东西,所以我创建了一个简单的函数,它产生了相同的结果。为了解析句子,我改编了the referenced link 的版本。

    import re
    import string
    from stanfordcorenlp import StanfordCoreNLP
    from nltk import Tree
    from functools import reduce
    regex = re.compile('[%s]' % re.escape(string.punctuation))
    
    def parse_sentence(sentence):
        nlp = StanfordCoreNLP(r'./stanford-corenlp-full-2018-02-27')
        sentence = regex.sub('', sentence)
    
        result = nlp.parse(sentence)
        result = result.replace('\n', '')
        result = re.sub(' +',' ', result)
    
        nlp.close() # Do not forget to close! The backend server will consume a lot memery.
        return result.encode("utf-8")
    
    def binarize(parsed_sentence):
        sentence = sentence.replace("\n", "")
    
        for pattern in ["ROOT", "SINV", "NP", "S", "PP", "ADJP", "SBAR", 
                        "DT", "JJ", "NNS", "VP", "VBP", "RB"]:
            sentence = sentence.replace("({}".format(pattern), "(")
    
        sentence = re.sub(' +',' ', sentence)
        return sentence
    

    我的版本或接受的版本都没有提供与 SNLIMultiNLI 语料库中相同的结果,因为它们将树的两个单叶聚集在一起。 MultiNLI 语料库中的一个示例显示

    "( ( The ( new rights ) ) ( are ( nice enough ) ) )",

    展位在这里回答的地方返回

    '( ( ( ( The) ( new) ( rights)) ( ( are) ( ( nice) ( enough)))))'.

    我不是 NLP 方面的专家,所以我希望这不会产生任何影响。至少它不适用于我的应用程序。

    【讨论】:

      【解决方案2】:

      这里是一些示例代码,它将擦除树中每个节点的标签。

      package edu.stanford.nlp.examples;
      
      import edu.stanford.nlp.ling.*;
      import edu.stanford.nlp.pipeline.*;
      import edu.stanford.nlp.trees.*;
      import edu.stanford.nlp.util.*;
      
      import java.util.*;
      
      public class PrintTreeWithoutLabelsExample {
      
        public static void main(String[] args) {
          // set up pipeline properties
          Properties props = new Properties();
          props.setProperty("annotators", "tokenize,ssplit,pos,lemma,parse");
          // use faster shift reduce parser
          props.setProperty("parse.model", "edu/stanford/nlp/models/srparser/englishSR.ser.gz");
          props.setProperty("parse.maxlen", "100");
          props.setProperty("parse.binaryTrees", "true");
          // set up Stanford CoreNLP pipeline
          StanfordCoreNLP pipeline = new StanfordCoreNLP(props);
          // build annotation for text
          Annotation annotation = new Annotation("The red car drove on the highway.");
          // annotate the review
          pipeline.annotate(annotation);
          for (CoreMap sentence : annotation.get(CoreAnnotations.SentencesAnnotation.class)) {
            Tree sentenceConstituencyParse = sentence.get(TreeCoreAnnotations.TreeAnnotation.class);
            for (Tree subTree : sentenceConstituencyParse.subTrees()) {
              if (!subTree.isLeaf())
                subTree.setLabel(CoreLabel.wordFromString(""));
            }
            TreePrint treePrint = new TreePrint("oneline");
            treePrint.printTree(sentenceConstituencyParse);
          }
        }
      }
      

      【讨论】:

      • 感谢您的回答。我在开始时遇到了一些困难。这里有一个简短的总结:下载并安装CoreNLP,将给定的答案保存到src/edu/stanford/nlp/examples/,然后下载并导出Stanford SRParser
      猜你喜欢
      • 2014-05-20
      • 2015-10-06
      • 1970-01-01
      • 2011-12-30
      • 1970-01-01
      • 1970-01-01
      • 2020-04-26
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多