简体   繁体   中英

How to match between 2 list which is approximate word

I have parent list below

parent_list = ['AWS', 'GCP', 'ALIBABA', 'AZURE']

The incoming input is sentence = The use is asking for AWS and GCP

I need to check the incoming input with parent_list and put in the list

Expected out is [AWS, GCP]

My code is below which is working fine

[i for i in parent_list if i in sentence ]

Now I need to do some approximate match let's say if sentence = The use is asking for AliBab and gcp

You can see that AliBab is approximate to ALIBABA

Expected out is ['ALIBABA', 'GCP']

Try might be this:

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)

or with list comprehension:

result = [t for word in sentence.split()
           for t in types
           if word.lower() in t.lower() or t.lower() in word.lower()]

it looks cleaner, but bit complicated

for more than 1 delimeter, use:

import re
for word in re.split(' |,', sentence):

like:

result = [t for word in re.split(' |,', sentence)
           for t in types
           if word.lower() in t.lower() or t.lower() in word.lower()]

about adding delimiter, ',' is different one from ', '

Depends on the definition of approximation match.

If substring is a criteria then you can iterate over the words of the sentence and parent list and return matches if the word of the sentence appear as a substring of the element of the parent list.

matches = [elt for elt in parent_list if any(word.lower() in elt.lower() for word in sentence.split())]

You can use re.split() to split on multiple delimiters:

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)

You can do this:

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM