Skip to content

Commit

Permalink
Merge pull request #8533 from GoogleCloudPlatform/python-language-mig…
Browse files Browse the repository at this point in the history
…ration

migrate code from googleapis/python-language
  • Loading branch information
dandhlee authored Nov 22, 2022
2 parents bfc8bdc + c5eb6ba commit 16ff341
Show file tree
Hide file tree
Showing 76 changed files with 3,163 additions and 0 deletions.
1 change: 1 addition & 0 deletions .github/CODEOWNERS
Validating CODEOWNERS rules …
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@
/kms/**/** @GoogleCloudPlatform/dee-infra @GoogleCloudPlatform/python-samples-reviewers
/kubernetes_engine/**/* @GoogleCloudPlatform/python-samples-reviewers
/kubernetes_engine/django_tutorial/**/* @glasnt @GoogleCloudPlatform/python-samples-reviewers
/language/**/* @GoogleCloudPlatform/dee-data-ai @GoogleCloudPlatform/python-samples-reviewers
/media_cdn/**/* @justin-mp @msampathkumar @GoogleCloudPlatform/python-samples-reviewers
/memorystore/**/* @GoogleCloudPlatform/python-samples-reviewers
/ml_engine/**/* @ivanmkc @GoogleCloudPlatform/python-samples-reviewers
Expand Down
4 changes: 4 additions & 0 deletions .github/blunderbuss.yml
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,10 @@ assign_issues_by:
- 'api: cloudiot'
to:
- GoogleCloudPlatform/api-iot
- labels:
- 'api: language'
to:
- GoogleCloudPlatform/dee-data-ai
- labels:
- 'api: ml'
to:
Expand Down
2 changes: 2 additions & 0 deletions .github/header-checker-lint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ ignoreFiles:
- "**/ghcnd-stations.txt"
- "texttospeech/snippets/resources/example.txt"
- "texttospeech/snippets/resources/hello.txt"
- "language/**/resources/*.txt"
- "language/snippets/classify_text/resources/texts/*.txt"

ignoreLicenseYear: true

Expand Down
1 change: 1 addition & 0 deletions language/AUTHORING_GUIDE.md
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
1 change: 1 addition & 0 deletions language/CONTRIBUTING.md
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
15 changes: 15 additions & 0 deletions language/snippets/README.md
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.
98 changes: 98 additions & 0 deletions language/snippets/api/README.rst
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/
22 changes: 22 additions & 0 deletions language/snippets/api/README.rst.in
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
92 changes: 92 additions & 0 deletions language/snippets/api/analyze.py
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))
Loading

0 comments on commit 16ff341

Please sign in to comment.