首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >寻找句子中的单词

寻找句子中的单词
EN

Stack Overflow用户
提问于 2020-06-30 03:34:21
回答 3查看 184关注 0票数 1

我想要一个Python函数,它可以从一个句子的输入和一个关键字列表中获得一个关键字。该函数将遍历关键字列表,并在给定关键字的句子中搜索。如果句子中有关键字,则函数将返回关键字。否则它将一事无成。

例如,我有一个清单"ai“、”机器学习“、”计算机科学“和一个句子”我的专业是计算机科学,我现在是一家商店“的零售商。如果该函数实现正确,它将输出关键字Computer Science。然而,目前,我的功能是输出计算机科学和ai (零售商也有ai在其中),因为我使用Python。有什么方法可以改善我的功能以获得更好的结果吗?

下面是我尝试的代码:

代码语言:javascript
复制
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)
EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2020-06-30 04:16:38

我认为最好的方法是使用regex,因为如果您只使用in,它将匹配,因为ai在单词retailer中。您可以在regex (\b)中使用单词边界。

代码语言:javascript
复制
>>> import re
>>> keywords = "|".join(keywords_list)
>>> re.findall(f"\\b({keywords})\\b", sentence)
['Computer Science']
票数 3
EN

Stack Overflow用户

发布于 2020-06-30 04:28:08

以下是你能做的事:

代码语言:javascript
复制
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)

输出:

代码语言:javascript
复制
['Computer Science']
票数 2
EN

Stack Overflow用户

发布于 2020-06-30 03:51:09

代码语言:javascript
复制
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)
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/62649873

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档