Showing
1 changed file
with
19 additions
and
12 deletions
... | @@ -23,20 +23,27 @@ def GetResult(annotations): | ... | @@ -23,20 +23,27 @@ def GetResult(annotations): |
23 | else: | 23 | else: |
24 | neutral += 1 | 24 | neutral += 1 |
25 | tot += 1 | 25 | tot += 1 |
26 | - return tot, positive, neutral, negative | ||
27 | 26 | ||
28 | -def analyze(movie_review_filename): | 27 | + with open(BASE_DIR + 'sentiment.txt', 'w', encoding='utf-8-sig') as txt_file: |
28 | + txt_file.write(str(tot)) | ||
29 | + txt_file.write(str(int(positive / tot * 100))) | ||
30 | + txt_file.write(str(int(neutral / tot * 100))) | ||
31 | + txt_file.write(str(int(negative / tot * 100))) | ||
32 | + print("txt file saved") | ||
33 | + # return tot, positive, neutral, negative | ||
34 | + | ||
35 | +def analyze(filename): | ||
29 | client = language_v1.LanguageServiceClient() | 36 | client = language_v1.LanguageServiceClient() |
30 | - try: | 37 | + # try: |
31 | - with open(movie_review_filename, "r", encoding='utf-8-sig') as review_file: | 38 | + with open(filename, "r", encoding='utf-8-sig') as file: |
32 | - # Instantiates a plain text document. | 39 | + # Instantiates a plain text document. |
33 | - content = review_file.read() | 40 | + content = file.read() |
34 | - document = language_v1.Document(content=content, type_=language_v1.Document.Type.PLAIN_TEXT) | 41 | + document = language_v1.Document(content=content, type_=language_v1.Document.Type.PLAIN_TEXT) |
35 | - annotations = client.analyze_sentiment(request={'document': document}) | 42 | + annotations = client.analyze_sentiment(request={'document': document}) |
36 | 43 | ||
37 | - return GetResult(annotations) | 44 | + GetResult(annotations) |
38 | - except: | 45 | + # except: |
39 | - return 0, 0, 0, 0 | 46 | + # return 0, 0, 0, 0 |
40 | 47 | ||
41 | def StartSentimentAnalysis(): | 48 | def StartSentimentAnalysis(): |
42 | - return analyze("data.txt") | ||
... | \ No newline at end of file | ... | \ No newline at end of file |
49 | + analyze("data.txt") | ||
... | \ No newline at end of file | ... | \ No newline at end of file | ... | ... |
-
Please register or login to post a comment