Home > Back-end >  How to find is an element in dictionary is in list (Python)
How to find is an element in dictionary is in list (Python)

Time:05-29

I am making a program which determines the tone of a text that the user inputs. I have the user input some text, which is split and converted to a list after all punctuation is removed:

import re
text_y_punct = input("Enter your text: ").lower().strip()
text_n_punct = re.sub(r'[^\w\s]','',text_y_punct)
text_list = text_n_punct.split(" ")

And I also have a dictionary of tones and their severeness:

anger_tones = {"I am angry": 10, "We are angry": 10, "You make me angry": 10, "You're making me angry": 10}

How do I determine if any phrase from anger_tones is found within text_list?

CodePudding user response:

You can't do an approach like this to solve your problem.

If you want to do it properly then you are going to have to look up sentiment analysis using python.

And it is a somewhat advanced topic. Good luck. For simple approach you can use something like this:

text_y_punct = input("Enter your text: ").lower().strip()
sentences_dict = {'i am not very happy': 5, 'i am fuming': 9}
for sentence, rating in sentences_dict.items():
  if sentence in text_y_punct:
    print(text_y_punct   ": "   str(rating))
  • Related