def get_wordnet_pos(treebank_tag): if treebank_tag.startswith('J'): return wordnet.ADJ elif treebank_tag.startswith('V'): return wordnet.VERB elif treebank_tag.startswith('N'): return wordnet.NOUN elif treebank_tag.startswith('R'): return wordnet.ADV else: return wordnet.NOUN lemmatizer = WordNetLemmatizer() def preprocess_text(text): text = text.lower() # lowercase text tokens = word_tokenize(text) # tokenize filtered_words = [word for word in tokens if word.lower() not in stopword_list] lemmatized_words = [lemmatizer.lemmatize(w, get_wordnet_pos(w)) for w in filtered_words] lemmatized_clean = [word.translate(str.maketrans('', '', string.punctuation)) for word in lemmatized_words] return ' '.join(lemmatized_clean) def text_model(df): df['preprocessed_text'] = df['Comment'].apply(preprocess_text) return df['preprocessed_text'] def prediction(model, X): y_pred = model.predict(X) predictions = np.argmax(y_pred, axis=1) for index, val in enumerate(predictions): if val == 0: print(f"Text {index} indicates the person is feeling FEAR") elif val == 1: print(f"Text {index} indicates the person is feeling ANGER") else: print(f"Text {index} indicates the person is feeling JOY")