Add initial Sentiment Analysis Tutorial files for Cloud NL API#522
Conversation
Change includes tutorial code, test code, and test review files
|
Hey, whoa, please don't merge without review! I'm going to revert this - please open another PR. |
| @@ -0,0 +1 @@ | |||
| /Library/Frameworks/Python.framework/Versions/2.7/Python No newline at end of file | |||
There was a problem hiding this comment.
Can you remove the gcloud dir from the checkin?
| @@ -0,0 +1,4 @@ | |||
| What was Hollywood thinking with this movie! I hated, | |||
There was a problem hiding this comment.
Can you rename the sentiment dir to resources, for consistency with the other samples
| @@ -0,0 +1 @@ | |||
| SSByZWFsbHkgd2FudGVkIHRvIGxvdmUgJ0JsYWRlcnVubmVyJyBidXQgdWx0aW1hdGVseSBJIGNvdWxkbid0IGdldApteXNlbGYgdG8gYXBwcmVjaWF0ZSBpdCBmdWxseS4gSG93ZXZlciwgeW91IG1heSBsaWtlIGl0IGlmIHlvdSdyZSBpbnRvCnNjaWVuY2UgZmljdGlvbiwgZXNwZWNpYWxseSBpZiB5b3UncmUgaW50ZXJlc3RlZCBpbiB0aGUgcGhpbG9zb3BoaWNhbApleHBsb3JhdGlvbiBvZiB3aGF0IGl0IG1lYW5zIHRvIGJlIGh1bWFuIG9yIG1hY2hpbmUuIFNvbWUgb2YgdGhlIGdpem1vcwpsaWtlIHRoZSBmbHlpbmcgY2FycyBhbmQgdGhlIFZvdWlnaHQtS2FtcGZmIG1hY2hpbmUgKHdoaWNoIHNlZW1lZCB2ZXJ5CnN0ZWFtcHVuayksIHdlcmUgcXVpdGUgY29vbC4KCkkgZGlkIGZpbmQgdGhlIHBsb3QgcHJldHR5IHNsb3cgYW5kIGJ1dCB0aGUgZGlhbG9ndWUgYW5kIGFjdGlvbiBzZXF1ZW5jZXMKd2VyZSBnb29kLiBVbmxpa2UgbW9zdCBzY2llbmNlIGZpY3Rpb24gZmlsbXMsIHRoaXMgb25lIHdhcyBtb3N0bHkgcXVpZXQsIGFuZApub3QgYWxsIHRoYXQgbXVjaCBoYXBwZW5lZCwgZXhjZXB0IGR1cmluZyB0aGUgbGFzdCAxNSBtaW51dGVzLiBJIGRpZG4ndAp1bmRlcnN0YW5kIHdoeSBhIHVuaWNvcm4gd2FzIGluIHRoZSBtb3ZpZS4gVGhlIHZpc3VhbCBlZmZlY3RzIHdlcmUgZmFudGFzdGljLApob3dldmVyLCBhbmQgdGhlIG11c2ljYWwgc2NvcmUgYW5kIG92ZXJhbGwgbW9vZCB3YXMgcXVpdGUgaW50ZXJlc3RpbmcuCkEgZnV0dXJpc3QgTG9zIEFuZ2VsZXMgdGhhdCB3YXMgYm90aCBoaWdobHkgcG9saXNoZWQgYW5kIGFsc28gZmFsbGluZyBhcGFydApyZW1pbmRlZCBtZSBvZiAnT3V0bGFuZC4nIENlcnRhaW5seSwgdGhlIHN0eWxlIG9mIHRoZSBmaWxtIG1hZGUgdXAgZm9yCm1hbnkgb2YgaXRzIHBlZGFudGljIHBsb3QgaG9sZXMuCgpJZiB5b3Ugd2FudCB5b3VyIHNjaS1maSB0byBiZSBsYXNlcnMgYW5kIHNwYWNlc2hpcHMsICdCbGFkZXJ1bm5lcicgbWF5CmRpc2FwcG9pbnQgeW91LiBCdXQgaWYgeW91IHdhbnQgaXQgdG8gbWFrZSB5b3UgdGhpbmssIHRoaXMgbW92aWUgbWF5CmJlIHdvcnRoIHRoZSBtb25leS4KCg== | |||
There was a problem hiding this comment.
I think this file isn't necessary?
| http = httplib2.Http() | ||
| credentials.authorize(http) | ||
|
|
||
| service = discovery.build('language', 'v1beta1', http=http) |
There was a problem hiding this comment.
You can just do:
credentials = GoogleCredentials.get_application_default()
service = discovery.build('language', 'v1beta1', credentials=credentials)The python client lib will take care of adding the right scope, and authorizing the http object.
| polarity = response['documentSentiment']['polarity'] | ||
| magnitude = response['documentSentiment']['magnitude'] | ||
| except KeyError: | ||
| print("The response did not contain the expected fields.") |
There was a problem hiding this comment.
Just let the KeyError propagate up. Error handling doesn't really provide much educational value IMO..
|
|
||
|
|
||
| if __name__ == '__main__': | ||
| parser = argparse.ArgumentParser() |
There was a problem hiding this comment.
Add a module-level docstring and set that as the description. See http://go/python-sample-guide#argparse-section
| except KeyError: | ||
| print("The response did not contain the expected fields.") | ||
| print('Sentiment: polarity of %s with magnitude of %s' | ||
| % (polarity, magnitude)) |
There was a problem hiding this comment.
Prefer using the 'string {}'.format(arg) syntax
| polarity = response['documentSentiment']['polarity'] | ||
| magnitude = response['documentSentiment']['magnitude'] | ||
| except KeyError: | ||
| print("The response did not contain the expected fields.") |
There was a problem hiding this comment.
style: prefer single quotes over double quotes
Change includes tutorial code, test code, and test review files