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

hw

parent ef084774
Branches
No related tags found
No related merge requests found
from __future__ import print_function
from __future__ import division
from builtins import input
print(4)
print(4/5)
print(2223/223.5)
x = input()
print(x)
This
movie
has
a
very
bad
plot
and
terrible
ending
from __future__ import print_function, division from __future__ import print_function, division
from builtins import input from builtins import input
def main(): def main():
pass average = list()
reviews=list()
try:
with open('sentiment.txt') as s:
reviews = list(s)
print(len(reviews))
except OSError:
print('Does not exist')
sentiments={}
for review in reviews:
words = review.split()
for word in words:
if word != review[0]:
if word in sentiments.keys():
sentiments[word].append(int(review[0]))
else:
sentiments[word] = list()
sentiments[word].append(int(review[0]))
print(sentiments['arty'])
print(len(sentiments['arty']))
# 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)
y = None
while True:
print('Enter your choice: \n'
'1: Get the score of a word\n'
'2: Get the average score of words in a file (one word per line)\n'
'3: Find the highest/lowest scoring words in a file\n'
'4: Sort words from a file into positive.txt and negative.txt\n'
'5: Exit the program')
try:
y = int(input('Enter a number 1-5:'))
except ValueError:
print('Invalid input')
if y>5:
print ('Invalid input')
if y ==5:
print('Goodbye')
break
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)
if y==2 or y==3:
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))
if 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))
except IOError:
print('File could not be found')
if y==3:
rating = sum(sentiments[x])/len(sentiments[x])
highest_score = max(average)
lowest_score = min(average)
for file_word, rate in rating.items():
if rate == highest_score:
j = file_word
if rate == lowest_score:
y == 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=y, min=lowest_score))
if __name__ == '__main__': if __name__ == '__main__':
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment