使用python提取包含关键字或短语列表的句子

使用python提取包含关键字或短语列表的句子,python,file,search,text,Python,File,Search,Text,我使用以下代码从文件中提取了一个句子(该句子应该包含部分或全部搜索关键字) 上述代码的问题在于,如果其中一个搜索关键字与句子词不匹配,它不会打印所需的句子。我想要一个代码,打印包含部分或全部搜索关键字的句子。如果代码还可以搜索短语并提取相应的句子,那就太好了。因此,您希望找到至少包含一个关键字的句子。您可以使用而不是 编辑: 如果要查找包含最多关键字的句子: sent_words = [] for sentence in sentences: sent_words.append(set(

我使用以下代码从文件中提取了一个句子(该句子应该包含部分或全部搜索关键字)


上述代码的问题在于,如果其中一个搜索关键字与句子词不匹配,它不会打印所需的句子。我想要一个代码,打印包含部分或全部搜索关键字的句子。如果代码还可以搜索短语并提取相应的句子,那就太好了。

因此,您希望找到至少包含一个关键字的句子。您可以使用而不是

编辑: 如果要查找包含最多关键字的句子:

sent_words = []
for sentence in sentences:
    sent_words.append(set(sentence.split()))
num_keywords = [len(sent & set(search_keywords)) for sent in sent_words]

# Find only one sentence
ind = num_keywords.index(max(num_keywords))
# Find all sentences with that number of keywords
ind = [i for i, x in enumerate(num_keywords) if x == max(num_keywords)]

因此,您希望找到至少包含一个关键字的句子。您可以使用而不是

编辑: 如果要查找包含最多关键字的句子:

sent_words = []
for sentence in sentences:
    sent_words.append(set(sentence.split()))
num_keywords = [len(sent & set(search_keywords)) for sent in sent_words]

# Find only one sentence
ind = num_keywords.index(max(num_keywords))
# Find all sentences with that number of keywords
ind = [i for i, x in enumerate(num_keywords) if x == max(num_keywords)]

如果我理解正确,您应该使用而不是
all()


如果我理解正确,您应该使用而不是
all()


似乎您想计算每个句子中的
搜索键盘数。您可以按如下方式执行此操作:

sentences = "My name is sing song. I am a mother. I am happy. You sing like my mother".split(".")
search_keywords=['mother','sing','song']

for sentence in sentences:
    print("{} key words in sentence:".format(sum(1 for word in search_keywords if word in sentence)))
    print(sentence + "\n")

# Outputs:
#2 key words in sentence:
#My name is sing song
#
#1 key words in sentence:
# I am a mother
#
#0 key words in sentence:
# I am happy
#
#2 key words in sentence:
# You sing like my mother
或者,如果您只需要具有最匹配的
搜索关键字的句子,您可以制作一个字典并查找最大值:

dct = {}
for sentence in sentences:
    dct[sentence] = sum(1 for word in search_keywords if word in sentence)

best_sentences = [key for key,value in dct.items() if value == max(dct.values())]


print("\n".join(best_sentences))

# Outputs:
#My name is sing song
# You sing like my mother

似乎您想计算每个句子中的
搜索键盘数。您可以按如下方式执行此操作:

sentences = "My name is sing song. I am a mother. I am happy. You sing like my mother".split(".")
search_keywords=['mother','sing','song']

for sentence in sentences:
    print("{} key words in sentence:".format(sum(1 for word in search_keywords if word in sentence)))
    print(sentence + "\n")

# Outputs:
#2 key words in sentence:
#My name is sing song
#
#1 key words in sentence:
# I am a mother
#
#0 key words in sentence:
# I am happy
#
#2 key words in sentence:
# You sing like my mother
或者,如果您只需要具有最匹配的
搜索关键字的句子,您可以制作一个字典并查找最大值:

dct = {}
for sentence in sentences:
    dct[sentence] = sum(1 for word in search_keywords if word in sentence)

best_sentences = [key for key,value in dct.items() if value == max(dct.values())]


print("\n".join(best_sentences))

# Outputs:
#My name is sing song
# You sing like my mother

你的意思是
any()
而不是
all()
?假设搜索关键字是三个。。。。然后代码应该尝试打印包含所有关键字的句子…否则它应该检查是否有两个关键字…否则一个关键字我已经编写并编辑了一个答案希望完全回答您的问题您的意思是
any()
而不是
all()
?假设搜索关键字是三个。。。。然后代码应该尝试打印包含所有关键字的句子…否则它应该检查是否有两个关键字…否则一个关键字我已经编写并编辑了答案希望完全回答您的问题此代码打印包含任何搜索关键字的多个句子。但我想要的是包含大多数搜索关键字的句子作为最佳答案。此代码打印包含任何搜索关键字的几个句子。但我想要的是包含大多数搜索关键字的句子作为最佳答案。这将打印包含任何搜索关键字的几个句子。但我想要的是包含大多数搜索关键字的句子作为最佳答案。这将打印包含任何搜索关键字的几个句子。但我想要的是包含大多数搜索关键字的句子作为最佳答案。