Toggle navigation
Toggle navigation
This project
Loading...
Sign in
박은주
/
Todays_Issue
Go to a project
Toggle navigation
Toggle navigation pinning
Projects
Groups
Snippets
Help
Project
Activity
Repository
Pipelines
Graphs
Issues
0
Merge Requests
0
Wiki
Snippets
Network
Create a new issue
Builds
Commits
Issue Boards
Authored by
박은주
2021-06-08 17:29:19 +0900
Browse Files
Options
Browse Files
Download
Email Patches
Plain Diff
Commit
d85c5377707a9caafa0264ad09ee4d0539fbe18d
d85c5377
1 parent
fb9060b1
analyzer sample
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
42 additions
and
0 deletions
SentimentAnalyzer.py
SentimentAnalyzer.py
0 → 100644
View file @
d85c537
import
argparse
from
google.cloud
import
language_v1
def
print_result
(
annotations
):
score
=
annotations
.
document_sentiment
.
score
magnitude
=
annotations
.
document_sentiment
.
magnitude
tot
=
0
positive
=
0
negative
=
0
neutral
=
0
for
index
,
sentence
in
enumerate
(
annotations
.
sentences
):
sentence_sentiment
=
sentence
.
sentiment
.
score
if
sentence_sentiment
>
0
:
positive
+=
1
elif
sentence_sentiment
<
0
:
negative
+=
1
else
:
neutral
+=
1
# print(
# "Sentence {} has a sentiment score of {}".format(index, sentence_sentiment)
# )
tot
+=
1
print
(
"Overall Sentiment: score of {} with magnitude of {}"
.
format
(
score
,
magnitude
)
)
return
tot
,
positive
,
negative
def
analyze
(
movie_review_filename
):
client
=
language_v1
.
LanguageServiceClient
()
with
open
(
movie_review_filename
,
"r"
,
encoding
=
'utf-8-sig'
)
as
review_file
:
# Instantiates a plain text document.
content
=
review_file
.
read
()
document
=
language_v1
.
Document
(
content
=
content
,
type_
=
language_v1
.
Document
.
Type
.
PLAIN_TEXT
)
annotations
=
client
.
analyze_sentiment
(
request
=
{
'document'
:
document
})
print_result
(
annotations
)
if
__name__
==
"__main__"
:
analyze
(
"data.txt"
)
\ No newline at end of file
Please
register
or
login
to post a comment