@@ -8,6 +8,195 @@ Library for PHP][google-cloud-php] to make REST calls as well as
88contains samples using the more-efficient (though sometimes more
99complex) [ GRPC] [ grpc ] API. The GRPC API also allows streaming requests.
1010
11- [ speech -api] : http://cloud.google.com/natural-language
11+ [ language -api] : http://cloud.google.com/natural-language
1212[ google-cloud-php ] : https://googlecloudplatform.github.io/google-cloud-php/
13- [ grpc ] : http://grpc.io
13+ [ grpc ] : http://grpc.io
14+
15+
16+ ## Setup
17+
18+ ### Authentication
19+
20+ Authentication is typically done through [ Application Default Credentials] [ adc ]
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+ 1 . When running locally, use the [ Google Cloud SDK] [ google-cloud-sdk ]
26+
27+ gcloud auth application-default login
28+
29+ 1 . When running on App Engine or Compute Engine, credentials are already
30+ set-up. However, you may need to configure your Compute Engine instance
31+ with [ additional scopes] [ additional_scopes ] .
32+
33+ 1 . You can create a [ Service Account key file] [ service_account_key_file ] . This file can be used to
34+ authenticate to Google Cloud Platform services from any environment. To use
35+ the file, set the `` GOOGLE_APPLICATION_CREDENTIALS `` environment variable to
36+ the path to the key file, for example:
37+
38+ export GOOGLE_APPLICATION_CREDENTIALS=/path/to/service_account.json
39+
40+ [ adc ] : https://cloud.google.com/docs/authentication#getting_credentials_for_server-centric_flow
41+ [ additional_scopes ] : https://cloud.google.com/compute/docs/authentication#using
42+ [ service_account_key_file ] : https://developers.google.com/identity/protocols/OAuth2ServiceAccount#creatinganaccount
43+
44+ ## Install Dependencies
45+
46+ 1 . [ Enable the Cloud Natural Language API] ( https://console.cloud.google.com/flows/enableapi?apiid=language.googleapis.com ) .
47+
48+ 1 . ** Install dependencies** via [ Composer] ( http://getcomposer.org/doc/00-intro.md ) .
49+ Run ` php composer.phar install ` (if composer is installed locally) or ` composer install `
50+ (if composer is installed globally).
51+
52+ 1 . Create a service account at the
53+ [ Service account section in the Cloud Console] ( https://console.cloud.google.com/iam-admin/serviceaccounts/ )
54+
55+ 1 . Download the json key file of the service account.
56+
57+ 1 . Set ` GOOGLE_APPLICATION_CREDENTIALS ` environment variable pointing to that file.
58+
59+ ## Samples
60+
61+ To run the Natural Language Samples:
62+
63+ $ php language.php
64+ Cloud Natural Language
65+
66+ Usage:
67+ command [options] [arguments]
68+
69+ Options:
70+ -h, --help Display this help message
71+ -q, --quiet Do not output any message
72+ -V, --version Display this application version
73+ --ansi Force ANSI output
74+ --no-ansi Disable ANSI output
75+ -n, --no-interaction Do not ask any interactive question
76+ -v|vv|vvv, --verbose Increase the verbosity of messages: 1 for normal output, 2 for more verbose output and 3 for debug
77+
78+ Available commands:
79+ all Analyze syntax, sentiment and entities in text.
80+ entities Analyze entities in text.
81+ help Displays help for a command
82+ list Lists commands
83+ sentiment Analyze sentiment in text.
84+ syntax Analyze syntax in text.
85+
86+ ### Run Analyze Entities
87+
88+ To run the Analyze Entities sample:
89+
90+ $ php language.php entities 'I know the way to San Jose. Do You?'
91+ entities:
92+ -
93+ name: way
94+ type: OTHER
95+ metadata: { }
96+ salience: 0.6970506
97+ mentions:
98+ -
99+ text:
100+ content: way
101+ beginOffset: 11
102+ type: COMMON
103+ -
104+ name: 'San Jose'
105+ type: LOCATION
106+ metadata:
107+ mid: /m/0f04v
108+ wikipedia_url: 'http://en.wikipedia.org/wiki/San_Jose,_California'
109+ salience: 0.30294943
110+ mentions:
111+ -
112+ text:
113+ content: 'San Jose'
114+ beginOffset: 18
115+ type: PROPER
116+ language: en
117+
118+ ### Run Analyze Sentiment
119+
120+ To run the Analyze Sentiment sample:
121+
122+ $ php language.php sentiment 'I know the way to San Jose. Do you?'
123+ documentSentiment:
124+ magnitude: 0.3
125+ score: 0.3
126+ language: en
127+ sentences:
128+ -
129+ text:
130+ content: 'I know the way to San Jose'
131+ beginOffset: 0
132+ sentiment:
133+ magnitude: 0.3
134+ score: 0.3
135+ -
136+ text:
137+ content: 'Do you?'
138+ beginOffset: 28
139+ sentiment:
140+ magnitude: 0.1
141+ score: -0.1
142+
143+ ### Run Analyze Syntax
144+
145+ To run the Analyze Syntax sample:
146+
147+ $ php language.php syntax 'I know the way to San Jose. Do you?'
148+ sentences:
149+ -
150+ text:
151+ content: 'I know the way to San Jose.'
152+ beginOffset: 0
153+ -
154+ text:
155+ content: 'Do you?'
156+ beginOffset: 28
157+ tokens:
158+ -
159+ text:
160+ content: I
161+ beginOffset: 0
162+ partOfSpeech:
163+ tag: PRON
164+ aspect: ASPECT_UNKNOWN
165+ case: NOMINATIVE
166+ form: FORM_UNKNOWN
167+ gender: GENDER_UNKNOWN
168+ mood: MOOD_UNKNOWN
169+ number: SINGULAR
170+ person: FIRST
171+ proper: PROPER_UNKNOWN
172+ reciprocity: RECIPROCITY_UNKNOWN
173+ tense: TENSE_UNKNOWN
174+ voice: VOICE_UNKNOWN
175+ dependencyEdge:
176+ headTokenIndex: 1
177+ label: NSUBJ
178+ lemma: I
179+ score: 0.3
180+ ...
181+ language: en
182+ entities: { }
183+
184+ ## The client library
185+
186+ This sample uses the [ Google Cloud Client Library for PHP] [ google-cloud-php ] .
187+ You can read the documentation for more details on API usage and use GitHub
188+ to [ browse the source] [ google-cloud-php-source ] and [ report issues] [ google-cloud-php-issues ] .
189+
190+ ## Troubleshooting
191+
192+ If you have not set a timezone you may get an error from php. This can be resolved by:
193+
194+ 1 . Finding where the php.ini is stored by running php -i | grep 'Configuration File'
195+ 1 . Finding out your timezone from the list on this page: http://php.net/manual/en/timezones.php
196+ 1 . Editing the php.ini file (or creating one if it doesn't exist)
197+ 1 . Adding the timezone to the php.ini file e.g., adding the following line: date.timezone = "America/Los_Angeles"
198+
199+ [ google-cloud-php ] : https://googlecloudplatform.github.io/google-cloud-php
200+ [ google-cloud-php-source ] : https://github.com/GoogleCloudPlatform/google-cloud-php
201+ [ google-cloud-php-issues ] : https://github.com/GoogleCloudPlatform/google-cloud-php/issues
202+ [ google-cloud-sdk ] : https://cloud.google.com/sdk/
0 commit comments