Function to calculate sentiment based on word-dictionary import nltk from nltk.tokenize import word_tokenize # Download the punkt tokenizer nltk.download('punkt') # Example lists of positive and negative words pos_words = {"good", "successful", "positive", "bright", "excellent", "great", "happy", "sunny"} neg_words = {"lockdown", "affected", "pandemic", "bad", "poor", "sad"} def calc_sentiment_based_on_word_dict(text): sentiment_score = 0 words = word_tokenize(text) for word in words: if word in pos_words: print('pos:', word) sentiment_score += 1 if word in neg_words: print('neg:', word) sentiment_score -= 1 return sentiment_score / len(words) # Text to analyze text = 'The weather is great and everyone is happy with the sunny day.' sentiment = calc_sentiment_based_on_word_dict(text) print('The sentiment score of this text is: {:.2f}'.format(sentiment))