Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit a236a5a

Browse filesBrowse files
author
Jon Wayne Parrott
authored
Merge pull request GoogleCloudPlatform#593 from GoogleCloudPlatform/nl15
Language API changes
2 parents f16a274 + 8a86957 commit a236a5a
Copy full SHA for a236a5a

File tree

Expand file treeCollapse file tree

4 files changed

+212
-12
lines changed
Filter options
Expand file treeCollapse file tree

4 files changed

+212
-12
lines changed

‎language/api/README.rst

Copy file name to clipboard
+98Lines 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+
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+
Analyze syntax
74+
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
75+
76+
77+
78+
To run this sample:
79+
80+
.. code-block:: bash
81+
82+
$ python analyze.py
83+
84+
usage: analyze.py [-h] {entities,sentiment,syntax} text
85+
86+
Analyzes text using the Google Cloud Natural Language API.
87+
88+
positional arguments:
89+
{entities,sentiment,syntax}
90+
text
91+
92+
optional arguments:
93+
-h, --help show this help message and exit
94+
95+
96+
97+
98+
.. _Google Cloud SDK: https://cloud.google.com/sdk/

‎language/api/analyze.py

Copy file name to clipboardExpand all lines: language/api/analyze.py
+10-10Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,9 @@ def get_service():
3131
['https://www.googleapis.com/auth/cloud-platform'])
3232
http = httplib2.Http()
3333
scoped_credentials.authorize(http)
34-
return discovery.build('language', 'v1beta1', http=http)
34+
return discovery.build('language', 'v1',
35+
http=http,
36+
credentials=credentials)
3537

3638

3739
def get_native_encoding_type():
@@ -48,7 +50,7 @@ def analyze_entities(text, encoding='UTF32'):
4850
'type': 'PLAIN_TEXT',
4951
'content': text,
5052
},
51-
'encodingType': encoding,
53+
'encoding_type': encoding,
5254
}
5355

5456
service = get_service()
@@ -59,12 +61,13 @@ def analyze_entities(text, encoding='UTF32'):
5961
return response
6062

6163

62-
def analyze_sentiment(text):
64+
def analyze_sentiment(text, encoding='UTF32'):
6365
body = {
6466
'document': {
6567
'type': 'PLAIN_TEXT',
6668
'content': text,
67-
}
69+
},
70+
'encoding_type': encoding
6871
}
6972

7073
service = get_service()
@@ -81,15 +84,12 @@ def analyze_syntax(text, encoding='UTF32'):
8184
'type': 'PLAIN_TEXT',
8285
'content': text,
8386
},
84-
'features': {
85-
'extract_syntax': True,
86-
},
87-
'encodingType': encoding,
87+
'encoding_type': encoding
8888
}
8989

9090
service = get_service()
9191

92-
request = service.documents().annotateText(body=body)
92+
request = service.documents().analyzeSyntax(body=body)
9393
response = request.execute()
9494

9595
return response
@@ -108,7 +108,7 @@ def analyze_syntax(text, encoding='UTF32'):
108108
if args.command == 'entities':
109109
result = analyze_entities(args.text, get_native_encoding_type())
110110
elif args.command == 'sentiment':
111-
result = analyze_sentiment(args.text)
111+
result = analyze_sentiment(args.text, get_native_encoding_type())
112112
elif args.command == 'syntax':
113113
result = analyze_syntax(args.text, get_native_encoding_type())
114114

‎language/api/analyze_test.py

Copy file name to clipboardExpand all lines: language/api/analyze_test.py
+2-2Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,15 +33,15 @@ def test_analyze_sentiment(capsys):
3333
'your face is really ugly and i hate it.')
3434

3535
sentiment = result['documentSentiment']
36-
assert sentiment['polarity'] < 0
36+
assert sentiment['score'] < 0
3737
assert sentiment['magnitude'] < 1
3838

3939
result = analyze.analyze_sentiment(
4040
'cheerio, mate - I greatly admire the pallor of your visage, and your '
4141
'angle of repose leaves little room for improvement.')
4242

4343
sentiment = result['documentSentiment']
44-
assert sentiment['polarity'] > 0
44+
assert sentiment['score'] > 0
4545
assert sentiment['magnitude'] < 1
4646

4747

‎language/cloud-client/README.rst

Copy file name to clipboard
+102Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
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+
86+
87+
The client library
88+
-------------------------------------------------------------------------------
89+
90+
This sample uses the `Google Cloud Client Library for Python`_.
91+
You can read the documentation for more details on API usage and use GitHub
92+
to `browse the source`_ and `report issues`_.
93+
94+
.. Google Cloud Client Library for Python:
95+
https://googlecloudplatform.github.io/google-cloud-python/
96+
.. browse the source:
97+
https://github.com/GoogleCloudPlatform/google-cloud-python
98+
.. report issues:
99+
https://github.com/GoogleCloudPlatform/google-cloud-python/issues
100+
101+
102+
.. _Google Cloud SDK: https://cloud.google.com/sdk/

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.