Algorithm 如何捕捉句子中以动词开头、以名词结尾的部分

Algorithm 如何捕捉句子中以动词开头、以名词结尾的部分,algorithm,nlp,nltk,Algorithm,Nlp,Nltk,我尝试使用NLTK包在一个句子中捕获以下内容: verb + smth + noun 也可能是这样 verb + smth + noun + and + noun 我真的花了整整一天的时间来摆弄正则表达式,但仍然没有产生任何合适的结果 我在看教程,但没什么帮助。当你知道这些东西可能介于两者之间时,有一个相对简单的方法使用NLTK的CFG。这肯定不是最有效的方法。要获得全面的分析,请参阅NLTK的关于 正如你所提到的,我们有两种模式: <verb> ... <noun>

我尝试使用NLTK包在一个句子中捕获以下内容:

verb + smth + noun
也可能是这样

verb + smth + noun + and + noun
我真的花了整整一天的时间来摆弄正则表达式,但仍然没有产生任何合适的结果


我在看教程,但没什么帮助。

当你知道这些东西可能介于两者之间时,有一个相对简单的方法使用NLTK的CFG。这肯定不是最有效的方法。要获得全面的分析,请参阅NLTK的关于

正如你所提到的,我们有两种模式:

<verb> ... <noun>

<verb> ... <noun> "and" <noun>
现在,假设这是我们要使用过滤器的句子列表:

sentences = ['scolded me and you', 'included certainly uhm maybe even her and I', 'loved me and maybe many others','nominated others not even him', 'told certainly among others uhm let me finish ... us and them', 'rescued all of us','rescued me and somebody else']
如您所见,第三个和最后一个短语无法通过筛选。我们可以检查其余部分是否与模式匹配:

def sentence_filter(sent, grammar):
    rd_parser = nltk.RecursiveDescentParser(grammar)
    try:
       for p in rd_parser.parse(sent):
           print("SUCCESS!")
    except:
        print("Doesn't match the filter...")

for s in sentences:
    s = s.split()
    sentence_filter(s, grammar)
运行此操作时,我们得到以下结果:

>>> 
SUCCESS!
SUCCESS!
Doesn't match the filter...
SUCCESS!
SUCCESS!
SUCCESS!
Doesn't match the filter...
>>> 

不清楚你在这里做了什么。在第一句话中,您试图使用NLTK,但在第二句话中,您花了一天的时间处理正则表达式。那么NLTK或正则表达式是什么呢。另外,如果你花了一整天的时间,也许你会与我们分享你能达到的最接近的尺寸?使用
nltk.trigrams()
和/或
nltk.ngrams()
检查你感兴趣的所有尺寸的帧,并保留你想要的帧。如果帧长度变化太大,只需迭代索引即可。
>>> 
SUCCESS!
SUCCESS!
Doesn't match the filter...
SUCCESS!
SUCCESS!
SUCCESS!
Doesn't match the filter...
>>>