Skip to content
GitLab
Menu
Projects
Groups
Snippets
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
Christopher Alan Lif
sentiment
Commits
489d8a48
Commit
489d8a48
authored
Feb 20, 2017
by
Christopher Alan Lif
Browse files
Organized code and added comments
parent
804f83f0
Changes
2
Hide whitespace changes
Inline
Side-by-side
sentiment.py
View file @
489d8a48
...
...
@@ -62,16 +62,27 @@ def main():
'4: Sort words from a file into positive.txt and negative.txt
\n
'
'5: Exit the program'
)
selection
=
verify_input_for_selection
()
if
selection
==
1
:
search_for_numbers
(
rating_numbers
)
elif
selection
==
2
:
get_average_for_file
(
rating_numbers
)
elif
selection
==
3
:
get_highest_and_lowest_words
(
rating_numbers
)
elif
selection
==
4
:
sort_words_into_file
(
rating_numbers
)
elif
selection
==
5
:
end_program
()
get_input
(
selection
,
rating_numbers
)
def
get_input
(
selection
,
rating_numbers
):
output
=
''
if
selection
==
1
:
search
=
input
(
'Enter a word to search for: '
)
output
=
search_for_numbers
(
rating_numbers
,
search
)
elif
selection
==
2
:
search
=
input
(
"Enter a file name to search for: "
)
output
=
get_average_for_file
(
rating_numbers
,
search
)
elif
selection
==
3
:
search
=
input
(
"Enter a file name to search for: "
)
output
=
get_highest_and_lowest_words
(
rating_numbers
,
search
)
elif
selection
==
4
:
search
=
input
(
"Enter a file name to search for: "
)
sort_words_into_file
(
rating_numbers
,
search
)
output
=
'Files created and written to.'
elif
selection
==
5
:
end_program
()
print_sentence
(
output
)
# Verify proper input of an int 1-5
...
...
@@ -108,22 +119,20 @@ def get_amount(word, rating_numbers):
# Selection 1: Get the score of a word
def
search_for_numbers
(
rating_numbers
):
search
=
input
(
'Enter a word to search for: '
)
def
search_for_numbers
(
rating_numbers
,
search
):
amount
=
get_amount
(
search
,
rating_numbers
)
average
=
get_average
(
search
,
rating_numbers
)
print
(
'The word "'
+
str
(
search
)
+
'" appears '
+
str
(
amount
)
+
' times, with an average '
'sentiment of '
+
str
(
average
)
+
'.'
)
sentence
=
'The word "'
+
str
(
search
)
+
'" appears '
+
str
(
amount
)
+
\
' times, with an average sentiment of '
+
str
(
average
)
+
'.'
return
sentence
# Selection 2: Get the average score of words in a file (one word per line)
def
get_average_for_file
(
rating_numbers
):
def
get_average_for_file
(
rating_numbers
,
search
):
valid_entry
=
False
search_list
=
[]
search
=
''
while
not
valid_entry
:
try
:
search
=
input
(
"Enter a file name to search for: "
)
with
open
(
search
)
as
data
:
search_list
=
data
.
read
().
splitlines
()
valid_entry
=
True
...
...
@@ -145,21 +154,21 @@ def get_average_for_file(rating_numbers):
summary
=
"negative"
else
:
summary
=
"neutral"
print
(
"The overall sentiment of "
+
search
+
" is "
+
summary
+
"."
)
output
=
(
"The overall sentiment of "
+
search
+
" is "
+
summary
+
"."
)
return
output
else
:
print
(
"None of the words in this file are documented, sorry!"
)
print
()
output
=
"None of the words in this file are documented, sorry!"
return
output
# Selection 3: Find the highest/lowest scoring words in a file
def
get_highest_and_lowest_words
(
rating_numbers
):
def
get_highest_and_lowest_words
(
rating_numbers
,
search
):
valid_entry
=
False
search_list
=
[]
highest_rating
=
''
lowest_rating
=
''
while
not
valid_entry
:
try
:
search
=
input
(
"Enter a file name to search for: "
)
with
open
(
search
)
as
data
:
search_list
=
data
.
read
().
splitlines
()
valid_entry
=
True
...
...
@@ -183,20 +192,19 @@ def get_highest_and_lowest_words(rating_numbers):
lowest_rating
=
min_max_list
[
-
1
]
except
IndexError
:
print
(
"None of the words in this file are documented, sorry!"
)
print
(
'The most positive word is "'
+
highest_rating
+
'" with a score of '
+
str
(
get_average
(
highest_rating
,
rating_numbers
))
+
'.
'
)
print
(
'The most negative word is "'
+
lowest_rating
+
'" with a score of '
+
str
(
get_average
(
lowest_rating
,
rating_numbers
))
+
"."
)
print
()
output
=
'The most positive word is "'
+
highest_rating
+
\
'" with a score of '
+
str
(
get_average
(
highest_rating
,
rating_numbers
))
+
'.
\n
'
+
\
'The most negative word is "'
+
lowest_rating
+
\
'" with a score of '
+
str
(
get_average
(
lowest_rating
,
rating_numbers
))
+
".
\n
"
return
output
# Selection 4: Sort words from a file into positive.txt and negative.txt
def
sort_words_into_file
(
rating_numbers
):
def
sort_words_into_file
(
rating_numbers
,
search
):
valid_entry
=
False
search_list
=
[]
while
not
valid_entry
:
try
:
search
=
input
(
"Enter a file name to search for: "
)
with
open
(
search
)
as
data
:
search_list
=
data
.
read
().
splitlines
()
valid_entry
=
True
...
...
@@ -218,6 +226,10 @@ def sort_words_into_file(rating_numbers):
print
()
def
print_sentence
(
sentence
):
print
(
sentence
)
# Selection 5: Exit the program
def
end_program
():
print
(
'Thank you, for using the sentiment program!'
)
...
...
testSentiment.py
View file @
489d8a48
...
...
@@ -63,3 +63,11 @@ class Test(TestCase):
expected
=
0
actual
=
sentiment
.
get_amount
(
'scramble'
,
dict_2
)
self
.
assertEquals
(
expected
,
actual
)
def
test_search_for_numbers_1
(
self
):
dict_1
=
dict
()
dict_2
=
dict
()
sentiment
.
setup
(
dict_1
,
dict_2
)
actual
=
sentiment
.
search_for_numbers
(
dict_2
,
'arty'
)
expected
=
'The word "arty" appears 4 times, with an average sentiment of 1.25.'
self
.
assertEquals
(
expected
,
actual
)
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment