在句子列表中查找单词列表并返回匹配的句子

问题描述 投票:-1回答:1

从句子列表和单词列表中,只有当所有三个单词都与单词列表(Trigrams)匹配时,如何返回句子列表。

请提出建议。下面是示例列表。

listwords = [['people','suffering','acute'], ['Covid-19','Corona','like'], ['people','must','collectively']]

listsent = ['The number of people suffering acute hunger could almost double.',
            'Lockdowns and global economic recession have',
            'one more shock – like Covid-19 – to push them over the edge',
            'people must collectively act now to mitigate the impact']

输出列表应为第一句和最后一句,因为它们在列表词中具有三个匹配的词。

预期输出是:

['The number of people suffering acute hunger could almost double.',
 'people must collectively act now to mitigate the impact']
python nlp nltk list-comprehension trigram
1个回答
0
投票

欢迎使用堆栈溢出

尝试此解决方案:

listwords = [['people','suffering','acute'], ['Covid-19','Corona','like'], ['people','must','collectively']]

listsent = ['The number of people suffering acute hunger could almost double.',
            'Lockdowns and global economic recession have',
            'one more shock – like Covid-19 – to push them over the edge',
            'people must collectively act now to mitigate the impact']

# interate through each sentence
for sentence in listsent:
    # iterate through each group of words
    for words in listwords:
        # check to see if each word group is in the current sentence
        if all(word in sentence for word in words):
            print(sentence)

我评论了这些行,以使您了解发生了什么事情

代码的第一部分遍历列表中的每个句子

for sentence in listsent:

然后我们需要遍历单词列表中的单词组

for words in listwords

这是让事情变得有趣的地方。由于您有嵌套列表,因此我们需要检查以确保在句子中找到所有三个单词。

if all(word in sentence for word in words):

最后,您可以打印出包含所有单词的每个句子

print(sentence)

您也可以将其放在函数中,并将找到的句子作为新列表返回

listwords = [['people','suffering','acute'], ['Covid-19','Corona','like'], ['people','must','collectively']]

listsent = ['The number of people suffering acute hunger could almost double.',
            'Lockdowns and global economic recession have',
            'one more shock – like Covid-19 – to push them over the edge',
            'people must collectively act now to mitigate the impact']


def check_words(listwords, listsent):
    listsent_new = []
    # interate through each sentence
    for sentence in listsent:
        # iterate through each group of words
        for words in listwords:
            # check to see if each word group is in the current sentence
            if all(word in sentence for word in words):
                listsent_new.append(sentence)
    return listsent_new


if __name__ == '__main__':
    print(check_words(listwords, listsent))
    
© www.soinside.com 2019 - 2024. All rights reserved.