你好!要在Python中抽取含有关键词的句子,你可以使用正则表达式来匹配关键词。例如,假设你想抽取所有含有单词"apple"的句子,你可以使用如下代码:
import re
def extract_sentences(text, keyword):
# 使用正则表达式匹配句子
pattern = r'[^.!?]*\b' + keyword + r'\b[^.!?]*[.!?]'
return re.findall(pattern, text, re.IGNORECASE)
# 测试函数
text = "I like apples. Apples are my favorite fruit. I have an apple tree in my backyard."
keyword = "apple"
sentences = extract_