Skip to content

Commit

Permalink
New Functions Natural Language API sample
Browse files Browse the repository at this point in the history
  • Loading branch information
ryanmats committed Oct 20, 2019
1 parent d68128e commit ea48dd7
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 0 deletions.
28 changes: 28 additions & 0 deletions functions-language/language.py
@@ -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!'
3 changes: 3 additions & 0 deletions functions-language/requirements.txt
@@ -0,0 +1,3 @@
# Function dependencies, for example:
# package>=version
google-cloud-language

0 comments on commit ea48dd7

Please sign in to comment.