Skip to content

Commit 16ff341

Browse files
authored
Merge pull request #8533 from GoogleCloudPlatform/python-language-migration
migrate code from googleapis/python-language
2 parents bfc8bdc + c5eb6ba commit 16ff341

File tree

76 files changed

+3163
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

76 files changed

+3163
-0
lines changed

.github/CODEOWNERS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@
5555
/kms/**/** @GoogleCloudPlatform/dee-infra @GoogleCloudPlatform/python-samples-reviewers
5656
/kubernetes_engine/**/* @GoogleCloudPlatform/python-samples-reviewers
5757
/kubernetes_engine/django_tutorial/**/* @glasnt @GoogleCloudPlatform/python-samples-reviewers
58+
/language/**/* @GoogleCloudPlatform/dee-data-ai @GoogleCloudPlatform/python-samples-reviewers
5859
/media_cdn/**/* @justin-mp @msampathkumar @GoogleCloudPlatform/python-samples-reviewers
5960
/memorystore/**/* @GoogleCloudPlatform/python-samples-reviewers
6061
/ml_engine/**/* @ivanmkc @GoogleCloudPlatform/python-samples-reviewers

.github/blunderbuss.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,10 @@ assign_issues_by:
8585
- 'api: cloudiot'
8686
to:
8787
- GoogleCloudPlatform/api-iot
88+
- labels:
89+
- 'api: language'
90+
to:
91+
- GoogleCloudPlatform/dee-data-ai
8892
- labels:
8993
- 'api: ml'
9094
to:

.github/header-checker-lint.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ ignoreFiles:
1818
- "**/ghcnd-stations.txt"
1919
- "texttospeech/snippets/resources/example.txt"
2020
- "texttospeech/snippets/resources/hello.txt"
21+
- "language/**/resources/*.txt"
22+
- "language/snippets/classify_text/resources/texts/*.txt"
2123

2224
ignoreLicenseYear: true
2325

language/AUTHORING_GUIDE.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
See https://github.com/GoogleCloudPlatform/python-docs-samples/blob/main/AUTHORING_GUIDE.md

language/CONTRIBUTING.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
See https://github.com/GoogleCloudPlatform/python-docs-samples/blob/main/CONTRIBUTING.md

language/snippets/README.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# Google Cloud Natural Language API examples
2+
3+
[![Open in Cloud Shell][shell_img]][shell_link]
4+
5+
[shell_img]: http://gstatic.com/cloudssh/images/open-btn.png
6+
[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
7+
8+
This directory contains Python examples that use the
9+
[Google Cloud Natural Language API](https://cloud.google.com/natural-language/).
10+
11+
- [api](api) has a simple command line tool that shows off the API's features.
12+
13+
- [sentiment](sentiment) contains the [Sentiment Analysis
14+
Tutorial](https://cloud.google.com/natural-language/docs/sentiment-tutorial)
15+
code as used within the documentation.

language/snippets/api/README.rst

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
.. This file is automatically generated. Do not edit this file directly.
2+
3+
Google Cloud Natural Language API Python Samples
4+
===============================================================================
5+
6+
.. image:: https://gstatic.com/cloudssh/images/open-btn.png
7+
: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
8+
9+
10+
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.
11+
12+
13+
14+
15+
.. _Google Cloud Natural Language API: https://cloud.google.com/natural-language/docs/
16+
17+
18+
19+
20+
21+
Setup
22+
-------------------------------------------------------------------------------
23+
24+
25+
Authentication
26+
++++++++++++++
27+
28+
This sample requires you to have authentication setup. Refer to the
29+
`Authentication Getting Started Guide`_ for instructions on setting up
30+
credentials for applications.
31+
32+
.. _Authentication Getting Started Guide:
33+
https://cloud.google.com/docs/authentication/getting-started
34+
35+
Install Dependencies
36+
++++++++++++++++++++
37+
38+
#. Clone python-docs-samples and change directory to the sample directory you want to use.
39+
40+
.. code-block:: bash
41+
42+
$ git clone https://github.com/GoogleCloudPlatform/python-docs-samples.git
43+
44+
#. 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.
45+
46+
.. _Python Development Environment Setup Guide:
47+
https://cloud.google.com/python/setup
48+
49+
#. Create a virtualenv. Samples are compatible with Python 2.7 and 3.4+.
50+
51+
.. code-block:: bash
52+
53+
$ virtualenv env
54+
$ source env/bin/activate
55+
56+
#. Install the dependencies needed to run the samples.
57+
58+
.. code-block:: bash
59+
60+
$ pip install -r requirements.txt
61+
62+
.. _pip: https://pip.pypa.io/
63+
.. _virtualenv: https://virtualenv.pypa.io/
64+
65+
Samples
66+
-------------------------------------------------------------------------------
67+
68+
Analyze syntax
69+
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
70+
71+
.. image:: https://gstatic.com/cloudssh/images/open-btn.png
72+
: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
73+
74+
75+
76+
77+
To run this sample:
78+
79+
.. code-block:: bash
80+
81+
$ python analyze.py
82+
83+
usage: analyze.py [-h] {entities,sentiment,syntax} text
84+
85+
Analyzes text using the Google Cloud Natural Language API.
86+
87+
positional arguments:
88+
{entities,sentiment,syntax}
89+
text
90+
91+
optional arguments:
92+
-h, --help show this help message and exit
93+
94+
95+
96+
97+
98+
.. _Google Cloud SDK: https://cloud.google.com/sdk/

language/snippets/api/README.rst.in

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# This file is used to generate README.rst
2+
3+
product:
4+
name: Google Cloud Natural Language API
5+
short_name: Cloud Natural Language API
6+
url: https://cloud.google.com/natural-language/docs/
7+
description: >
8+
The `Google Cloud Natural Language API`_ provides natural language
9+
understanding technologies to developers, including sentiment analysis,
10+
entity recognition, and syntax analysis. This API is part of the larger
11+
Cloud Machine Learning API.
12+
13+
setup:
14+
- auth
15+
- install_deps
16+
17+
samples:
18+
- name: Analyze syntax
19+
file: analyze.py
20+
show_help: true
21+
22+
folder: language/api

language/snippets/api/analyze.py

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
#!/usr/bin/env python
2+
3+
# Copyright 2016 Google, Inc
4+
#
5+
# Licensed under the Apache License, Version 2.0 (the "License");
6+
# you may not use this file except in compliance with the License.
7+
# You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing, software
12+
# distributed under the License is distributed on an "AS IS" BASIS,
13+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
# See the License for the specific language governing permissions and
15+
# limitations under the License.
16+
17+
"""Analyzes text using the Google Cloud Natural Language API."""
18+
19+
import argparse
20+
import json
21+
import sys
22+
23+
import googleapiclient.discovery
24+
25+
26+
def get_native_encoding_type():
27+
"""Returns the encoding type that matches Python's native strings."""
28+
if sys.maxunicode == 65535:
29+
return "UTF16"
30+
else:
31+
return "UTF32"
32+
33+
34+
def analyze_entities(text, encoding="UTF32"):
35+
body = {
36+
"document": {"type": "PLAIN_TEXT", "content": text},
37+
"encoding_type": encoding,
38+
}
39+
40+
service = googleapiclient.discovery.build("language", "v1")
41+
42+
request = service.documents().analyzeEntities(body=body)
43+
response = request.execute()
44+
45+
return response
46+
47+
48+
def analyze_sentiment(text, encoding="UTF32"):
49+
body = {
50+
"document": {"type": "PLAIN_TEXT", "content": text},
51+
"encoding_type": encoding,
52+
}
53+
54+
service = googleapiclient.discovery.build("language", "v1")
55+
56+
request = service.documents().analyzeSentiment(body=body)
57+
response = request.execute()
58+
59+
return response
60+
61+
62+
def analyze_syntax(text, encoding="UTF32"):
63+
body = {
64+
"document": {"type": "PLAIN_TEXT", "content": text},
65+
"encoding_type": encoding,
66+
}
67+
68+
service = googleapiclient.discovery.build("language", "v1")
69+
70+
request = service.documents().analyzeSyntax(body=body)
71+
response = request.execute()
72+
73+
return response
74+
75+
76+
if __name__ == "__main__":
77+
parser = argparse.ArgumentParser(
78+
description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter
79+
)
80+
parser.add_argument("command", choices=["entities", "sentiment", "syntax"])
81+
parser.add_argument("text")
82+
83+
args = parser.parse_args()
84+
85+
if args.command == "entities":
86+
result = analyze_entities(args.text, get_native_encoding_type())
87+
elif args.command == "sentiment":
88+
result = analyze_sentiment(args.text, get_native_encoding_type())
89+
elif args.command == "syntax":
90+
result = analyze_syntax(args.text, get_native_encoding_type())
91+
92+
print(json.dumps(result, indent=2))

0 commit comments

Comments
 (0)