gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-04 04:12:40 25 4
gpt4 key购买 nike

我有一个英语句子语料库

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

关于python - NLP:根据给定语法验证句子,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56064307/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com