Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
S
sentiment
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Deploy
Releases
Container registry
Model registry
Monitor
Incidents
Service Desk
Analyze
Value stream analytics
Contributor analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
GitLab community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Sam Neupane
sentiment
Commits
7e3eca38
Commit
7e3eca38
authored
8 years ago
by
Sam Neupane
Browse files
Options
Downloads
Patches
Plain Diff
hw
parent
ef084774
Branches
Branches containing commit
No related tags found
No related merge requests found
Changes
3
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
examples.py
+8
-0
8 additions, 0 deletions
examples.py
negative_test.txt
+11
-0
11 additions, 0 deletions
negative_test.txt
sentiment.py
+154
-2
154 additions, 2 deletions
sentiment.py
with
173 additions
and
2 deletions
examples.py
+
8
−
0
View file @
7e3eca38
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 diff is collapsed.
Click to expand it.
negative_test.txt
0 → 100644
+
11
−
0
View file @
7e3eca38
This
movie
has
a
very
bad
plot
and
terrible
ending
This diff is collapsed.
Click to expand it.
sentiment.py
+
154
−
2
View file @
7e3eca38
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__
'
:
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment