1111# See the License for the specific language governing permissions and
1212# limitations under the License.
1313
14+ # [START sentiment_tutorial]
1415"""Demonstrates how to make a simple call to the Natural Language API."""
1516
17+ # [START sentiment_tutorial_import]
1618import argparse
1719
1820from google .cloud import language
21+ # [END sentiment_tutorial_import]
1922
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 ):
3324 score = annotations .sentiment .score
3425 magnitude = annotations .sentiment .magnitude
3526
@@ -47,6 +38,19 @@ def main(movie_review_filename):
4738 return 0
4839
4940
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+
5054if __name__ == '__main__' :
5155 parser = argparse .ArgumentParser (
5256 description = __doc__ ,
@@ -55,4 +59,8 @@ def main(movie_review_filename):
5559 'movie_review_filename' ,
5660 help = 'The filename of the movie review you\' d like to analyze.' )
5761 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