Permalink
Fetching contributors…
Cannot retrieve contributors at this time
64 lines (57 sloc) 2.02 KB
<?php
/**
* Copyright 2017 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/**
* For instructions on how to run the full sample:
*
* @see https://github.com/GoogleCloudPlatform/php-docs-samples/tree/master/language/README.md
*/
# [START language_sentiment_text]
namespace Google\Cloud\Samples\Language;
use Google\Cloud\Language\LanguageClient;
/**
* Find the sentiment in text.
* ```
* analyze_sentiment('Do you know the way to San Jose?');
* ```
*
* @param string $text The text to analyze.
* @param string $projectId (optional) Your Google Cloud Project ID
*
*/
function analyze_sentiment($text, $projectId = null)
{
// Create the Natural Language client
$language = new LanguageClient([
'projectId' => $projectId,
]);
// Call the analyzeSentiment function
$annotation = $language->analyzeSentiment($text);
// Print document and sentence sentiment information
$sentiment = $annotation->sentiment();
printf('Document Sentiment:' . PHP_EOL);
printf(' Magnitude: %s' . PHP_EOL, $sentiment['magnitude']);
printf(' Score: %s' . PHP_EOL, $sentiment['score']);
printf(PHP_EOL);
foreach ($annotation->sentences() as $sentence) {
printf('Sentence: %s' . PHP_EOL, $sentence['text']['content']);
printf('Sentence Sentiment:' . PHP_EOL);
printf(' Magnitude: %s' . PHP_EOL, $sentence['sentiment']['magnitude']);
printf(' Score: %s' . PHP_EOL, $sentence['sentiment']['score']);
printf(PHP_EOL);
}
}
# [END language_sentiment_text]