0

我创建了以下代码来识别由一个动词后跟一个或多个限定词,然后是一个或多个名词组成的语法。语法不会将第二个名词识别为语法中的第二个名词(例如短语:“监控停车位”):

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))
4

1 回答 1

0

就像在标准regex中获得您需要的许多元素+(这意味着one or more)或*(这意味着zero or more

 {<VBG|VBZ|VB>?<DT>?<NN|NNS>+}

您也可以使用{,2}to get 0, 1or 2elements, or {1,2}get 1or 2elements, or {2}to get exactly 2elements

 {<VBG|VBZ|VB>?<DT>?<NN|NNS>{,2}}
于 2019-12-21T06:19:03.987 回答