Skip to content

Commit 32ae358

Browse files
authored
Merge pull request GoogleCloudPlatform#789 from gogasca/syntax
[syntax] Migrate to new client LanguageServiceClient in 0.16.2
2 parents cf0b45a + bdd50f6 commit 32ae358

File tree

3 files changed

+75
-38
lines changed

3 files changed

+75
-38
lines changed

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($content, $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,9 @@
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\Document\Type;
29+
use Google\Cloud\Language\V1beta2\LanguageServiceClient;
2830

2931
/**
3032
* Find the syntax in text.
@@ -39,20 +41,40 @@
3941
function analyze_syntax($text, $projectId = null)
4042
{
4143
// Create the Natural Language client
42-
$language = new LanguageClient([
43-
'projectId' => $projectId,
44-
]);
44+
$languageServiceClient = new LanguageServiceClient(['projectId' => $projectId]);
4545

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);
46+
try {
47+
$tag_types = [
48+
0 => 'UNKNOWN',
49+
1 => 'ADJ',
50+
2 => 'ADP',
51+
3 => 'ADV',
52+
4 => 'CONJ',
53+
5 => 'DET',
54+
6 => 'NOUN',
55+
7 => 'NUM',
56+
8 => 'PRON',
57+
9 => 'PRT',
58+
10 => 'PUNCT',
59+
11 => 'VERB',
60+
12 => 'X',
61+
13 => 'AFFIX',
62+
];
63+
// Create a new Document
64+
$document = new Document();
65+
// Add text as content and set document type to PLAIN_TEXT
66+
$document->setContent($text)->setType(Type::PLAIN_TEXT);
67+
// Call the analyzeEntities function
68+
$response = $languageServiceClient->analyzeSyntax($document, []);
69+
$tokens = $response->getTokens();
70+
// Print out information about each entity
71+
foreach ($tokens as $token) {
72+
printf('Token text: %s' . PHP_EOL, $token->getText()->getContent());
73+
printf('Token part of speech: %s' . PHP_EOL, $tag_types[$token->getPartOfSpeech()->getTag()]);
74+
print(PHP_EOL);
75+
}
76+
} finally {
77+
$languageServiceClient->close();
5678
}
5779
}
5880
# [END language_syntax_text]

language/src/analyze_syntax_from_file.php

Lines changed: 38 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -24,42 +24,57 @@
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\Document\Type;
29+
use Google\Cloud\Language\V1beta2\LanguageServiceClient;
2930

3031
/**
3132
* Find the syntax in text stored in a Cloud Storage bucket.
3233
* ```
3334
* analyze_syntax_from_file('my-bucket', 'file_with_text.txt');
3435
* ```
3536
*
36-
* @param string $bucketName The Cloud Storage bucket.
37-
* @param string $objectName The Cloud Storage object with text.
37+
* @param string $gcsUri The Cloud Storage path with text.
3838
* @param string $projectId (optional) Your Google Cloud Project ID
3939
*
4040
*/
41-
function analyze_syntax_from_file($bucketName, $objectName, $projectId = null)
41+
function analyze_syntax_from_file($gcsUri, $projectId = null)
4242
{
43-
// Create the Cloud Storage object
44-
$storage = new StorageClient();
45-
$bucket = $storage->bucket($bucketName);
46-
$storageObject = $bucket->object($objectName);
47-
4843
// Create the Natural Language client
49-
$language = new LanguageClient([
50-
'projectId' => $projectId,
51-
]);
52-
53-
// Call the analyzeSyntax function
54-
$annotation = $language->analyzeSyntax($storageObject);
44+
$languageServiceClient = new LanguageServiceClient(['projectId' => $projectId]);
5545

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);
46+
try {
47+
$tag_types = [
48+
0 => 'UNKNOWN',
49+
1 => 'ADJ',
50+
2 => 'ADP',
51+
3 => 'ADV',
52+
4 => 'CONJ',
53+
5 => 'DET',
54+
6 => 'NOUN',
55+
7 => 'NUM',
56+
8 => 'PRON',
57+
9 => 'PRT',
58+
10 => 'PUNCT',
59+
11 => 'VERB',
60+
12 => 'X',
61+
13 => 'AFFIX',
62+
];
63+
// Create a new Document
64+
$document = new Document();
65+
// Pass GCS URI and set document type to PLAIN_TEXT
66+
$document->setGcsContentUri($gcsUri)->setType(Type::PLAIN_TEXT);
67+
// Call the analyzeEntities function
68+
$response = $languageServiceClient->analyzeSyntax($document, []);
69+
$tokens = $response->getTokens();
70+
// Print out information about each entity
71+
foreach ($tokens as $token) {
72+
printf('Token text: %s' . PHP_EOL, $token->getText()->getContent());
73+
printf('Token part of speech: %s' . PHP_EOL, $tag_types[$token->getPartOfSpeech()->getTag()]);
74+
print(PHP_EOL);
75+
}
76+
} finally {
77+
$languageServiceClient->close();
6378
}
6479
}
6580
# [END language_syntax_gcs]

0 commit comments

Comments
 (0)