Python 如何在两个近似单词列表之间进行匹配
我有下面的家长名单 父目录=Python 如何在两个近似单词列表之间进行匹配,python,Python,我有下面的家长名单 父目录=['AWS','GCP','ALIBABA','AZURE'] 输入是句子=用户要求AWS和GCP 我需要用parent_列表检查传入的输入,并将其放入列表中 预期输出为[AWS,GCP] 我的代码在下面,运行良好 [如果我在句子中,则我代表父母列表中的我] 现在我需要做一些近似匹配,假设句子=的用法是询问AliBab和gcp 您可以看到,AliBab与ALIBABA 预期的结果是['ALIBABA','GCP']尝试可能是这样的: types = ['AWS', '
['AWS','GCP','ALIBABA','AZURE']
输入是句子=用户要求AWS和GCP
我需要用parent_列表检查传入的输入,并将其放入列表中
预期输出为[AWS,GCP]
我的代码在下面,运行良好
[如果我在句子中,则我代表父母列表中的我]
现在我需要做一些近似匹配,假设句子=的用法是询问AliBab和gcp
您可以看到,AliBab
与ALIBABA
预期的结果是['ALIBABA','GCP']
尝试可能是这样的:
types = ['AWS', 'GCP', 'ALIBABA', 'AZURE']
sentence = 'The use is asking for AW and GCP or something'
result = []
for word in sentence.split():
for t in types:
if word.lower() in t.lower() or t.lower() in word.lower():
result.append(t)
print(result)
或具有列表理解能力:
result = [t for word in sentence.split()
for t in types
if word.lower() in t.lower() or t.lower() in word.lower()]
它看起来更干净,但有点复杂
对于超过1个delimeter,请使用:
import re
for word in re.split(' |,', sentence):
比如:
关于添加分隔符,“,”与“,”不同。取决于近似匹配的定义 如果
substring
是一个条件,则可以迭代句子和父列表中的单词,如果句子中的单词显示为父列表元素的子字符串,则返回匹配项
matches = [elt for elt in parent_list if any(word.lower() in elt.lower() for word in sentence.split())]
您可以使用
re.split()
在多个分隔符上拆分:
parent_list = ['AWS', 'GCP', 'ALIBABA', 'AZURE']
sentence = "The use is asking for AliBab and gcp"
import re
matches = [elt for elt in parent_list if any(word.lower() in elt.lower() or elt.lower() in word.lower() for word in re.split('[, ]', sentence))]
print(matches)
sentence = "The use is asking for AWS,GCP"
matches = [elt for elt in parent_list if any(word.lower() in elt.lower() or elt.lower() in word.lower() for word in re.split('[, ]', sentence))]
print(matches)
您可以这样做:
parent_list = ['AWS', 'GCP', 'ALIBABA', 'AZURE']
used_words = []
string = "The use is asking for AWS and GCP"
for word in parent_list:
if(word.lower() in string.lower()):
used_words.append(word)
print(used_words)
使用Levenshtein距离-如果我的输入是“用户请求AWS,GCP”,请查看一个快速问题。如果我的输入是“用户请求AWS,GCP”,请查看最新答案。如果我的输入是“用户请求AWS,GCP”,请查看最新答案。您可以使用
re.split()“用途是询问AWS,GCP”如何获得输出['AWS','GCP']@Nons,仍在工作!
parent_list = ['AWS', 'GCP', 'ALIBABA', 'AZURE']
used_words = []
string = "The use is asking for AWS and GCP"
for word in parent_list:
if(word.lower() in string.lower()):
used_words.append(word)
print(used_words)