-
Notifications
You must be signed in to change notification settings - Fork 6.6k
Add initial Sentiment Analysis Tutorial files for Cloud NL API #522
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
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 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
style: prefer single quotes over double quotes
Change includes tutorial code, test code, and test review files