【问题标题】:Why Stanford parser with nltk is not correctly parsing a sentence?为什么带有 nltk 的斯坦福解析器无法正确解析句子?
【发布时间】:2016-04-30 08:27:39
【问题描述】:

我在 python 中使用带有 nltk 的斯坦福解析器,并从 Stanford Parser and NLTK 获得帮助来设置斯坦福 nlp 库。

from nltk.parse.stanford import StanfordParser
from nltk.parse.stanford import StanfordDependencyParser
parser     = StanfordParser(model_path="edu/stanford/nlp/models/lexparser/englishPCFG.ser.gz")
dep_parser = StanfordDependencyParser(model_path="edu/stanford/nlp/models/lexparser/englishPCFG.ser.gz")
one = ("John sees Bill")
parsed_Sentence = parser.raw_parse(one)
# GUI
for line in parsed_Sentence:
       print line
       line.draw()

parsed_Sentence = [parse.tree() for parse in dep_parser.raw_parse(one)]
print parsed_Sentence

# GUI
for line in parsed_Sentence:
        print line
        line.draw()

我得到了错误的解析和依赖树,如下例所示,它将“sees”视为名词而不是动词。

我该怎么办? 当我改变句子时,它工作得很好,例如(one = 'John see Bill')。 这句话的正确输出可以从这里查看correct ouput of parse tree

正确输出的例子也如下所示:

【问题讨论】:

  • 请贴出完整的代码 sn-p 以便其他人了解dep_parser 来自哪里=)

标签: python parsing nlp nltk stanford-nlp


【解决方案1】:

再一次,没有完美的模型(见Python NLTK pos_tag not returning the correct part-of-speech tag);P

您可以尝试使用“更准确”的解析器,使用 NeuralDependencyParser

首先使用正确的环境变量正确设置解析器(请参阅Stanford Parser and NLTKhttps://gist.github.com/alvations/e1df0ba227e542955a8a),然后:

>>> from nltk.internals import find_jars_within_path
>>> from nltk.parse.stanford import StanfordNeuralDependencyParser
>>> parser = StanfordNeuralDependencyParser(model_path="edu/stanford/nlp/models/parser/nndep/english_UD.gz")
>>> stanford_dir = parser._classpath[0].rpartition('/')[0]
>>> slf4j_jar = stanford_dir + '/slf4j-api.jar'
>>> parser._classpath = list(parser._classpath) + [slf4j_jar]
>>> parser.java_options = '-mx5000m'
>>> sent = "John sees Bill"
>>> [parse.tree() for parse in parser.raw_parse(sent)]
[Tree('sees', ['John', 'Bill'])]

请注意NeuralDependencyParser 只生成依赖树:

【讨论】:

  • 我使用的是模型 "englishPCFG.ser.gz" 而你使用的是模型 "english_UD.gz" 。但是,我们如何选择这些模型,以便我们能够选择正确的模型?
  • 没有完美的模型,也没有正确/错误的模型,只有最适合您的数据的模型。所以我想说,尝试所有这些,然后根据解析的最终目的来评估它们。
  • 请按照gist.github.com/alvations/e1df0ba227e542955a8a上的说明进行到底
  • 我正在尝试你的代码,它需要很长时间才能执行,...执行需要时间吗???
  • 关注gist.github.com/alvations/e1df0ba227e542955a8a 上的最后一个sn-p,上面写着“由于CoreNLP 在解析之前可能需要一段时间来加载所有模型,因此在解析时最好使用raw_parse_sents 而不是raw_parse不止一句话”;P
猜你喜欢
  • 1970-01-01
  • 2018-01-01
  • 2015-05-25
  • 1970-01-01
  • 1970-01-01
  • 2014-03-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多