Issue
I have list of strings say,
x1 = ['esk','wild man','eskimo', 'sta','(+)-6-[amina(4-chlora)(1-metha-1h-imidol-5-yl)mhyl]-4-(3-chlora)-1-methyl-2(1h)-quinoa']
I need to extract the x1s present in few sentences.
My sentence is "eskimo lives as a wild man in wild jungle and he stands as a guard".
In the sentence, I need to extract first word eskimo and the seventh and eighth words wild man and they are separate words as in x1. I should not extract "stands" even though sta is present in stands.
def get_name(input_str):
prod_name= []
for row in x1:
if (row.strip().lower()in input_str.lower().strip()) or (len([x for x in input_str.split() if "\b"+x in row])>0):
prod_name.append(row)
return list(set(prod_name))
The function
get_name("eskimo lives as a wild man in wild jungle and he stands as a guard")
returns
[esk, eskimo,wild man,sta]
But the expected is
[eskimo,wild man]
May I know what has to be changed in the code?
Solution
I have a slightly different approach. Firstly you could split the input sentence into words and also split each of the phrases you want to check for into constituent words. Then check if each of all words of a phrase are present in the sentence.
x1 = ['esk','wild man','eskimo', 'sta','(+)-6-[amina(4-chlora)(1-metha-1h-imidol-5-yl)mhyl]-4-(3-chlora)-1-methyl-2(1h)-quinoa']
input_sentence = "eskimo lives as a wild man in wild jungle and he stands as a guard"
# Remove all punctuation marks from the sentence
input_sentence = input_sentence.replace('!', '').replace('.', '').replace('?', '').replace(',', '')
# Split the input sentence into its component words to check individually
input_words = input_sentence.split()
for ele in x1:
# Split each element in x1 into words
ele_words = ele.split()
# Check if all words are part of the input words
if all(ele in input_words for ele in ele_words) and ele in input_sentence:
print(ele)
Answered By - Praneeth Jain
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.