gpt4 book ai didi

python - 为什么带有 nltk 的 Stanford 解析器不能正确解析一个句子?

转载 作者:太空狗 更新时间:2023-10-30 01:00:27 25 4
gpt4 key购买 nike

我在 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()

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

Example parse tree Example dependency tree

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

正确输出的例子如下:

correctly parsed

correct dependency parsed tree

最佳答案

再一次强调,没有模型是完美的(参见 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 仅生成依赖树:

enter image description here

关于python - 为什么带有 nltk 的 Stanford 解析器不能正确解析一个句子?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34968716/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com