Search code examples
pythonsentiment-analysispos-taggersenti-wordnet

sentiment analysis in python


I am extracting reviews from various website and storing them in a file and then classifying each sentence as positive or negative with the help of senti-wordnet ( which gives certain scores).I am using python 2.7. I don't know how it works for reviews stored in a file. Do anybody know the code in python for this ?

f1=open("foodP.txt","r")
word_features =[]
words = []

for line in f1:
    word_features.append(line)
    s=str(word_features)
    tokens=nltk.word_tokenize(s)    

for i,j in nltk.pos_tag(tokens):
    if j in ['VBN','VBP','VB','JJ', 'JJR', 'JJS', 'RB', 'RBR', 'RBS']:
        words.append(i)
print words

this code will give only adjective , adverbs and verbs i need. i want to classify these words as either positive or negative .


Solution

  • You don't need POS for sentiment analysis, at least it's not required. Prepare feature by using bag_of_words in X and "neg"/"pos" as Y. Then split into train/test sets and apply classification algorithm - NaiveBayes, MaxEnt, RandomForest, SVM.