2
如何在NLTK中使用Stanford依賴關係解析器 我厭倦了下面的代碼它沒有給出任何樹結構。可以請指導,我是python和NLTK的新手。如何在Python中使用stanford依賴關係解析器
-----------------------------------------------------------------------------------------
import os
sentence = "this is a foo bar i want to parse."
os.popen("echo '"+sentence+"' > ~/stanfordtemp.txt")
parser_out = os.popen("~/stanford-parser-full-2014-06-16/lexparser.sh ~/stanfordtemp.txt").readlines()
bracketed_parse = " ".join([i.strip() for i in parser_out if (len(i.strip()) > 0) == "("])
print bracketed_parse
-----------------------------------------------------------------------------------------
Output:
Loading parser from serialized file edu/stanford/nlp/models/lexparser/englishPCFG.ser.gz ... done [1.3 sec].
Parsing file: /home/stanfordtemp.txt
Parsing [sent. 1 len. 10]: this is a foo bar i want to parse .
Parsed file: /home/stanfordtemp.txt [1 sentences].
Parsed 10 words in 1 sentences (18.05 wds/sec; 1.81 sents/sec).