Skip to main content
 首页 » 编程设计

python之NLP:根据给定语法验证句子

2024年11月24日9zhoujg

我有一个英语句子语料库

sentences = [ 
    "Mary had a little lamb.", 
    "John has a cute black pup.", 
    "I ate five apples." 
] 

和语法(为了简单起见)

grammar = (''' 
    NP: {<NNP><VBZ|VBD><DT><JJ>*<NN><.>} # NP 
    ''') 

我想过滤掉不符合语法的句子。 是否有内置的 NLTK 函数可以实现这一点? 在上面的示例中,前两个句子遵循我的语法模式,但不是最后一个。

请您参考如下方法:

长话短说

编写一个语法,检查它是否解析,遍历子树并寻找你正在寻找的非终端,例如NP

参见:

代码:

import nltk 
 
grammar = (''' 
    NP: {<NNP><VBZ|VBD><DT><JJ>*<NN><.>} # NP 
    ''') 
 
sentences = [ 
    "Mary had a little lamb.", 
    "John has a cute black pup.", 
    "I ate five apples." 
] 
 
def has_noun_phrase(sentence): 
    parsed = chunkParser.parse(pos_tag(word_tokenize(sentence))) 
    for subtree in parsed: 
        if type(subtree) == nltk.Tree and subtree.label() == 'NP': 
            return True 
    return False 
 
chunkParser = nltk.RegexpParser(grammar) 
for sentence in sentences: 
    print(has_noun_phrase(sentence))