Skip to content

Commit c02c187

Browse files
gguussdpebot
authored andcommitted
NL v1beta2 (#908)
* 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.
1 parent bd718e4 commit c02c187

16 files changed

+539
-0
lines changed
Lines changed: 144 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,144 @@
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-entities-text,sentiment-entities-file,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-entities-text,sentiment-entities-file,sentiment-text,sentiment-file,entities-text,entities-file,syntax-text,syntax-file}
108+
sentiment-entities-text
109+
Detects entity sentiment in the provided text.
110+
sentiment-entities-file
111+
Detects entity sentiment in a Google Cloud Storage
112+
file.
113+
sentiment-text Detects sentiment in the text.
114+
sentiment-file Detects sentiment in the file located in Google Cloud
115+
Storage.
116+
entities-text Detects entities in the text.
117+
entities-file Detects entities in the file located in Google Cloud
118+
Storage.
119+
syntax-text Detects syntax in the text.
120+
syntax-file Detects syntax in the file located in Google Cloud
121+
Storage.
122+
123+
optional arguments:
124+
-h, --help show this help message and exit
125+
126+
127+
128+
129+
The client library
130+
-------------------------------------------------------------------------------
131+
132+
This sample uses the `Google Cloud Client Library for Python`_.
133+
You can read the documentation for more details on API usage and use GitHub
134+
to `browse the source`_ and `report issues`_.
135+
136+
.. Google Cloud Client Library for Python:
137+
https://googlecloudplatform.github.io/google-cloud-python/
138+
.. browse the source:
139+
https://github.com/GoogleCloudPlatform/google-cloud-python
140+
.. report issues:
141+
https://github.com/GoogleCloudPlatform/google-cloud-python/issues
142+
143+
144+
.. _Google Cloud SDK: https://cloud.google.com/sdk/
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
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 2017 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 with they v1beta2 version
24+
language_client = language.Client(api_version='v1beta2')
25+
26+
# The text to analyze
27+
text = 'Hallo Welt!'
28+
document = language_client.document_from_text(text, language='DE')
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()
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# Copyright 2017 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
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
gapic-google-cloud-language-v1beta2==0.15.3
2+
google-cloud-language==0.24.1
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)