使用NLTK和MaltParser的依赖项解析器 [英] Dependency parser using NLTK and MaltParser

查看:75
本文介绍了使用NLTK和MaltParser的依赖项解析器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用NLTK和Maltparser从自然语言的句子中提取依存关系.我通过以下代码使用斯坦福解析器进行了一些实验:

I'm using NLTK and Maltparser to extract dependencies from sentences in natural language. I did some experiments using Stanford parser with this code:

sentence =  '''I shot an elephant in my pajamas'''
os.popen("echo '"+sentence+"' > ~/stanfordtemp.txt")
parser_out = os.popen("/usr/local/Cellar/stanford-parser/2.0.3/bin/lexparser.sh ~/stanfordtemp.txt").readlines()

for i, tag in enumerate(parser_out):
    if len(tag.strip()) > 0 and tag.strip()[0] == '(':
        parse = " ".join(tag.strip())
        print i, "Parse: ", tag
    elif len(tag.strip()) > 0:
        print i, "Typed dependencies: ", tag 
bracketed_parse = " ".join( [tag.strip() for tag in parser_out if len(tag.strip()) > 0 and tag.strip()[0] == "("] )
print bracketed_parse

并获得了不错的结果:

Parsing [sent. 1 len. 7]: I shot an elephant in my pajamas

Parsed 7 words in 1 sentences (12,87 wds/sec; 1,84 sents/sec).
0 Parse:  (ROOT
1 Parse:    (S
2 Parse:      (NP (PRP I))
3 Parse:      (VP (VBD shot)
4 Parse:        (NP (DT an) (NN elephant))
5 Parse:        (PP (IN in)
6 Parse:          (NP (PRP$ my) (NNS pajamas))))))
8 Typed dependencies:  nsubj(shot-2, I-1)
9 Typed dependencies:  root(ROOT-0, shot-2)
10 Typed dependencies:  det(elephant-4, an-3)
11 Typed dependencies:  dobj(shot-2, elephant-4)
12 Typed dependencies:  poss(pajamas-7, my-6)
13 Typed dependencies:  prep_in(shot-2, pajamas-7)

使用MaltParser,我有以下代码:

With the MaltParser I have this code:

os.environ['MALTPARSERHOME']="/Applications/maltparser-1.7.2"
maltParser = nltk.parse.malt.MaltParser(working_dir="/Applications/maltparser-1.7.2", 
                                        mco="engmalt.linear-1.7",
                                        additional_java_args=['-Xmx1024m'])
txt = '''I shot an elephant in my pajamas'''
graph = maltParser.raw_parse(txt)
print(graph.tree().pprint())

和以下输出:

(pajamas (shot I) an elephant in my)

问题:我可以使用与使用斯坦福解析器相同的输出吗?任何帮助都会很棒.

Question: Can I have the same output as when I'm using Stanford parser? Any help would be great.

推荐答案

在MALT文档中浏览时,我看不到一个选项与您显示的详细的Stanford Parser输出100%匹配,但是您可以尝试使用connlx和connlu输出选项以查看它们是否具有您所需的信息.

Poking around in the MALT documentation I don't see an option that will 100% match the detailed Stanford Parser output that you show but you could experiment with the connlx and connlu output options to see if they have the information that you need.

http://www.maltparser.org/options.html

原则上,通过转换MALT输出,可以将非投影依赖性分析重新构造为选区分析,这样可以为您省去很多麻烦,但标记组成部分将需要更多工作.

In principle, a non-projective dependency analysis can be reformulated as a constituency analysis by a transformation of the MALT output which would give you the bracketing for not too much effort but labeling the consituents would be a lot more work.

这篇关于使用NLTK和MaltParser的依赖项解析器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆