我想从stanford parser检索到的语法生成句子,但是NLTK不能解释PRP$。在
from nltk.parse.stanford import StanfordParser
from nltk.grammar import CFG
from nltk.parse.generate import generate
sp=StanfordParser(model_path='/home/aman/stanford_resource/stanford-parser-full-2014-06-16/stanford-parser-3.4-models/edu/stanford/nlp/models/lexparser/englishPCFG.ser.gz',path_to_jar='/home/aman/stanford_resource/stanford-parser-full-2014-06-16/stanford-parser.jar',path_to_models_jar='/home/aman/stanford_resource/stanford-postagger-full-2014-08-27/stanford-postagger-3.4.1.jar')
sent1='He killed the tiger in his pants'
parse_result=sp.raw_parse(sent1)
grammar_list=[]
for p in parse_result:
l=p.productions()
grammar_string='\n'.join(map(str,l))
grammar=CFG.fromstring(grammar_string)
#grammar_list.append(grammar)
#for s in generate(grammar,n=3):
# print s
ValueError: Unable to parse line 11: NP -> PRP$ NNS
Expected a nonterminal, found: $ NNS
怎么可能呢有效。应该我专门为这些语法类别指导nltk。在
我不知道您为什么要尝试将手工构建的CFG与Stanford解析器的输出结合起来,但是这里有一个解决这个问题的方法:
我对
nltk/grammar.py
的快速检查表明,$
不是非终端名称的合法字符。可以通过如下方式修补模块来轻松纠正:在上面的内容中,我刚刚将
^{pr2}$$
添加到您将在nltk/grammar.py
中找到的regexp。然后,您可以创建和使用其结果中包含$
的语法:相关问题 更多 >
编程相关推荐