Skip to content

Commit a0ba8c0

Browse files
committed
Migrate to new client LanguageServiceClient in 0.16.2
Migrate analyze entities and analyze entities to read from GCS Modify test cases to read single URI
1 parent 00f6623 commit a0ba8c0

File tree

4 files changed

+50
-55
lines changed

4 files changed

+50
-55
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
@@ -127,7 +127,7 @@
127127
// Regex to match a Cloud Storage path as the first argument
128128
// e.g "gs://my-bucket/file_with_text.txt"
129129
if (preg_match('/^gs:\/\/([a-z0-9\._\-]+)\/(\S+)$/', $content, $matches)) {
130-
analyze_entities_from_file($matches[1], $matches[2], $projectId);
130+
analyze_entities_from_file($matches[0], $projectId);
131131
} else {
132132
analyze_entities($content, $projectId);
133133
}

language/src/analyze_entities.php

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

27-
use Google\Cloud\Language\LanguageClient;
28-
27+
use Google\Cloud\Language\V1beta2\Document;
28+
use Google\Cloud\Language\V1beta2\LanguageServiceClient;
2929
/**
3030
* Find the entities in text.
3131
* ```
@@ -39,26 +39,27 @@
3939
function analyze_entities($text, $projectId = null)
4040
{
4141
// Create the Natural Language client
42-
$language = new LanguageClient([
43-
'projectId' => $projectId,
44-
]);
45-
46-
// Call the analyzeEntities function
47-
$annotation = $language->analyzeEntities($text);
48-
49-
// Print out information about each entity
50-
$entities = $annotation->entities();
51-
foreach ($entities as $entity) {
52-
printf('Name: %s' . PHP_EOL, $entity['name']);
53-
printf('Type: %s' . PHP_EOL, $entity['type']);
54-
printf('Salience: %s' . PHP_EOL, $entity['salience']);
55-
if (array_key_exists('wikipedia_url', $entity['metadata'])) {
56-
printf('Wikipedia URL: %s' . PHP_EOL, $entity['metadata']['wikipedia_url']);
42+
$languageServiceClient = new LanguageServiceClient(['projectId' => $projectId]);
43+
try {
44+
$document = new Document();
45+
// Add text as content and set document type to PLAIN_TEXT, otherwise you get a Code: 3 INVALID_ARGUMENT error
46+
$document->setContent($text)->setType(1);
47+
// Call the analyzeEntities function
48+
$response = $languageServiceClient->analyzeEntities($document, []);
49+
$entities = $response->getEntities();
50+
// Print out information about each entity
51+
foreach ($entities as $entity) {
52+
printf('Name: %s' . PHP_EOL, $entity->getName());
53+
printf('Type: %s' . PHP_EOL, $entity->getType());
54+
printf('Salience: %s' . PHP_EOL, $entity->getSalience());
55+
printf('Wikipedia URL: %s' . PHP_EOL, $entity->getMetadata()->offsetGet('wikipedia_url'));
56+
printf('Knowledge Graph MID: %s' . PHP_EOL, $entity->getMetadata()->offsetGet('mid'));
57+
printf(PHP_EOL);
5758
}
58-
if (array_key_exists('mid', $entity['metadata'])) {
59-
printf('Knowledge Graph MID: %s' . PHP_EOL, $entity['metadata']['mid']);
60-
}
61-
printf(PHP_EOL);
59+
60+
} finally {
61+
$languageServiceClient->close();
6262
}
63+
6364
}
64-
# [END language_entities_text]
65+
# [END language_entities_text]

language/src/analyze_entities_from_file.php

Lines changed: 25 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -24,48 +24,42 @@
2424
# [START language_entities_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 entities in text stored in a Cloud Storage bucket.
3232
* ```
33-
* analyze_entities_from_file('my-bucket', 'file_with_text.txt');
33+
* analyze_entities_from_file('gs://my-bucket/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_entities_from_file($bucketName, $objectName, $projectId = null)
40+
function analyze_entities_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 analyzeEntities function
54-
$annotation = $language->analyzeEntities($storageObject);
55-
56-
// Print out information about each entity
57-
$entities = $annotation->entities();
58-
foreach ($entities as $entity) {
59-
printf('Name: %s' . PHP_EOL, $entity['name']);
60-
printf('Type: %s' . PHP_EOL, $entity['type']);
61-
printf('Salience: %s' . PHP_EOL, $entity['salience']);
62-
if (array_key_exists('wikipedia_url', $entity['metadata'])) {
63-
printf('Wikipedia URL: %s' . PHP_EOL, $entity['metadata']['wikipedia_url']);
43+
$languageServiceClient = new LanguageServiceClient(['projectId' => $projectId]);
44+
try {
45+
$document = new Document();
46+
// Pass GCS URI and set document type to PLAIN_TEXT, otherwise you get a Code: 3 INVALID_ARGUMENT error
47+
$document->setGcsContentUri($gcsUri)->setType(1);
48+
// Call the analyzeEntities function
49+
$response = $languageServiceClient->analyzeEntities($document, []);
50+
$entities = $response->getEntities();
51+
// Print out information about each entity
52+
foreach ($entities as $entity) {
53+
printf('Name: %s' . PHP_EOL, $entity->getName());
54+
printf('Type: %s' . PHP_EOL, $entity->getType());
55+
printf('Salience: %s' . PHP_EOL, $entity->getSalience());
56+
printf('Wikipedia URL: %s' . PHP_EOL, $entity->getMetadata()->offsetGet('wikipedia_url'));
57+
printf('Knowledge Graph MID: %s' . PHP_EOL, $entity->getMetadata()->offsetGet('mid'));
58+
printf(PHP_EOL);
6459
}
65-
if (array_key_exists('mid', $entity['metadata'])) {
66-
printf('Knowledge Graph MID: %s' . PHP_EOL, $entity['metadata']['mid']);
67-
}
68-
printf(PHP_EOL);
60+
61+
} finally {
62+
$languageServiceClient->close();
6963
}
7064
}
71-
# [END language_entities_gcs]
65+
# [END language_entities_gcs]

0 commit comments

Comments
 (0)