首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用NLTK和MaltParser的依赖关系解析器

使用NLTK和MaltParser的依赖关系解析器
EN

Stack Overflow用户
提问于 2014-02-17 04:10:03
回答 1查看 1.2K关注 0票数 6

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

代码语言:javascript
复制
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

并得到了这个好的结果:

代码语言:javascript
复制
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,我有这样的代码:

代码语言:javascript
复制
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())

和以下输出:

代码语言:javascript
复制
(pajamas (shot I) an elephant in my)

问:我可以获得与使用Stanford解析器时相同的输出吗?任何帮助都是最好的。

EN

回答 1

Stack Overflow用户

发布于 2017-12-23 07:52:54

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

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

原则上,非投影依赖分析可以通过麦芽输出的转换重新表示为成分分析,这将为您提供不需要太多努力的括号,但标记成分将是更多的工作。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/21815891

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档