in language/src/analyze_all_from_file.php [39:112]
function analyze_all_from_file(string $uri): void
{
// Create the Natural Language client
$languageServiceClient = new LanguageServiceClient();
// Create a new Document, pass GCS URI and set type to PLAIN_TEXT
$document = (new Document())
->setGcsContentUri($uri)
->setType(Type::PLAIN_TEXT);
// Set Features to extract ['entities', 'syntax', 'sentiment']
$features = (new Features())
->setExtractEntities(true)
->setExtractSyntax(true)
->setExtractDocumentSentiment(true);
// Collect annotations
$request = (new AnnotateTextRequest())
->setDocument($document)
->setFeatures($features);
$response = $languageServiceClient->annotateText($request);
// Process Entities
$entities = $response->getEntities();
foreach ($entities as $entity) {
printf('Name: %s' . PHP_EOL, $entity->getName());
printf('Type: %s' . PHP_EOL, EntityType::name($entity->getType()));
printf('Salience: %s' . PHP_EOL, $entity->getSalience());
if ($entity->getMetadata()->offsetExists('wikipedia_url')) {
printf('Wikipedia URL: %s' . PHP_EOL, $entity->getMetadata()->offsetGet('wikipedia_url'));
}
if ($entity->getMetadata()->offsetExists('mid')) {
printf('Knowledge Graph MID: %s' . PHP_EOL, $entity->getMetadata()->offsetGet('mid'));
}
printf('Mentions:' . PHP_EOL);
foreach ($entity->getMentions() as $mention) {
printf(' Begin Offset: %s' . PHP_EOL, $mention->getText()->getBeginOffset());
printf(' Content: %s' . PHP_EOL, $mention->getText()->getContent());
printf(' Mention Type: %s' . PHP_EOL, MentionType::name($mention->getType()));
printf(PHP_EOL);
}
printf(PHP_EOL);
}
// Process Sentiment
$document_sentiment = $response->getDocumentSentiment();
// Print document information
printf('Document Sentiment:' . PHP_EOL);
printf(' Magnitude: %s' . PHP_EOL, $document_sentiment->getMagnitude());
printf(' Score: %s' . PHP_EOL, $document_sentiment->getScore());
printf(PHP_EOL);
$sentences = $response->getSentences();
foreach ($sentences as $sentence) {
printf('Sentence: %s' . PHP_EOL, $sentence->getText()->getContent());
printf('Sentence Sentiment:' . PHP_EOL);
$sentiment = $sentence->getSentiment();
if ($sentiment) {
printf('Entity Magnitude: %s' . PHP_EOL, $sentiment->getMagnitude());
printf('Entity Score: %s' . PHP_EOL, $sentiment->getScore());
}
printf(PHP_EOL);
}
// Process Syntax
$tokens = $response->getTokens();
// Print out information about each entity
foreach ($tokens as $token) {
printf('Token text: %s' . PHP_EOL, $token->getText()->getContent());
printf('Token part of speech: %s' . PHP_EOL, Tag::name($token->getPartOfSpeech()->getTag()));
printf(PHP_EOL);
}
}