11
11
# See the License for the specific language governing permissions and
12
12
# limitations under the License.
13
13
14
+ # [START sentiment_tutorial]
14
15
"""Demonstrates how to make a simple call to the Natural Language API."""
15
16
17
+ # [START sentiment_tutorial_import]
16
18
import argparse
17
19
18
20
from google .cloud import language
21
+ # [END sentiment_tutorial_import]
19
22
20
-
21
- def main (movie_review_filename ):
22
- """Run a sentiment analysis request on text within a passed filename."""
23
- language_client = language .Client ()
24
-
25
- with open (movie_review_filename , 'r' ) as review_file :
26
- # Instantiates a plain text document.
27
- document = language_client .document_from_html (review_file .read ())
28
-
29
- # Detects sentiment in the document.
30
- annotations = document .annotate_text (include_sentiment = True ,
31
- include_syntax = False , include_entities = False )
32
-
23
+ def print_result (annotations ):
33
24
score = annotations .sentiment .score
34
25
magnitude = annotations .sentiment .magnitude
35
26
@@ -47,6 +38,19 @@ def main(movie_review_filename):
47
38
return 0
48
39
49
40
41
+ def analyze (movie_review_filename ):
42
+ """Run a sentiment analysis request on text within a passed filename."""
43
+ language_client = language .Client ()
44
+
45
+ with open (movie_review_filename , 'r' ) as review_file :
46
+ # Instantiates a plain text document.
47
+ document = language_client .document_from_html (review_file .read ())
48
+
49
+ # Detects sentiment in the document.
50
+ return document .annotate_text (include_sentiment = True ,
51
+ include_syntax = False , include_entities = False )
52
+
53
+
50
54
if __name__ == '__main__' :
51
55
parser = argparse .ArgumentParser (
52
56
description = __doc__ ,
@@ -55,4 +59,8 @@ def main(movie_review_filename):
55
59
'movie_review_filename' ,
56
60
help = 'The filename of the movie review you\' d like to analyze.' )
57
61
args = parser .parse_args ()
58
- main (args .movie_review_filename )
62
+ # Perform the analysis
63
+ annotations = analyze (args .movie_review_filename )
64
+ # Print the results
65
+ print_result (annotations )
66
+ # [END sentiment_tutorial]
0 commit comments