2014-02-16 33 views
6

我使用NLTK和Maltparser從自然語言中的句子中提取依賴關係。我沒有使用斯坦福解析器使用此代碼一些實驗:使用NLTK和MaltParser的依賴關係解析器

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我有這樣的代碼:

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) 

問題:我可以使用與使用斯坦福解析器時相同的輸出嗎?任何幫助都會很棒。

+0

嘿你找到任何方法嗎? –

+0

沒有不幸的不是! – Marcelo

回答

1

在MALT文檔中查找我沒有看到與100%匹配的顯示的詳細斯坦福分析器輸出的選項,但您可以嘗試使用connlx和connlu輸出選項來查看他們是否具有您的信息需要。

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

原則上,非投影依賴性分析可以通過MALT輸出的轉變,這將使你沒有太多的精力包圍,但標籤的構成要件將再次闡述爲一個選區分析很多工作。