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
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Cale Wagner
sentiment
Commits
fc60d3ed
Commit
fc60d3ed
authored
8 years ago
by
Cale Wagner
Browse files
Options
Downloads
Patches
Plain Diff
Completed through step 20
parent
9596996a
No related branches found
No related tags found
No related merge requests found
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
negative_test.txt
+0
-0
0 additions, 0 deletions
negative_test.txt
sentiment.py
+65
-30
65 additions, 30 deletions
sentiment.py
with
65 additions
and
30 deletions
negative_test.txt
0 → 100644
+
0
−
0
View file @
fc60d3ed
This diff is collapsed.
Click to expand it.
sentiment.py
+
65
−
30
View file @
fc60d3ed
...
@@ -4,42 +4,77 @@ from builtins import input
...
@@ -4,42 +4,77 @@ from builtins import input
import
sys
import
sys
def
main
():
def
sentiment
():
print
(
'
Enter your choice:
\n
'
try
:
'
1: Get the score of a word
\n
'
file
=
open
(
'
sentiment.txt
'
).
read
()
'
2: Get the average score of words in a file (one word per line)
\n
'
lines
=
file
.
splitlines
()
'
3: Find the highest/lowest scoring words in a file
\n
'
i
=
input
(
"
Enter a word:
"
)
'
4: Sort words from a file into positive.txt and negative.txt
\n
'
count
=
0
'
5: Exit the program
\n
'
)
ratings
=
0
for
line
in
lines
:
words
=
line
.
split
(
"
"
)
for
word
in
words
:
if
(
i
==
word
):
count
+=
1
ratings
+=
float
(
words
[
0
])
if
count
==
0
:
print
(
"
The word
\"
"
+
i
+
"
\"
appears
"
+
str
(
count
)
+
"
times with an average sentiment of None.
"
)
else
:
print
(
"
The word
\"
"
+
i
+
"
\"
appears
"
+
str
(
count
)
+
"
times with an average sentiment of
"
+
str
(
ratings
/
float
(
count
))
+
"
.
"
)
choice
=
raw_input
(
'
Enter a number 1-5:
'
)
except
IOError
:
print
(
"
Cannot read from that file
"
)
pass
def
collection_average
():
file
=
open
(
'
negative_test.txt
'
).
read
()
lines
=
file
.
splitlines
()
i
=
input
(
"
Enter a filename:
"
)
count
=
0
ratings
=
0
for
line
in
lines
:
words
=
line
.
split
(
"
"
)
for
word
in
words
:
if
(
i
==
word
):
count
+=
1
ratings
+=
float
(
words
[
0
])
if
count
==
0
:
print
(
"
The word
\"
"
+
i
+
"
\"
appears
"
+
str
(
count
)
+
"
times with an average sentiment of None.
"
)
else
:
print
(
"
The word
\"
"
+
i
+
"
\"
appears
"
+
str
(
count
)
+
"
times with an average sentiment of
"
+
str
(
ratings
/
float
(
count
))
+
"
.
"
)
if
choice
==
5
:
print
(
'
Goodbye!
'
)
ans
=
[]
pass
sentiments
=
dict
()
total
=
0
def
main
():
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
"
)
choice
=
input
(
"
Enter a number 1-5:
"
)
try
:
try
:
with
open
(
'
sentiment.txt
'
,
'
r
'
)
as
f
:
if
choice
==
'
1
'
:
lines
=
f
.
read
().
splitlines
()
# split lines
sentiment
()
user_search_value
=
raw_input
(
'
Please enter a word:
'
)
elif
choice
==
'
2
'
:
for
line
in
f
.
readlines
():
collection_average
()
line
=
line
.
strip
().
lower
().
split
()
elif
choice
==
'
5
'
:
for
words
in
line
:
print
(
'
Goodbye!
'
)
if
words
.
find
(
user_search_value
):
sys
.
exit
(
0
)
total
+=
1
elif
choice
>
'
5
'
or
not
choice
>
'
0
'
:
print
(
total
)
raise
"
This input does not work!
"
for
l
in
lines
:
words
=
l
.
split
()
# split each string in line
ans
.
append
(
words
)
# appending to final ans
print
(
len
(
lines
))
var
=
'
The word
'
+
str
(
user_search_value
)
+
'
appears
'
+
str
(
total
)
+
'
times with an average sentiment of
'
+
str
(
sentiments
)
print
(
var
)
except
IOError
:
except
IOError
:
print
(
"
Error: can
\'
t find file or read data
"
)
print
(
'
That is not a valid input!
'
)
pass
if
__name__
==
'
__main__
'
:
if
__name__
==
'
__main__
'
:
main
()
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