Skip to content
Snippets Groups Projects
Commit a0ed7156 authored by Sam Neupane's avatar Sam Neupane
Browse files

hw

parent 767f8142
Branches master
No related tags found
No related merge requests found
from unittest import TestCase
from sentiment import method_11
from sentiment import first_input13
class Unit_test(TestCase):
def test_method_11(self):
with open('sentiment.txt') as s:
reviews = list(s)
sentiments = {}
self.assertEqual(method_11(reviews,sentiments)['arty'], [3,1,1,0])
def test_first_Input13(self):
sentiments = {}
y=input()
self.assertEqual(first_input13(sentiments,y)['Invalid input'])
from __future__ import print_function, division
from builtins import input
import numpy as np
def main():
reviews = list()
try:
with open('sentiment.txt') as s:
reviews = list(s)
# print(len(reviews))
except OSError:
print('Does not exist')
sentiments = {}
......@@ -25,7 +27,7 @@ def main():
# y=x, num=0, av='None'
# )
# print(output)
y = None
#y = None
while True:
print('Enter your choice: \n'
......@@ -45,67 +47,98 @@ def main():
if y == 5:
print('Goodbye')
break
if y == 1:
x = input("Enter a word")
if x in sentiments:
first_input13(sentiments, y)
output = 'The word "{y}" appears "{num}" times with an average sentiment of {av}. '.format(
y=x, num=len(sentiments[x]), av=sum(sentiments[x]) / len(sentiments[x]))
else:
output = 'The word "{y}" appears "{num}" times with an average sentiment of {av}. '.format(
y=x, num=0, av='None'
)
print(output)
average = list()
if y == 2 or y == 3:
if y == 2 or y == 3 or y==4:
average = list()
file = input('Enter a file')
try:
with open(file) as file_words:
user_file = file_words.read().split()
for word_in_file in user_file:
if word_in_file in sentiments.keys():
average.append(sum(sentiments[word]) / len(sentiments[word]))
if y == 2:
average.append(sum(sentiments[word_in_file]) / len(sentiments[word_in_file]))
if not average:
print('The average score of words in {y} is None '.format(y=file))
else:
average_words = sum(average) / len(average)
if average_words >= 2.01:
print('The average score of words in {y} is {av}'.format(y=file, av=average_words))
print('The overall sentiment of {y} is positive'.format(y=file))
elif average_words <= 1.99:
print('The average score of words in {y} is {av}'.format(y=file, av=average_words))
print('The overall sentiment of {y} is negative'.format(y=file))
else:
print('The average score of words in {y} is {av}'.format(y=file, av=average_words))
print('The overall sentiment of {y} is neutral'.format(y=file))
if y == 3:
# rating = sum(sentiments[x])/len(sentiments[x])
split_file = file.split()
#
highest_score = max(average)
lowest_score = min(average)
p = average.append(sum_word)
for check_word in split_file:
if check_word in sentiments:
method_11(check_word)
sum_word = sum(method_11(check_word)) / len(method_11(check_word))
for file_word, rate in sum_word.items():
if rate == highest_score:
j = file_word
if rate == lowest_score:
j == file_word
print('The most positive word is {phrase} with a score of {j}'.format(phrase=j, j=highest_score))
print('The most negative word is {phrase} with a score of {min}'.format(phrase=j, min=lowest_score))
except IOError:
raise
print('File could not be found')
for word_in_file in user_file:
if word_in_file in sentiments.keys():
average.append(sum(sentiments[word]) / len(sentiments[word]))
if y == 2:
average.append(sum(sentiments[word_in_file]) / len(sentiments[word_in_file]))
if not average:
print('The average score of words in {y} is None '.format(y=file))
else:
one_word_average = sum(average) / len(average)
if one_word_average >= 2.01:
print('The average score of words in {y} is {av}'.format(y=file, av=one_word_average))
print('The overall sentiment of {y} is positive'.format(y=file))
elif one_word_average <= 1.99:
print('The average score of words in {y} is {av}'.format(y=file, av=one_word_average))
print('The overall sentiment of {y} is negative'.format(y=file))
else:
print('The average score of words in {y} is {av}'.format(y=file, av=one_word_average))
print('The overall sentiment of {y} is neutral'.format(y=file))
if y == 3:
max_score = None
min_score = None
max_word = None
min_Word = None
number = 0
score_of_one_word = sum(sentiments[word_in_file]) / len(sentiments[word_in_file])
for word_in_file in user_file:
if word_in_file in sentiments.keys():
if max_score is None:
max_score = score_of_one_word
max_word = word_in_file
if score_of_one_word> max_score:
max_score = score_of_one_word
max_word = word_in_file
if min_score is None:
min_score = score_of_one_word
min_Word = word_in_file
if score_of_one_word < min_score:
min_score = word_in_file
min_Word = word_in_file
print('The most positive word is {phrase} with a score of {max}'.format(phrase=max_word, max=max_score))
print('The most negative word is {phrase} with a score of {min}'.format(phrase=min_Word, min=min_score))
if y == 4:
lowest_scores= []
largest_scores =[]
score_of_one_word = sum(sentiments[word_in_file]) / len(sentiments[word_in_file])
for word_in_file in user_file:
if word_in_file in sentiments.keys():
if score_of_one_word <= 1.99:
lowest_scores.append(word_in_file)
elif one_word_average>= 2.01:
largest_scores.append(word_in_file)
lowest_scores = str([lowest_scores])
largest_scores = str([largest_scores])
with open('negative.txt', 'w') as negative_file:
negative_file.write(lowest_scores)
with open('positive.txt', 'w') as positive_file:
positive_file.write(largest_scores)
def first_input13(sentiments, y):
if y == 1:
x = input("Enter a word")
if x in sentiments:
output = 'The word "{y}" appears "{num}" times with an average sentiment of {av}. '.format(
y=x, num=len(sentiments[x]), av=sum(sentiments[x]) / len(sentiments[x]))
else:
output = 'The word "{y}" appears "{num}" times with an average sentiment of {av}. '.format(
y=x, num=0, av='None'
)
print(output)
def method_11(reviews, sentiments):
......
from unittest import TestCase
from sentiment import method_11
class TestMethod_11(TestCase):
def test_method_11(self):
reviews = ['John', "jack "]
sentiments = [2,2]
self.assertEqual(method_11(reviews,sentiments)[3,0,1,1], ['arty'])
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment