本文介绍了Python提取包含2个单词的句子的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我遇到了与此链接 Python提取包含单词的句子,但不同之处是我想在同一句子中找到2个单词.我需要从一个包含2个特定单词的语料库中提取句子.有人可以帮我吗?
I have the same problem that was discussed in this link Python extract sentence containing word, but the difference is that I want to find 2 words in the same sentence. I need to extract sentences from a corpus, which contains 2 specific words. Does anyone could help me, please?
推荐答案
如果这是您的意思:
import re
txt="I like to eat apple. Me too. Let's go buy some apples."
define_words = 'some apple'
print re.findall(r"([^.]*?%s[^.]*\.)" % define_words,txt)
Output: [" Let's go buy some apples."]
您也可以尝试:
define_words = raw_input("Enter string: ")
检查句子中是否包含已定义的单词:
Check if the sentence contain the defined words:
import re
txt="I like to eat apple. Me too. Let's go buy some apples."
words = 'go apples'.split(' ')
sentences = re.findall(r"([^.]*\.)" ,txt)
for sentence in sentences:
if all(word in sentence for word in words):
print sentence
这篇关于Python提取包含2个单词的句子的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!