Last active
October 11, 2019 19:34
-
-
Save gabidavila/6385379e29ce3797b585fa67226162e5 to your computer and use it in GitHub Desktop.
Cloud Run example.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
FROM composer:latest as composer | |
COPY src /var/www/html | |
WORKDIR /var/www/html | |
ENV COMPOSER_ALLOW_SUPERUSER 1 | |
RUN composer install && composer dump-autoload | |
RUN composer require google/cloud-language | |
FROM php:apache-stretch | |
COPY --from=composer /var/www/html . | |
RUN docker-php-ext-install bcmath | |
RUN sed -i 's/80/${PORT}/g' /etc/apache2/sites-available/000-default.conf /etc/apache2/ports.conf |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
require 'vendor/autoload.php'; | |
use Google\Cloud\Language\V1beta2\Document; | |
use Google\Cloud\Language\V1beta2\Document\Type; | |
use Google\Cloud\Language\V1beta2\LanguageServiceClient; | |
/** | |
* 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) | |
{ | |
$languageServiceClient = new LanguageServiceClient(['projectId' => $projectId]); | |
try { | |
// Create a new Document | |
$document = new Document(); | |
// Pass GCS URI and set document type to PLAIN_TEXT | |
$document->setContent($text)->setType(Type::PLAIN_TEXT); | |
// Call the analyzeSentiment function | |
$response = $languageServiceClient->analyzeSentiment($document); | |
$document_sentiment = $response->getDocumentSentiment(); | |
// Print document information | |
echo "<p><b>Full Sentence:</b> " . $text . "</p>"; | |
echo "<ul>"; | |
printf("<li>" . '<b>Document Sentiment:</b>' . "</li>"); | |
printf("<li>" . '<b>Magnitude:</b> %s' . "</li>", $document_sentiment->getMagnitude()); | |
printf("<li>" . '<b>Score:</b> %s' . "</li>", $document_sentiment->getScore()); | |
$sentences = $response->getSentences(); | |
echo "<li><b><i>Sentences:</i></b></li>"; | |
echo "<ul>"; | |
foreach ($sentences as $sentence) { | |
printf("<li>" . '<i>Sentence:</i> %s' . "</li>", $sentence->getText()->getContent()); | |
printf("<li>" . '<i>Sentence Sentiment:</i>' . "</li>"); | |
$sentiment = $sentence->getSentiment(); | |
if ($sentiment) { | |
printf("<li>" . '<i>Entity Magnitude:</i> %s' . "</li>", $sentiment->getMagnitude()); | |
printf("<li>" . '<i>Entity Score:</i> %s' . "</li>", $sentiment->getScore()); | |
} | |
} | |
echo "</ul>"; | |
echo "</ul>"; | |
} finally { | |
$languageServiceClient->close(); | |
} | |
} | |
if($_GET['text'] && strlen($_GET['text']) > 0) { | |
analyze_sentiment($_GET['text']); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment