Text Analysis-Affective analysis
Natural language Processing (NLP)
• Translating natural Language (text) into a form that is easier to understand by computer programs
• Preprocessing-derived string-> to quantify simple emotional analysis
Construct an emotional dictionary by oneself construct a dictionary, as
Like-> 1, good-> 2, Bad->-1, terrible-2 based on keyword matching
Problem:
Encounter new words, special words, etc., poor extensibility using machine learning model, nltk.classify
Import NLTK from Nltk.stem import Wordnetlemmatizer to Nltk.corpus import stopwords from nltk.classify import Naivebayes Classifier Text1 = ' I like the movie so much! ' Text2 = ' It's a good movie. ' Text3 = ' This is a great one. ' Text4 = ' Th
At are a really bad movie. ' Text5 = ' that is a terrible movie. ' def proc_text (text): "" Pre-processing text "" "# participle raw_words = nltk.word_tokenize (text) # Forms normalized WOR
Dnet_lematizer = Wordnetlemmatizer () words = [Wordnet_lematizer.lemmatize (Raw_word) for Raw_word in Raw_words] # Remove Deactivate word filtered_words = [word for word in words if Word not in stopwords.words (' Chinese ')] # True indicates that the word is in the text, in order to make
Use the classifier in NLTK to return {word:true to Word in Filtered_words} # to construct a training sample train_data = [[Proc_text (Text1), 1], [Proc_text (Text2), 1], [Proc_text (Text3), 1], [Proc_text (TEXT4), 0], [proc_text (TEXT5), 0]] print (train_data) "" [[{' I]: True, ' much ': true, ' movie ': true, ' LiKe ': True, '! ': true}, 1], [{' Good ': true, '. ': true, ' that ': true, ' movie ': true}, 1], [{' Here ': true, ' great ': true, ' on E ': true, '. ': true}, 1], [{' Really ': true, '. ': true, ' that ': true, ' movie ': true, ' bad ': true}, 0], [{' This ': true, ' ter Rible ': true, '. ': true, ' movie ': True}, 0]] "" "# Training Model Nb_model = Naivebayesclassifier.train (train_data) # test Model TEXT6 = ' That's a bad one. ' Print (Nb_model.classify (Proc_text (TEXT5)) #0