Showing
1 changed file
with
9 additions
and
11 deletions
1 | import argparse | 1 | import argparse |
2 | +import os | ||
2 | 3 | ||
3 | from google.cloud import language_v1 | 4 | from google.cloud import language_v1 |
4 | 5 | ||
5 | -def print_result(annotations): | 6 | +BASE_DIR = os.path.dirname(os.path.realpath(__file__)) |
7 | +os.environ["GOOGLE_APPLICATION_CREDENTIALS"] = BASE_DIR + "/artful-fortress-316201-f135fd520d56.json" | ||
8 | + | ||
9 | +def GetResult(annotations): | ||
6 | score = annotations.document_sentiment.score | 10 | score = annotations.document_sentiment.score |
7 | magnitude = annotations.document_sentiment.magnitude | 11 | magnitude = annotations.document_sentiment.magnitude |
8 | 12 | ||
... | @@ -18,14 +22,8 @@ def print_result(annotations): | ... | @@ -18,14 +22,8 @@ def print_result(annotations): |
18 | negative += 1 | 22 | negative += 1 |
19 | else: | 23 | else: |
20 | neutral += 1 | 24 | neutral += 1 |
21 | - # print( | ||
22 | - # "Sentence {} has a sentiment score of {}".format(index, sentence_sentiment) | ||
23 | - # ) | ||
24 | tot += 1 | 25 | tot += 1 |
25 | - print( | 26 | + return tot, positive, neutral, negative |
26 | - "Overall Sentiment: score of {} with magnitude of {}".format(score, magnitude) | ||
27 | - ) | ||
28 | - return tot, positive, negative | ||
29 | 27 | ||
30 | def analyze(movie_review_filename): | 28 | def analyze(movie_review_filename): |
31 | client = language_v1.LanguageServiceClient() | 29 | client = language_v1.LanguageServiceClient() |
... | @@ -36,7 +34,7 @@ def analyze(movie_review_filename): | ... | @@ -36,7 +34,7 @@ def analyze(movie_review_filename): |
36 | document = language_v1.Document(content=content, type_=language_v1.Document.Type.PLAIN_TEXT) | 34 | document = language_v1.Document(content=content, type_=language_v1.Document.Type.PLAIN_TEXT) |
37 | annotations = client.analyze_sentiment(request={'document': document}) | 35 | annotations = client.analyze_sentiment(request={'document': document}) |
38 | 36 | ||
39 | - print_result(annotations) | 37 | + return GetResult(annotations) |
40 | 38 | ||
41 | -if __name__ == "__main__": | ||
42 | - analyze("data.txt") | ||
... | \ No newline at end of file | ... | \ No newline at end of file |
39 | +def StartSentimentAnalysis(): | ||
40 | + return analyze("data.txt") | ||
... | \ No newline at end of file | ... | \ No newline at end of file | ... | ... |
-
Please register or login to post a comment