Skip to content

Commit 55e4bc4

Browse files
gguussdpebot
authored andcommitted
* Reorganizes samples, adds new snippet, and demonstrates switching API versions using GAPIC manual layer. * Corrects beta version in link * Copyright dates on new files * Removes README with nav, changes all snippets to use v1beta2 in beta folder * Fixes v1beta2 test on GCS sentiment.
0 parents  commit 55e4bc4

File tree

8 files changed

+458
-0
lines changed

8 files changed

+458
-0
lines changed

samples/snippets/README.rst

Lines changed: 139 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,139 @@
1+
.. This file is automatically generated. Do not edit this file directly.
2+
3+
Google Cloud Natural Language API Python Samples
4+
===============================================================================
5+
6+
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.
7+
8+
9+
10+
11+
.. _Google Cloud Natural Language API: https://cloud.google.com/natural-language/docs/
12+
13+
Setup
14+
-------------------------------------------------------------------------------
15+
16+
17+
Authentication
18+
++++++++++++++
19+
20+
Authentication is typically done through `Application Default Credentials`_,
21+
which means you do not have to change the code to authenticate as long as
22+
your environment has credentials. You have a few options for setting up
23+
authentication:
24+
25+
#. When running locally, use the `Google Cloud SDK`_
26+
27+
.. code-block:: bash
28+
29+
gcloud beta auth application-default login
30+
31+
32+
#. When running on App Engine or Compute Engine, credentials are already
33+
set-up. However, you may need to configure your Compute Engine instance
34+
with `additional scopes`_.
35+
36+
#. You can create a `Service Account key file`_. This file can be used to
37+
authenticate to Google Cloud Platform services from any environment. To use
38+
the file, set the ``GOOGLE_APPLICATION_CREDENTIALS`` environment variable to
39+
the path to the key file, for example:
40+
41+
.. code-block:: bash
42+
43+
export GOOGLE_APPLICATION_CREDENTIALS=/path/to/service_account.json
44+
45+
.. _Application Default Credentials: https://cloud.google.com/docs/authentication#getting_credentials_for_server-centric_flow
46+
.. _additional scopes: https://cloud.google.com/compute/docs/authentication#using
47+
.. _Service Account key file: https://developers.google.com/identity/protocols/OAuth2ServiceAccount#creatinganaccount
48+
49+
Install Dependencies
50+
++++++++++++++++++++
51+
52+
#. Install `pip`_ and `virtualenv`_ if you do not already have them.
53+
54+
#. Create a virtualenv. Samples are compatible with Python 2.7 and 3.4+.
55+
56+
.. code-block:: bash
57+
58+
$ virtualenv env
59+
$ source env/bin/activate
60+
61+
#. Install the dependencies needed to run the samples.
62+
63+
.. code-block:: bash
64+
65+
$ pip install -r requirements.txt
66+
67+
.. _pip: https://pip.pypa.io/
68+
.. _virtualenv: https://virtualenv.pypa.io/
69+
70+
Samples
71+
-------------------------------------------------------------------------------
72+
73+
Quickstart
74+
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
75+
76+
77+
78+
To run this sample:
79+
80+
.. code-block:: bash
81+
82+
$ python quickstart.py
83+
84+
85+
Snippets
86+
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
87+
88+
89+
90+
To run this sample:
91+
92+
.. code-block:: bash
93+
94+
$ python snippets.py
95+
96+
usage: snippets.py [-h]
97+
{sentiment-text,sentiment-file,entities-text,entities-file,syntax-text,syntax-file}
98+
...
99+
100+
This application demonstrates how to perform basic operations with the
101+
Google Cloud Natural Language API
102+
103+
For more information, the documentation at
104+
https://cloud.google.com/natural-language/docs.
105+
106+
positional arguments:
107+
{sentiment-text,sentiment-file,entities-text,entities-file,syntax-text,syntax-file}
108+
sentiment-text Detects sentiment in the text.
109+
sentiment-file Detects sentiment in the file located in Google Cloud
110+
Storage.
111+
entities-text Detects entities in the text.
112+
entities-file Detects entities in the file located in Google Cloud
113+
Storage.
114+
syntax-text Detects syntax in the text.
115+
syntax-file Detects syntax in the file located in Google Cloud
116+
Storage.
117+
118+
optional arguments:
119+
-h, --help show this help message and exit
120+
121+
122+
123+
124+
The client library
125+
-------------------------------------------------------------------------------
126+
127+
This sample uses the `Google Cloud Client Library for Python`_.
128+
You can read the documentation for more details on API usage and use GitHub
129+
to `browse the source`_ and `report issues`_.
130+
131+
.. Google Cloud Client Library for Python:
132+
https://googlecloudplatform.github.io/google-cloud-python/
133+
.. browse the source:
134+
https://github.com/GoogleCloudPlatform/google-cloud-python
135+
.. report issues:
136+
https://github.com/GoogleCloudPlatform/google-cloud-python/issues
137+
138+
139+
.. _Google Cloud SDK: https://cloud.google.com/sdk/

samples/snippets/README.rst.in

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
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: Quickstart
19+
file: quickstart.py
20+
- name: Snippets
21+
file: snippets.py
22+
show_help: true
23+
24+
cloud_client_library: true

samples/snippets/quickstart.py

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
#!/usr/bin/env python
2+
3+
# Copyright 2016 Google Inc. All Rights Reserved.
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+
18+
def run_quickstart():
19+
# [START language_quickstart]
20+
# Imports the Google Cloud client library
21+
from google.cloud import language
22+
23+
# Instantiates a client
24+
language_client = language.Client()
25+
26+
# The text to analyze
27+
text = 'Hello, world!'
28+
document = language_client.document_from_text(text)
29+
30+
# Detects the sentiment of the text
31+
sentiment = document.analyze_sentiment().sentiment
32+
33+
print('Text: {}'.format(text))
34+
print('Sentiment: {}, {}'.format(sentiment.score, sentiment.magnitude))
35+
# [END language_quickstart]
36+
37+
38+
if __name__ == '__main__':
39+
run_quickstart()

samples/snippets/quickstart_test.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# Copyright 2016 Google Inc. All Rights Reserved.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
16+
import quickstart
17+
18+
19+
def test_quickstart(capsys):
20+
quickstart.run_quickstart()
21+
out, _ = capsys.readouterr()
22+
assert 'Sentiment' in out

samples/snippets/requirements.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
google-cloud-language==0.24.1

samples/snippets/resources/text.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
President Obama is speaking at the White House.

0 commit comments

Comments
 (0)