【发布时间】:2022-01-27 15:10:16
【问题描述】:
我正在使用nltk 中的语法定义名词短语。 nltk提供的例子是:
grammar = "NP: {<DT>?<NNP>*<NN>}"
那么如果我有一个类似的句子:show me the Paris hospitals,库可以检测到名词短语:
>>> s
'show me the Paris hospitals'
>>> grammar = "NP: {<DT>?<NNP>*<NNS>}"
>>> nltk.RegexpParser(grammar).parse(nltk.pos_tag(nltk.word_tokenize(s)))
Tree('S', [('show', 'VB'), ('me', 'PRP'), Tree('NP', [('the', 'DT'), ('Paris', 'NNP'), ('hospitals', 'NNS')])])
现在,句子可以写成另一种方式:show me the hospitals of Paris,因此我需要将语法更改为:
>>> grammar = "NP: {<DT>?<NNS><IN><NNP>}"
>>> s = "show me the hospitals in Paris"
>>> nltk.RegexpParser(grammar).parse(nltk.pos_tag(nltk.word_tokenize(s)))
Tree('S', [('show', 'VB'), ('me', 'PRP'), Tree('NP', [('the', 'DT'), ('hospitals', 'NNS'), ('in', 'IN'), ('Paris', 'NNP')])])
如何将这两种语法组合成一个独特的语法?我无法弄清楚这两个语法的 OR 条件。
【问题讨论】: