Question: Python and Sentiment Analysis The following is my source code in Python. The purpose of the code is to count the total number of words

Python and Sentiment Analysis

The following is my source code in Python. The purpose of the code is to count the total number of words in a text file and to also count the number of positive and negative words separately. This is what I get after hitting run F5 on my computer:

Counter() Positive: 0.0 Negative: 0.0

import nltk.classify.util from nltk.classify import NaiveBayesClassifier from nltk.corpus import names

from itertools import chain from glob import glob from collections import Counter

def word_feats(words): return dict([(word, True) for word in words]) positive_vocab = [ 'awesome', 'outstanding', 'fantastic', 'terrific', 'good', 'nice', 'great', ':)' ] negative_vocab = [ 'bad', 'terrible','useless', 'hate', ':(' ] neutral_vocab = [ 'movie','the','sound','was','is','actors','did','know','words','not' ] positive_features = [(word_feats(pos), 'pos') for pos in positive_vocab] negative_features = [(word_feats(neg), 'neg') for neg in negative_vocab] neutral_features = [(word_feats(neu), 'neu') for neu in neutral_vocab] train_set = negative_features + positive_features + neutral_features classifier = NaiveBayesClassifier.train(train_set) # Predict neg = 0 pos = 0 readFile = open("why I chose you love poem milad.txt","r+").read()

lines = [line.lower() for line in readFile] with open('why I chose you love poem milad.txt', 'w') as out: out.writelines(sorted(lines))

lines = [line.split(' ') for line in readFile] with open('why I chose you love poem milad.txt', 'w') as out: out.writelines(sorted(lines))

for word in readFile.split(): classResult = classifier.classify(word_feats(word)) if classResult == 'neg': neg = neg + 1 if classResult == 'pos': pos = pos + 1 wordcount = 0

wordcount = Counter(readFile)

print(wordcount)

print('Positive: ' + str(float(pos))) print('Negative: ' + str(float(neg)))

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!