'Text matching not working, not working with "Or" condition

I used the following code for text matching. But not getting the output. Showing no error but getting no for each condition.

for i in range(len(df['content'])):

if ('healthy|light|health|helthy') in str(df['content'][i]).lower() and 'breakfast' in str(df['content'][i]).lower() and ('vitamin|vitamins|nutrition|nutritions|minerals|mineral|fibre|gluten|diet|choice|cereals|nutritious|ragi|jowar|wheat|digestable|calories|calory|weight loss') in str(df['content'][i]).lower():
    list1.append("yes")
        
else:
    list1.append("no")

df['Health benefits']=list1

print(df)



Solution 1:[1]

I think there are several errors in this code,

  • The first one is: You are iterating on a dict, but i don't know if the df['content'] is a string or a list, if it's a string you are iterating the string per letter.
  • the second one is: i don't know if it's a copy past error but the code up is not well indented.
  • I don't know if you can use ('word1|word2') check in python i tested it in local and it didn't work, you can do something like that:
ztring = "zio was eating an apple"

if ("zio|pear") in ztring:
    print("this won't work")

words1 = ["zio", "pear"]

for word in words1:
    if word in ztring:
        print("now it's ok")

So i think that you can do a better work using a function like that:

def check_if_words_in_string(word_list: list,phrase: str) -> bool:
    for word in word_list:
        if word in phrase:
            return True
    return False

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Skrullwar