Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
New Functions Natural Language API sample
- Loading branch information
Showing
2 changed files
with
31 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
from google.cloud import language | ||
from google.cloud.language import types | ||
from google.cloud.language import enums | ||
|
||
def hello_world(request): | ||
"""Responds to any HTTP request. | ||
Args: | ||
request (flask.Request): HTTP request object. | ||
Returns: | ||
The response text or any set of values that can be turned into a | ||
Response object using | ||
`make_response <http://flask.pocoo.org/docs/1.0/api/#flask.Flask.make_response>`. | ||
""" | ||
request_json = request.get_json() | ||
if request.args and 'message' in request.args: | ||
client = language.LanguageServiceClient() | ||
text = request.args.get('message') | ||
document = types.Document( | ||
content=text, | ||
type=enums.Document.Type.PLAIN_TEXT) | ||
sentiment = client.analyze_sentiment(document).document_sentiment | ||
score = sentiment.score | ||
magnitude = sentiment.magnitude | ||
return str(score*magnitude) | ||
elif request_json and 'message' in request_json: | ||
return request_json['message'] | ||
else: | ||
return f'Hello World!' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
# Function dependencies, for example: | ||
# package>=version | ||
google-cloud-language |