我想要一个Python函数,它可以从一个句子的输入和一个关键字列表中获得一个关键字。该函数将遍历关键字列表,并在给定关键字的句子中搜索。如果句子中有关键字,则函数将返回关键字。否则它将一事无成。
例如,我有一个清单"ai“、”机器学习“、”计算机科学“和一个句子”我的专业是计算机科学,我现在是一家商店“的零售商。如果该函数实现正确,它将输出关键字Computer Science。然而,目前,我的功能是输出计算机科学和ai (零售商也有ai在其中),因为我使用Python。有什么方法可以改善我的功能以获得更好的结果吗?
下面是我尝试的代码:
sentence = "My major is Computer Science, and I am currently working as a retailer for a store"
keywords_list = ["ai", "Machine Learning", "Computer Science"]
output_list = list()
for keyword in keywords_list:
if keyword in sentence:
output_list.append(keyword)发布于 2020-06-30 04:16:38
我认为最好的方法是使用regex,因为如果您只使用in,它将匹配,因为ai在单词retailer中。您可以在regex (\b)中使用单词边界。
>>> import re
>>> keywords = "|".join(keywords_list)
>>> re.findall(f"\\b({keywords})\\b", sentence)
['Computer Science']发布于 2020-06-30 04:28:08
以下是你能做的事:
sentence = "My major is Computer Science, and I am currently working as a retailer for a store"
keywords_list = ["ai", "Machine Learning", "Computer Science"]
alph = 'abcdefghijklmnopqrstuvwxyz'
output_list = list()
for keyword in keywords_list:
k = [''.join([w for w in key if w.lower() in alph]) for key in keyword.split()] # List of all the words in keyword, with all punctuations removed
s = [''.join([w for w in wor if w.lower() in alph]) for wor in sentence.split()] # List of all the words in sentence, with all punctuations removed
if any([k == s[i:len(k)+i] for i in range(len(s)-len(k))]): # If any of the lists in keyword equals to any slice of the sentence list
output_list.append(keyword) # That means that the keyword is in
print(output_list)输出:
['Computer Science']发布于 2020-06-30 03:51:09
sentence = input("sentence here: ").casefold()
keywords = ["ai", "Machine Learning", "Computer Science"]
# making all keywords lower case
for keyword in keywords:
keywords[keywords.index(keyword)] = keyword.casefold()
result= ""
#searching
for keyword in keywords:
if keyword in sentence:
result += "{} ".format(keyword)
if result == "":
result = None
print(result)https://stackoverflow.com/questions/62649873
复制相似问题