Showing
1 changed file
with
10 additions
and
8 deletions
... | @@ -27,14 +27,16 @@ def GetResult(annotations): | ... | @@ -27,14 +27,16 @@ def GetResult(annotations): |
27 | 27 | ||
28 | def analyze(movie_review_filename): | 28 | def analyze(movie_review_filename): |
29 | client = language_v1.LanguageServiceClient() | 29 | client = language_v1.LanguageServiceClient() |
30 | - | 30 | + try: |
31 | - with open(movie_review_filename, "r", encoding='utf-8-sig') as review_file: | 31 | + with open(movie_review_filename, "r", encoding='utf-8-sig') as review_file: |
32 | - # Instantiates a plain text document. | 32 | + # Instantiates a plain text document. |
33 | - content = review_file.read() | 33 | + content = review_file.read() |
34 | - 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) |
35 | - annotations = client.analyze_sentiment(request={'document': document}) | 35 | + annotations = client.analyze_sentiment(request={'document': document}) |
36 | - | 36 | + |
37 | - return GetResult(annotations) | 37 | + return GetResult(annotations) |
38 | + except: | ||
39 | + return 0, 0, 0, 0 | ||
38 | 40 | ||
39 | def StartSentimentAnalysis(): | 41 | def StartSentimentAnalysis(): |
40 | return analyze("data.txt") | 42 | return analyze("data.txt") |
... | \ No newline at end of file | ... | \ No newline at end of file | ... | ... |
-
Please register or login to post a comment