Say I have the following list.
food_list = ['ice cream', 'apple', 'pancake', 'sushi']
And I want to find each item on that list on the following string.
my_str = 'I had pancake for breakfast this morning, while my sister ate some apples. I brought one apple and ate it on my way to work. My coworker was having his birthday today, and he gave us free ice cream. It was the best ice cream I had this year.'
my_str = my_str.lower()
I want to count the number of items in the string.
ice cream : 2, apple: 1, pancake: 1, sushi:0
Notice that apple is only counted once, because apples should not be counted. I cannot possibly split it by space, because of items like ice cream.
I was thinking of replacing the word in the list by something and count that later, but it's very slow (when applied to bigger data). And I wonder if there is better solution.
for word in food_list:
find_word = re.sub(r'\b'+word+r'\b', "***", my_str)
count_word = find_word.count("***")
print(word+": "+str(count_word))
I hope it's clear enough. Thanks