Skip to content

Commit 8e5ee41

Browse files
committed
[syntax] Migrate to new client LanguageServiceClient in 0.16.2
1 parent 00f6623 commit 8e5ee41

File tree

4 files changed

+75
-39
lines changed

4 files changed

+75
-39
lines changed

language/composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"require": {
3-
"google/cloud-language": "~0.12",
3+
"google/cloud-language": "~0.16.2",
44
"google/cloud-storage": "^1.3",
55
"symfony/console": "^3.0"
66
},

language/language.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -213,7 +213,7 @@
213213
// Regex to match a Cloud Storage path as the first argument
214214
// e.g "gs://my-bucket/file_with_text.txt"
215215
if (preg_match('/^gs:\/\/([a-z0-9\._\-]+)\/(\S+)$/', $content, $matches)) {
216-
analyze_syntax_from_file($matches[1], $matches[2], $projectId);
216+
analyze_syntax_from_file($matches[0], $projectId);
217217
} else {
218218
analyze_syntax($content, $projectId);
219219
}

language/src/analyze_syntax.php

Lines changed: 36 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,8 @@
2424
# [START language_syntax_text]
2525
namespace Google\Cloud\Samples\Language;
2626

27-
use Google\Cloud\Language\LanguageClient;
27+
use Google\Cloud\Language\V1beta2\Document;
28+
use Google\Cloud\Language\V1beta2\LanguageServiceClient;
2829

2930
/**
3031
* Find the syntax in text.
@@ -39,20 +40,41 @@
3940
function analyze_syntax($text, $projectId = null)
4041
{
4142
// Create the Natural Language client
42-
$language = new LanguageClient([
43-
'projectId' => $projectId,
44-
]);
43+
$languageServiceClient = new LanguageServiceClient(['projectId' => $projectId]);
4544

46-
// Call the analyzeSyntax function
47-
$annotation = $language->analyzeSyntax($text);
48-
49-
// Print syntax information. See https://cloud.google.com/natural-language/docs/reference/rest/v1/Token
50-
// to learn about more information you can extract from Token objects.
51-
$tokens = $annotation->tokens();
52-
foreach ($tokens as $token) {
53-
printf('Token text: %s' . PHP_EOL, $token['text']['content']);
54-
printf('Token part of speech: %s' . PHP_EOL, $token['partOfSpeech']['tag']);
55-
printf(PHP_EOL);
45+
try {
46+
$tag_types = [
47+
0 => 'UNKNOWN',
48+
1 => 'ADJ',
49+
2 => 'ADP',
50+
3 => 'ADV',
51+
4 => 'CONJ',
52+
5 => 'DET',
53+
6 => 'NOUN',
54+
7 => 'NUM',
55+
8 => 'PRON',
56+
9 => 'PRT',
57+
10 => 'PUNCT',
58+
11 => 'VERB',
59+
12 => 'X',
60+
13 => 'AFFIX',
61+
];
62+
// Create a new Document
63+
$document = new Document();
64+
// Add text as content and set document type to PLAIN_TEXT
65+
$document->setContent($text)->setType(1);
66+
// Call the analyzeEntities function
67+
$response = $languageServiceClient->analyzeSyntax($document, []);
68+
$tokens = $response->getTokens();
69+
// Print out information about each entity
70+
foreach ($tokens as $token) {
71+
printf('Token text: %s' . PHP_EOL, $token->getText()->getContent());
72+
printf('Token part of speech: %s' . PHP_EOL, $tag_types[$token->getPartOfSpeech()->getTag()]);
73+
printf(PHP_EOL);
74+
}
75+
} finally {
76+
$languageServiceClient->close();
5677
}
78+
5779
}
5880
# [END language_syntax_text]

language/src/analyze_syntax_from_file.php

Lines changed: 37 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -24,42 +24,56 @@
2424
# [START language_syntax_gcs]
2525
namespace Google\Cloud\Samples\Language;
2626

27-
use Google\Cloud\Language\LanguageClient;
28-
use Google\Cloud\Storage\StorageClient;
27+
use Google\Cloud\Language\V1beta2\Document;
28+
use Google\Cloud\Language\V1beta2\LanguageServiceClient;
2929

3030
/**
3131
* Find the syntax in text stored in a Cloud Storage bucket.
3232
* ```
3333
* analyze_syntax_from_file('my-bucket', 'file_with_text.txt');
3434
* ```
3535
*
36-
* @param string $bucketName The Cloud Storage bucket.
37-
* @param string $objectName The Cloud Storage object with text.
36+
* @param string $gcsUri The Cloud Storage path with text.
3837
* @param string $projectId (optional) Your Google Cloud Project ID
3938
*
4039
*/
41-
function analyze_syntax_from_file($bucketName, $objectName, $projectId = null)
40+
function analyze_syntax_from_file($gcsUri, $projectId = null)
4241
{
43-
// Create the Cloud Storage object
44-
$storage = new StorageClient();
45-
$bucket = $storage->bucket($bucketName);
46-
$storageObject = $bucket->object($objectName);
47-
4842
// Create the Natural Language client
49-
$language = new LanguageClient([
50-
'projectId' => $projectId,
51-
]);
52-
53-
// Call the analyzeSyntax function
54-
$annotation = $language->analyzeSyntax($storageObject);
43+
$languageServiceClient = new LanguageServiceClient(['projectId' => $projectId]);
5544

56-
// Print syntax information. See https://cloud.google.com/natural-language/docs/reference/rest/v1/Token
57-
// to learn about more information you can extract from Token objects.
58-
$tokens = $annotation->tokens();
59-
foreach ($tokens as $token) {
60-
printf('Token text: %s' . PHP_EOL, $token['text']['content']);
61-
printf('Token part of speech: %s' . PHP_EOL, $token['partOfSpeech']['tag']);
62-
printf(PHP_EOL);
45+
try {
46+
$tag_types = [
47+
0 => 'UNKNOWN',
48+
1 => 'ADJ',
49+
2 => 'ADP',
50+
3 => 'ADV',
51+
4 => 'CONJ',
52+
5 => 'DET',
53+
6 => 'NOUN',
54+
7 => 'NUM',
55+
8 => 'PRON',
56+
9 => 'PRT',
57+
10 => 'PUNCT',
58+
11 => 'VERB',
59+
12 => 'X',
60+
13 => 'AFFIX',
61+
];
62+
// Create a new Document
63+
$document = new Document();
64+
// Pass GCS URI and set document type to PLAIN_TEXT
65+
$document->setGcsContentUri($gcsUri)->setType(1);
66+
// Call the analyzeEntities function
67+
$response = $languageServiceClient->analyzeSyntax($document, []);
68+
$tokens = $response->getTokens();
69+
// Print out information about each entity
70+
foreach ($tokens as $token) {
71+
printf('Token text: %s' . PHP_EOL, $token->getText()->getContent());
72+
printf('Token part of speech: %s' . PHP_EOL, $tag_types[$token->getPartOfSpeech()->getTag()]);
73+
printf(PHP_EOL);
74+
}
75+
} finally {
76+
$languageServiceClient->close();
6377
}
6478
}
6579
# [END language_syntax_gcs]

0 commit comments

Comments
 (0)