-
Notifications
You must be signed in to change notification settings - Fork 6.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #8533 from GoogleCloudPlatform/python-language-mig…
…ration migrate code from googleapis/python-language
- Loading branch information
Showing
76 changed files
with
3,163 additions
and
0 deletions.
There are no files selected for viewing
Validating CODEOWNERS rules …
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
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
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
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 @@ | ||
See https://github.com/GoogleCloudPlatform/python-docs-samples/blob/main/AUTHORING_GUIDE.md |
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 @@ | ||
See https://github.com/GoogleCloudPlatform/python-docs-samples/blob/main/CONTRIBUTING.md |
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,15 @@ | ||
# Google Cloud Natural Language API examples | ||
|
||
[![Open in Cloud Shell][shell_img]][shell_link] | ||
|
||
[shell_img]: http://gstatic.com/cloudssh/images/open-btn.png | ||
[shell_link]: https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/GoogleCloudPlatform/python-docs-samples&page=editor&open_in_editor=language/README.md | ||
|
||
This directory contains Python examples that use the | ||
[Google Cloud Natural Language API](https://cloud.google.com/natural-language/). | ||
|
||
- [api](api) has a simple command line tool that shows off the API's features. | ||
|
||
- [sentiment](sentiment) contains the [Sentiment Analysis | ||
Tutorial](https://cloud.google.com/natural-language/docs/sentiment-tutorial) | ||
code as used within the documentation. |
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,98 @@ | ||
.. This file is automatically generated. Do not edit this file directly. | ||
Google Cloud Natural Language API Python Samples | ||
=============================================================================== | ||
|
||
.. image:: https://gstatic.com/cloudssh/images/open-btn.png | ||
:target: https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/GoogleCloudPlatform/python-docs-samples&page=editor&open_in_editor=language/api/README.rst | ||
|
||
|
||
This directory contains samples for Google Cloud Natural Language API. The `Google Cloud Natural Language API`_ provides natural language understanding technologies to developers, including sentiment analysis, entity recognition, and syntax analysis. This API is part of the larger Cloud Machine Learning API. | ||
|
||
|
||
|
||
|
||
.. _Google Cloud Natural Language API: https://cloud.google.com/natural-language/docs/ | ||
|
||
|
||
|
||
|
||
|
||
Setup | ||
------------------------------------------------------------------------------- | ||
|
||
|
||
Authentication | ||
++++++++++++++ | ||
|
||
This sample requires you to have authentication setup. Refer to the | ||
`Authentication Getting Started Guide`_ for instructions on setting up | ||
credentials for applications. | ||
|
||
.. _Authentication Getting Started Guide: | ||
https://cloud.google.com/docs/authentication/getting-started | ||
|
||
Install Dependencies | ||
++++++++++++++++++++ | ||
|
||
#. Clone python-docs-samples and change directory to the sample directory you want to use. | ||
|
||
.. code-block:: bash | ||
$ git clone https://github.com/GoogleCloudPlatform/python-docs-samples.git | ||
#. Install `pip`_ and `virtualenv`_ if you do not already have them. You may want to refer to the `Python Development Environment Setup Guide`_ for Google Cloud Platform for instructions. | ||
|
||
.. _Python Development Environment Setup Guide: | ||
https://cloud.google.com/python/setup | ||
|
||
#. Create a virtualenv. Samples are compatible with Python 2.7 and 3.4+. | ||
|
||
.. code-block:: bash | ||
$ virtualenv env | ||
$ source env/bin/activate | ||
#. Install the dependencies needed to run the samples. | ||
|
||
.. code-block:: bash | ||
$ pip install -r requirements.txt | ||
.. _pip: https://pip.pypa.io/ | ||
.. _virtualenv: https://virtualenv.pypa.io/ | ||
|
||
Samples | ||
------------------------------------------------------------------------------- | ||
|
||
Analyze syntax | ||
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ | ||
|
||
.. image:: https://gstatic.com/cloudssh/images/open-btn.png | ||
:target: https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/GoogleCloudPlatform/python-docs-samples&page=editor&open_in_editor=language/api/analyze.py,language/api/README.rst | ||
|
||
|
||
|
||
|
||
To run this sample: | ||
|
||
.. code-block:: bash | ||
$ python analyze.py | ||
usage: analyze.py [-h] {entities,sentiment,syntax} text | ||
Analyzes text using the Google Cloud Natural Language API. | ||
positional arguments: | ||
{entities,sentiment,syntax} | ||
text | ||
optional arguments: | ||
-h, --help show this help message and exit | ||
.. _Google Cloud SDK: https://cloud.google.com/sdk/ |
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,22 @@ | ||
# This file is used to generate README.rst | ||
|
||
product: | ||
name: Google Cloud Natural Language API | ||
short_name: Cloud Natural Language API | ||
url: https://cloud.google.com/natural-language/docs/ | ||
description: > | ||
The `Google Cloud Natural Language API`_ provides natural language | ||
understanding technologies to developers, including sentiment analysis, | ||
entity recognition, and syntax analysis. This API is part of the larger | ||
Cloud Machine Learning API. | ||
|
||
setup: | ||
- auth | ||
- install_deps | ||
|
||
samples: | ||
- name: Analyze syntax | ||
file: analyze.py | ||
show_help: true | ||
|
||
folder: language/api |
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,92 @@ | ||
#!/usr/bin/env python | ||
|
||
# Copyright 2016 Google, Inc | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
"""Analyzes text using the Google Cloud Natural Language API.""" | ||
|
||
import argparse | ||
import json | ||
import sys | ||
|
||
import googleapiclient.discovery | ||
|
||
|
||
def get_native_encoding_type(): | ||
"""Returns the encoding type that matches Python's native strings.""" | ||
if sys.maxunicode == 65535: | ||
return "UTF16" | ||
else: | ||
return "UTF32" | ||
|
||
|
||
def analyze_entities(text, encoding="UTF32"): | ||
body = { | ||
"document": {"type": "PLAIN_TEXT", "content": text}, | ||
"encoding_type": encoding, | ||
} | ||
|
||
service = googleapiclient.discovery.build("language", "v1") | ||
|
||
request = service.documents().analyzeEntities(body=body) | ||
response = request.execute() | ||
|
||
return response | ||
|
||
|
||
def analyze_sentiment(text, encoding="UTF32"): | ||
body = { | ||
"document": {"type": "PLAIN_TEXT", "content": text}, | ||
"encoding_type": encoding, | ||
} | ||
|
||
service = googleapiclient.discovery.build("language", "v1") | ||
|
||
request = service.documents().analyzeSentiment(body=body) | ||
response = request.execute() | ||
|
||
return response | ||
|
||
|
||
def analyze_syntax(text, encoding="UTF32"): | ||
body = { | ||
"document": {"type": "PLAIN_TEXT", "content": text}, | ||
"encoding_type": encoding, | ||
} | ||
|
||
service = googleapiclient.discovery.build("language", "v1") | ||
|
||
request = service.documents().analyzeSyntax(body=body) | ||
response = request.execute() | ||
|
||
return response | ||
|
||
|
||
if __name__ == "__main__": | ||
parser = argparse.ArgumentParser( | ||
description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter | ||
) | ||
parser.add_argument("command", choices=["entities", "sentiment", "syntax"]) | ||
parser.add_argument("text") | ||
|
||
args = parser.parse_args() | ||
|
||
if args.command == "entities": | ||
result = analyze_entities(args.text, get_native_encoding_type()) | ||
elif args.command == "sentiment": | ||
result = analyze_sentiment(args.text, get_native_encoding_type()) | ||
elif args.command == "syntax": | ||
result = analyze_syntax(args.text, get_native_encoding_type()) | ||
|
||
print(json.dumps(result, indent=2)) |
Oops, something went wrong.