我创建了以下代码来识别由一个动词后跟一个或多个限定词,然后是一个或多个名词组成的语法。语法不会将第二个名词识别为语法中的第二个名词(例如短语:“监控停车位”):
Testing sentence in grammar: monitoring a parking space
Grammar Chunk:
(S (MT monitoring/VBG a/DT parking/NN) (MT space/NN))
False
这是 Python 3.5.6 中使用的代码:
import nltk
def extractMT(sent):
grammar = r"""
MT:
{<VBG|VBZ|VB>?<DT>?<NN|NNS>}
"""
chunker = nltk.RegexpParser(grammar)
ne = set()
chunk = chunker.parse(nltk.pos_tag(nltk.word_tokenize(sent)))
print("Grammar Chunk: ")
print(chunk)
for tree in chunk.subtrees(filter=lambda t: t.label() == 'MT'):
returnList = []
for child in tree.leaves():
returnList.append(child[0])
ne.add(' '.join(returnList))
return ne
testSentence1 = "monitoring a parking space"
print ("Testing sentence in grammar: " + testSentence1)
print ("Is sentence in grammar?: " + testSentence1 in extractMT(testSentence1))