diff --git a/language/api/composer.json b/language/api/composer.json index fbd29b50d9..649ab0c767 100644 --- a/language/api/composer.json +++ b/language/api/composer.json @@ -3,7 +3,8 @@ "google/cloud-language": "^0.4", "symfony/console": "^3.0", "google/cloud-storage": "^1.0", - "symfony/yaml": "^3.2" + "google/gax": "^0.21.2", + "google/proto-client": "^0.22.0" }, "require-dev": { "phpunit/phpunit": "~4.0" @@ -20,7 +21,9 @@ "src/analyze_sentiment.php", "src/analyze_sentiment_from_file.php", "src/analyze_syntax.php", - "src/analyze_syntax_from_file.php" + "src/analyze_syntax_from_file.php", + "src/analyze_entity_sentiment.php", + "src/analyze_entity_sentiment_from_file.php" ] } } diff --git a/language/api/composer.lock b/language/api/composer.lock index 7c406bbe95..9b7fa340f3 100644 --- a/language/api/composer.lock +++ b/language/api/composer.lock @@ -4,7 +4,7 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#composer-lock-the-lock-file", "This file is @generated automatically" ], - "content-hash": "be26d5789fe87eae9a0453cc55c4e6c5", + "content-hash": "a2007804cb057547667f37a092dcbe3b", "packages": [ { "name": "firebase/php-jwt", @@ -234,6 +234,172 @@ "description": "Cloud Storage Client for PHP", "time": "2017-07-12T18:33:11+00:00" }, + { + "name": "google/gax", + "version": "0.21.2", + "source": { + "type": "git", + "url": "https://github.com/googleapis/gax-php.git", + "reference": "74e33a8c8532d27e80018d48b133d1caacf151fc" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/googleapis/gax-php/zipball/74e33a8c8532d27e80018d48b133d1caacf151fc", + "reference": "74e33a8c8532d27e80018d48b133d1caacf151fc", + "shasum": "" + }, + "require": { + "google/auth": "~0.9|^1.0", + "google/protobuf": "^3.3.2", + "grpc/grpc": "^1.4", + "php": ">=5.5" + }, + "require-dev": { + "phpunit/phpunit": "4.8.*", + "squizlabs/php_codesniffer": "2.*" + }, + "type": "library", + "autoload": { + "psr-4": { + "Google\\GAX\\": "src/", + "Google\\GAX\\UnitTests\\": "tests/", + "Google\\": "src/generated/Google/", + "GPBMetadata\\": "src/generated/GPBMetadata/" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "BSD-3-Clause" + ], + "description": "Google API Extensions for PHP", + "homepage": "https://github.com/googleapis/gax-php", + "keywords": [ + "google" + ], + "time": "2017-07-17T20:12:04+00:00" + }, + { + "name": "google/proto-client", + "version": "0.22.0", + "source": { + "type": "git", + "url": "https://github.com/googleapis/proto-client-php.git", + "reference": "a0b9515c8321c03a8e43293c41f76e1a7418570a" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/googleapis/proto-client-php/zipball/a0b9515c8321c03a8e43293c41f76e1a7418570a", + "reference": "a0b9515c8321c03a8e43293c41f76e1a7418570a", + "shasum": "" + }, + "require": { + "google/protobuf": "^3.3.2", + "php": ">=5.5" + }, + "require-dev": { + "google/gax": ">=0.20.0", + "phpunit/phpunit": "4.8.*" + }, + "type": "library", + "autoload": { + "psr-4": { + "Google\\": "src/Google/", + "GPBMetadata\\": "src/GPBMetadata/" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "BSD-3-Clause" + ], + "description": "Generated proto and gRPC classes for Google Cloud Platform in PHP", + "homepage": "https://github.com/googleapis/proto-client-php", + "keywords": [ + "google" + ], + "time": "2017-07-28T16:22:59+00:00" + }, + { + "name": "google/protobuf", + "version": "v3.4.0", + "source": { + "type": "git", + "url": "https://github.com/google/protobuf.git", + "reference": "80a37e0782d2d702d52234b62dd4b9ec74fd2c95" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/google/protobuf/zipball/80a37e0782d2d702d52234b62dd4b9ec74fd2c95", + "reference": "80a37e0782d2d702d52234b62dd4b9ec74fd2c95", + "shasum": "" + }, + "require": { + "php": ">=5.5.0" + }, + "require-dev": { + "phpunit/phpunit": ">=4.8.0" + }, + "suggest": { + "ext-bcmath": "Need to support JSON deserialization" + }, + "type": "library", + "autoload": { + "psr-4": { + "Google\\Protobuf\\": "php/src/Google/Protobuf", + "GPBMetadata\\Google\\Protobuf\\": "php/src/GPBMetadata/Google/Protobuf" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "BSD-3-Clause" + ], + "description": "proto library for PHP", + "homepage": "https://developers.google.com/protocol-buffers/", + "keywords": [ + "proto" + ], + "time": "2017-08-15T23:39:12+00:00" + }, + { + "name": "grpc/grpc", + "version": "v1.4.6", + "source": { + "type": "git", + "url": "https://github.com/grpc/grpc.git", + "reference": "d006cbc2edcc9d5d33ed229162adb6cfcaf327e6" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/grpc/grpc/zipball/d006cbc2edcc9d5d33ed229162adb6cfcaf327e6", + "reference": "d006cbc2edcc9d5d33ed229162adb6cfcaf327e6", + "shasum": "" + }, + "require": { + "php": ">=5.5.0" + }, + "require-dev": { + "google/auth": "v0.9" + }, + "suggest": { + "ext-protobuf": "For better performance, install the protobuf C extension.", + "google/protobuf": "To get started using grpc quickly, install the native protobuf library." + }, + "type": "library", + "autoload": { + "psr-4": { + "Grpc\\": "src/php/lib/Grpc/" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "BSD-3-Clause" + ], + "description": "gRPC library for PHP", + "homepage": "http://grpc.io/", + "keywords": [ + "rpc" + ], + "time": "2017-08-17T01:17:06+00:00" + }, { "name": "guzzlehttp/guzzle", "version": "6.3.0", @@ -807,16 +973,16 @@ }, { "name": "symfony/polyfill-mbstring", - "version": "v1.4.0", + "version": "v1.5.0", "source": { "type": "git", "url": "https://github.com/symfony/polyfill-mbstring.git", - "reference": "f29dca382a6485c3cbe6379f0c61230167681937" + "reference": "7c8fae0ac1d216eb54349e6a8baa57d515fe8803" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/symfony/polyfill-mbstring/zipball/f29dca382a6485c3cbe6379f0c61230167681937", - "reference": "f29dca382a6485c3cbe6379f0c61230167681937", + "url": "https://api.github.com/repos/symfony/polyfill-mbstring/zipball/7c8fae0ac1d216eb54349e6a8baa57d515fe8803", + "reference": "7c8fae0ac1d216eb54349e6a8baa57d515fe8803", "shasum": "" }, "require": { @@ -828,7 +994,7 @@ "type": "library", "extra": { "branch-alias": { - "dev-master": "1.4-dev" + "dev-master": "1.5-dev" } }, "autoload": { @@ -862,62 +1028,7 @@ "portable", "shim" ], - "time": "2017-06-09T14:24:12+00:00" - }, - { - "name": "symfony/yaml", - "version": "v3.3.6", - "source": { - "type": "git", - "url": "https://github.com/symfony/yaml.git", - "reference": "ddc23324e6cfe066f3dd34a37ff494fa80b617ed" - }, - "dist": { - "type": "zip", - "url": "https://api.github.com/repos/symfony/yaml/zipball/ddc23324e6cfe066f3dd34a37ff494fa80b617ed", - "reference": "ddc23324e6cfe066f3dd34a37ff494fa80b617ed", - "shasum": "" - }, - "require": { - "php": ">=5.5.9" - }, - "require-dev": { - "symfony/console": "~2.8|~3.0" - }, - "suggest": { - "symfony/console": "For validating YAML files using the lint command" - }, - "type": "library", - "extra": { - "branch-alias": { - "dev-master": "3.3-dev" - } - }, - "autoload": { - "psr-4": { - "Symfony\\Component\\Yaml\\": "" - }, - "exclude-from-classmap": [ - "/Tests/" - ] - }, - "notification-url": "https://packagist.org/downloads/", - "license": [ - "MIT" - ], - "authors": [ - { - "name": "Fabien Potencier", - "email": "fabien@symfony.com" - }, - { - "name": "Symfony Community", - "homepage": "https://symfony.com/contributors" - } - ], - "description": "Symfony Yaml Component", - "homepage": "https://symfony.com/", - "time": "2017-07-23T12:43:26+00:00" + "time": "2017-06-14T15:44:48+00:00" } ], "packages-dev": [ @@ -1031,16 +1142,16 @@ }, { "name": "phpdocumentor/reflection-docblock", - "version": "3.2.1", + "version": "3.2.2", "source": { "type": "git", "url": "https://github.com/phpDocumentor/ReflectionDocBlock.git", - "reference": "183824db76118b9dddffc7e522b91fa175f75119" + "reference": "4aada1f93c72c35e22fb1383b47fee43b8f1d157" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/phpDocumentor/ReflectionDocBlock/zipball/183824db76118b9dddffc7e522b91fa175f75119", - "reference": "183824db76118b9dddffc7e522b91fa175f75119", + "url": "https://api.github.com/repos/phpDocumentor/ReflectionDocBlock/zipball/4aada1f93c72c35e22fb1383b47fee43b8f1d157", + "reference": "4aada1f93c72c35e22fb1383b47fee43b8f1d157", "shasum": "" }, "require": { @@ -1072,7 +1183,7 @@ } ], "description": "With this component, a library can provide support for annotations via DocBlocks or otherwise retrieve information that is embedded in a DocBlock.", - "time": "2017-08-04T20:55:59+00:00" + "time": "2017-08-08T06:39:58+00:00" }, { "name": "phpdocumentor/type-resolver", @@ -1932,6 +2043,61 @@ "homepage": "https://github.com/sebastianbergmann/version", "time": "2015-06-21T13:59:46+00:00" }, + { + "name": "symfony/yaml", + "version": "v3.3.6", + "source": { + "type": "git", + "url": "https://github.com/symfony/yaml.git", + "reference": "ddc23324e6cfe066f3dd34a37ff494fa80b617ed" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/symfony/yaml/zipball/ddc23324e6cfe066f3dd34a37ff494fa80b617ed", + "reference": "ddc23324e6cfe066f3dd34a37ff494fa80b617ed", + "shasum": "" + }, + "require": { + "php": ">=5.5.9" + }, + "require-dev": { + "symfony/console": "~2.8|~3.0" + }, + "suggest": { + "symfony/console": "For validating YAML files using the lint command" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "3.3-dev" + } + }, + "autoload": { + "psr-4": { + "Symfony\\Component\\Yaml\\": "" + }, + "exclude-from-classmap": [ + "/Tests/" + ] + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Fabien Potencier", + "email": "fabien@symfony.com" + }, + { + "name": "Symfony Community", + "homepage": "https://symfony.com/contributors" + } + ], + "description": "Symfony Yaml Component", + "homepage": "https://symfony.com/", + "time": "2017-07-23T12:43:26+00:00" + }, { "name": "webmozart/assert", "version": "1.2.0", diff --git a/language/api/language.php b/language/api/language.php index 19f5c271bf..da1e88e150 100644 --- a/language/api/language.php +++ b/language/api/language.php @@ -1,6 +1,6 @@ php %command.full_name% gs://my_bucket/file_with_text.txt Example: - php %command.full_name% "John took a walk." -sentences: - - - text: - content: 'John took a walk.' - beginOffset: 0 - sentiment: - magnitude: 0 - score: 0 -tokens: - - - text: - content: John - beginOffset: 0 - partOfSpeech: - tag: NOUN - aspect: ASPECT_UNKNOWN - case: CASE_UNKNOWN - ... - dependencyEdge: - headTokenIndex: 1 - label: NSUBJ - lemma: John - ... -entities: - - - name: John - type: PERSON - metadata: { } - salience: 0.67526394 - mentions: - - - text: - content: John - beginOffset: 0 - type: PROPER - ... -documentSentiment: - magnitude: 0 - score: 0 -language: en + php %command.full_name% "Barack Obama lives in Washington D.C." +Name: Barack Obama +Type: PERSON +Salience: 0.676553 +Wikipedia URL: https://en.wikipedia.org/wiki/Barack_Obama +Knowledge Graph MID: /m/02mjmr +Mentions: + Begin Offset: 0 + Content: Barack Obama + Mention Type: PROPER + + +Name: Washington D.C. +Type: LOCATION +Salience: 0.323447 +Wikipedia URL: https://en.wikipedia.org/wiki/Washington,_D.C. +Knowledge Graph MID: /m/0rh6k +Mentions: + Begin Offset: 22 + Content: Washington D.C. + Mention Type: PROPER +... EOF ) ->setCode(function ($input, $output) { @@ -99,11 +79,10 @@ // Regex to match a Cloud Storage path as the first argument // e.g "gs://my-bucket/file_with_text.txt" if (preg_match('/^gs:\/\/([a-z0-9\._\-]+)\/(\S+)$/', $content, $matches)) { - $annotation = analyze_all_from_file($matches[1], $matches[2], $projectId); + analyze_all_from_file($matches[1], $matches[2], $projectId); } else { - $annotation = analyze_all($content, $projectId); + analyze_all($content, $projectId); } - $output->write(Yaml::dump($annotation->info(), 6)); }) ); @@ -119,21 +98,27 @@ php %command.full_name% gs://my_bucket/file_with_text.txt Example: - php %command.full_name% "John took a walk." -entities: - - - name: John - type: PERSON - metadata: { } - salience: 0.67526394 - mentions: - - - text: - content: John - beginOffset: 0 - type: PROPER - ... -language: en + php %command.full_name% "Barack Obama lives in Washington D.C." +Name: Barack Obama +Type: PERSON +Salience: 0.676553 +Wikipedia URL: https://en.wikipedia.org/wiki/Barack_Obama +Knowledge Graph MID: /m/02mjmr +Mentions: + Begin Offset: 0 + Content: Barack Obama + Mention Type: PROPER + + +Name: Washington D.C. +Type: LOCATION +Salience: 0.323447 +Wikipedia URL: https://en.wikipedia.org/wiki/Washington,_D.C. +Knowledge Graph MID: /m/0rh6k +Mentions: + Begin Offset: 22 + Content: Washington D.C. + Mention Type: PROPER EOF ) ->setCode(function ($input, $output) { @@ -142,11 +127,10 @@ // Regex to match a Cloud Storage path as the first argument // e.g "gs://my-bucket/file_with_text.txt" if (preg_match('/^gs:\/\/([a-z0-9\._\-]+)\/(\S+)$/', $content, $matches)) { - $annotation = analyze_entities_from_file($matches[1], $matches[2], $projectId); + analyze_entities_from_file($matches[1], $matches[2], $projectId); } else { - $annotation = analyze_entities($content, $projectId); + analyze_entities($content, $projectId); } - $output->write(Yaml::dump($annotation->info(), 6)); }) ); @@ -162,19 +146,20 @@ php %command.full_name% gs://my_bucket/file_with_text.txt Example: - php %command.full_name% "John took a walk." -documentSentiment: - magnitude: 0 - score: 0 -language: en -sentences: - - - text: - content: 'John took a walk.' - beginOffset: 0 - sentiment: - magnitude: 0 - score: 0 + php %command.full_name% "I like burgers. I dislike fish." +Document Sentiment: + Magnitude: 1.3 + Score: 0 + +Sentence: I like burgers. +Sentence Sentiment: + Magnitude: 0.6 + Score: 0.6 + +Sentence: I dislike fish. +Sentence Sentiment: + Magnitude: 0.6 + Score: -0.6 EOF ) ->setCode(function ($input, $output) { @@ -183,11 +168,10 @@ // Regex to match a Cloud Storage path as the first argument // e.g "gs://my-bucket/file_with_text.txt" if (preg_match('/^gs:\/\/([a-z0-9\._\-]+)\/(\S+)$/', $content, $matches)) { - $annotation = analyze_sentiment_from_file($matches[1], $matches[2], $projectId); + analyze_sentiment_from_file($matches[1], $matches[2], $projectId); } else { - $annotation = analyze_sentiment($content, $projectId); + analyze_sentiment($content, $projectId); } - $output->write(Yaml::dump($annotation->info(), 6)); }) ); @@ -203,31 +187,70 @@ php %command.full_name% gs://my_bucket/file_with_text.txt Example: - php %command.full_name% "John took a walk." -sentences: - - - text: - content: 'John took a walk.' - beginOffset: 0 -tokens: - - - text: - content: John - beginOffset: 0 - partOfSpeech: - tag: NOUN - aspect: ASPECT_UNKNOWN - case: CASE_UNKNOWN - form: FORM_UNKNOWN - ... - dependencyEdge: - headTokenIndex: 1 - label: NSUBJ - lemma: John - - - ... -language: en -entities: { } + php %command.full_name% "Barack Obama lives in Washington D.C." +Token text: Barack +Token part of speech: NOUN + +Token text: Obama +Token part of speech: NOUN + +Token text: lives +Token part of speech: VERB + +Token text: in +Token part of speech: ADP + +Token text: Washington +Token part of speech: NOUN + +Token text: D.C. +Token part of speech: NOUN +EOF + ) + ->setCode(function ($input, $output) { + $projectId = $input->getOption('project'); + $content = implode(' ', (array) $input->getArgument('content')); + // Regex to match a Cloud Storage path as the first argument + // e.g "gs://my-bucket/file_with_text.txt" + if (preg_match('/^gs:\/\/([a-z0-9\._\-]+)\/(\S+)$/', $content, $matches)) { + analyze_syntax_from_file($matches[1], $matches[2], $projectId); + } else { + analyze_syntax($content, $projectId); + } + }) +); + +// Analyze Entity Sentiment command +$application->add((new Command('entity-sentiment')) + ->setDefinition($inputDefinition) + ->setDescription('Analyze entity sentiment in text.') + ->setHelp(<<%command.name% command analyzes text using the Google Cloud Natural Language API. + + php %command.full_name% Text to analyze. + + php %command.full_name% gs://my_bucket/file_with_text.txt + +Example: + php %command.full_name% "New York is great. New York is good." +Entity Name: New York +Entity Type: LOCATION +Entity Salience: 1 +Entity Magnitude: 1.7999999523163 +Entity Score: 0 + +Mentions: + Begin Offset: 0 + Content: New York + Mention Type: PROPER + Mention Magnitude: 0.89999997615814 + Mention Score: 0.89999997615814 + + Begin Offset: 17 + Content: New York + Mention Type: PROPER + Mention Magnitude: 0.80000001192093 + Mention Score: -0.80000001192093 EOF ) ->setCode(function ($input, $output) { @@ -236,11 +259,10 @@ // Regex to match a Cloud Storage path as the first argument // e.g "gs://my-bucket/file_with_text.txt" if (preg_match('/^gs:\/\/([a-z0-9\._\-]+)\/(\S+)$/', $content, $matches)) { - $annotation = analyze_syntax_from_file($matches[1], $matches[2], $projectId); + analyze_entity_sentiment_from_file($content, $projectId); } else { - $annotation = analyze_syntax($content, $projectId); + analyze_entity_sentiment($content, $projectId); } - $output->write(Yaml::dump($annotation->info(), 6)); }) ); diff --git a/language/api/src/analyze_all.php b/language/api/src/analyze_all.php index 60220183ca..37f2c0f352 100644 --- a/language/api/src/analyze_all.php +++ b/language/api/src/analyze_all.php @@ -1,6 +1,6 @@ annotateText($text, [ 'features' => ['entities', 'syntax', 'sentiment'] ]); - return $annotation; + + // Print out information about each entity + $entities = $annotation->entities(); + foreach ($entities as $entity) { + printf('Name: %s' . PHP_EOL, $entity['name']); + printf('Type: %s' . PHP_EOL, $entity['type']); + printf('Salience: %s' . PHP_EOL, $entity['salience']); + if (array_key_exists('wikipedia_url', $entity['metadata'])) { + printf('Wikipedia URL: %s' . PHP_EOL, $entity['metadata']['wikipedia_url']); + } + if (array_key_exists('mid', $entity['metadata'])) { + printf('Knowledge Graph MID: %s' . PHP_EOL, $entity['metadata']['mid']); + } + printf('Mentions:' . PHP_EOL); + foreach ($entity['mentions'] as $mention) { + printf(' Begin Offset: %s' . PHP_EOL, $mention['text']['beginOffset']); + printf(' Content: %s' . PHP_EOL, $mention['text']['content']); + printf(' Mention Type: %s' . PHP_EOL, $mention['type']); + printf(PHP_EOL); + } + printf(PHP_EOL); + } + + // 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); + } + + // Print syntax information. See https://cloud.google.com/natural-language/docs/reference/rest/v1/Token + // to learn about more information you can extract from Token objects. + $tokens = $annotation->tokens(); + foreach ($tokens as $token) { + printf('Token text: %s' . PHP_EOL, $token['text']['content']); + printf('Token part of speech: %s' . PHP_EOL, $token['partOfSpeech']['tag']); + printf(PHP_EOL); + } } # [END analyze_all] diff --git a/language/api/src/analyze_all_from_file.php b/language/api/src/analyze_all_from_file.php index b3cac46b17..f18d37d4af 100644 --- a/language/api/src/analyze_all_from_file.php +++ b/language/api/src/analyze_all_from_file.php @@ -1,6 +1,6 @@ $projectId, ]); + + // Call the annotateText function $annotation = $language->annotateText($storageObject, [ 'features' => ['entities', 'syntax', 'sentiment'] ]); - return $annotation; + + // Print out information about each entity + $entities = $annotation->entities(); + foreach ($entities as $entity) { + printf('Name: %s' . PHP_EOL, $entity['name']); + printf('Type: %s' . PHP_EOL, $entity['type']); + printf('Salience: %s' . PHP_EOL, $entity['salience']); + if (array_key_exists('wikipedia_url', $entity['metadata'])) { + printf('Wikipedia URL: %s' . PHP_EOL, $entity['metadata']['wikipedia_url']); + } + if (array_key_exists('mid', $entity['metadata'])) { + printf('Knowledge Graph MID: %s' . PHP_EOL, $entity['metadata']['mid']); + } + printf('Mentions:' . PHP_EOL); + foreach ($entity['mentions'] as $mention) { + printf(' Begin Offset: %s' . PHP_EOL, $mention['text']['beginOffset']); + printf(' Content: %s' . PHP_EOL, $mention['text']['content']); + printf(' Mention Type: %s' . PHP_EOL, $mention['type']); + printf(PHP_EOL); + } + printf(PHP_EOL); + } + + // 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); + } + + // Print syntax information. See https://cloud.google.com/natural-language/docs/reference/rest/v1/Token + // to learn about more information you can extract from Token objects. + $tokens = $annotation->tokens(); + foreach ($tokens as $token) { + printf('Token text: %s' . PHP_EOL, $token['text']['content']); + printf('Token part of speech: %s' . PHP_EOL, $token['partOfSpeech']['tag']); + printf(PHP_EOL); + } } # [END analyze_all_from_file] diff --git a/language/api/src/analyze_entities.php b/language/api/src/analyze_entities.php index 064b6d0517..4d25ba5e1e 100644 --- a/language/api/src/analyze_entities.php +++ b/language/api/src/analyze_entities.php @@ -1,6 +1,6 @@ $projectId, ]); + // Call the analyzeEntities function $annotation = $language->analyzeEntities($text); - return $annotation; + + // Print out information about each entity + $entities = $annotation->entities(); + foreach ($entities as $entity) { + printf('Name: %s' . PHP_EOL, $entity['name']); + printf('Type: %s' . PHP_EOL, $entity['type']); + printf('Salience: %s' . PHP_EOL, $entity['salience']); + if (array_key_exists('wikipedia_url', $entity['metadata'])) { + printf('Wikipedia URL: %s' . PHP_EOL, $entity['metadata']['wikipedia_url']); + } + if (array_key_exists('mid', $entity['metadata'])) { + printf('Knowledge Graph MID: %s' . PHP_EOL, $entity['metadata']['mid']); + } + printf(PHP_EOL); + } } # [END analyze_entities] diff --git a/language/api/src/analyze_entities_from_file.php b/language/api/src/analyze_entities_from_file.php index 5e01d176d9..26334fee7e 100644 --- a/language/api/src/analyze_entities_from_file.php +++ b/language/api/src/analyze_entities_from_file.php @@ -1,6 +1,6 @@ bucket($bucketName); $storageObject = $bucket->object($objectName); - // Call the Natural Language client + // Create the Natural Language client $language = new LanguageClient([ 'projectId' => $projectId, ]); + + // Call the analyzeEntities function $annotation = $language->analyzeEntities($storageObject); - return $annotation; + + // Print out information about each entity + $entities = $annotation->entities(); + foreach ($entities as $entity) { + printf('Name: %s' . PHP_EOL, $entity['name']); + printf('Type: %s' . PHP_EOL, $entity['type']); + printf('Salience: %s' . PHP_EOL, $entity['salience']); + if (array_key_exists('wikipedia_url', $entity['metadata'])) { + printf('Wikipedia URL: %s' . PHP_EOL, $entity['metadata']['wikipedia_url']); + } + if (array_key_exists('mid', $entity['metadata'])) { + printf('Knowledge Graph MID: %s' . PHP_EOL, $entity['metadata']['mid']); + } + printf(PHP_EOL); + } } # [END analyze_entities_from_file] diff --git a/language/api/src/analyze_entity_sentiment.php b/language/api/src/analyze_entity_sentiment.php new file mode 100644 index 0000000000..b88c60d287 --- /dev/null +++ b/language/api/src/analyze_entity_sentiment.php @@ -0,0 +1,71 @@ + $projectId, + ]); + $document = new Document(); + $document->setType(Document_Type::PLAIN_TEXT); + $document->setContent($text); + $encodingType = EncodingType::UTF16; + + // Call the analyzeEntitySentiment function + $response = $language->analyzeEntitySentiment($document, $encodingType); + $entities = $response->getEntities(); + + $entity_types = array('UNKNOWN', 'PERSON', 'LOCATION', 'ORGANIZATION', 'EVENT', + 'WORK_OF_ART', 'CONSUMER_GOOD', 'OTHER'); + + // Print out information about each entity + foreach ($entities as $entity) { + printf('Entity Name: %s' . PHP_EOL, $entity->getName()); + printf('Entity Type: %s' . PHP_EOL, $entity_types[$entity->getType()]); + printf('Entity Salience: %s' . PHP_EOL, $entity->getSalience()); + printf('Entity Magnitude: %s' . PHP_EOL, $entity->getSentiment()->getMagnitude()); + printf('Entity Score: %s' . PHP_EOL, $entity->getSentiment()->getScore()); + printf(PHP_EOL); + } +} +# [END analyze_entity_sentiment] diff --git a/language/api/src/analyze_entity_sentiment_from_file.php b/language/api/src/analyze_entity_sentiment_from_file.php new file mode 100644 index 0000000000..93156be1bc --- /dev/null +++ b/language/api/src/analyze_entity_sentiment_from_file.php @@ -0,0 +1,71 @@ + $projectId, + ]); + $document = new Document(); + $document->setType(Document_Type::PLAIN_TEXT); + $document->setGcsContentUri($cloud_storage_uri); + $encodingType = EncodingType::UTF16; + + // Call the analyzeEntitySentiment function + $response = $language->analyzeEntitySentiment($document, $encodingType); + $entities = $response->getEntities(); + + $entity_types = array('UNKNOWN', 'PERSON', 'LOCATION', 'ORGANIZATION', 'EVENT', + 'WORK_OF_ART', 'CONSUMER_GOOD', 'OTHER'); + + // Print out information about each entity + foreach ($entities as $entity) { + printf('Entity Name: %s' . PHP_EOL, $entity->getName()); + printf('Entity Type: %s' . PHP_EOL, $entity_types[$entity->getType()]); + printf('Entity Salience: %s' . PHP_EOL, $entity->getSalience()); + printf('Entity Magnitude: %s' . PHP_EOL, $entity->getSentiment()->getMagnitude()); + printf('Entity Score: %s' . PHP_EOL, $entity->getSentiment()->getScore()); + printf(PHP_EOL); + } +} +# [END analyze_entity_sentiment_from_file] diff --git a/language/api/src/analyze_sentiment.php b/language/api/src/analyze_sentiment.php index 2d150b1e61..457392ee87 100644 --- a/language/api/src/analyze_sentiment.php +++ b/language/api/src/analyze_sentiment.php @@ -1,6 +1,6 @@ $projectId, ]); + // Call the analyzeSentiment function $annotation = $language->analyzeSentiment($text); - return $annotation; + + // 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 analyze_sentiment] diff --git a/language/api/src/analyze_sentiment_from_file.php b/language/api/src/analyze_sentiment_from_file.php index 72dd271a1e..e53c72b57d 100644 --- a/language/api/src/analyze_sentiment_from_file.php +++ b/language/api/src/analyze_sentiment_from_file.php @@ -1,6 +1,6 @@ bucket($bucketName); $storageObject = $bucket->object($objectName); - // Call the Natural Language client + // Create the Natural Language client $language = new LanguageClient([ 'projectId' => $projectId, ]); + + // Call the analyzeSentiment function $annotation = $language->analyzeSentiment($storageObject); - return $annotation; + + // 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 analyze_sentiment_from_file] diff --git a/language/api/src/analyze_syntax.php b/language/api/src/analyze_syntax.php index 461ce5dddb..5162e1c967 100644 --- a/language/api/src/analyze_syntax.php +++ b/language/api/src/analyze_syntax.php @@ -1,6 +1,6 @@ $projectId, ]); + // Call the analyzeSyntax function $annotation = $language->analyzeSyntax($text); - return $annotation; + + // Print syntax information. See https://cloud.google.com/natural-language/docs/reference/rest/v1/Token + // to learn about more information you can extract from Token objects. + $tokens = $annotation->tokens(); + foreach ($tokens as $token) { + printf('Token text: %s' . PHP_EOL, $token['text']['content']); + printf('Token part of speech: %s' . PHP_EOL, $token['partOfSpeech']['tag']); + printf(PHP_EOL); + } } # [END analyze_syntax] diff --git a/language/api/src/analyze_syntax_from_file.php b/language/api/src/analyze_syntax_from_file.php index a0285778e3..0c315f052f 100644 --- a/language/api/src/analyze_syntax_from_file.php +++ b/language/api/src/analyze_syntax_from_file.php @@ -1,6 +1,6 @@ bucket($bucketName); $storageObject = $bucket->object($objectName); - // Call the Natural Language client + // Create the Natural Language client $language = new LanguageClient([ 'projectId' => $projectId, ]); + + // Call the analyzeSyntax function $annotation = $language->analyzeSyntax($storageObject); - return $annotation; + + // Print syntax information. See https://cloud.google.com/natural-language/docs/reference/rest/v1/Token + // to learn about more information you can extract from Token objects. + $tokens = $annotation->tokens(); + foreach ($tokens as $token) { + printf('Token text: %s' . PHP_EOL, $token['text']['content']); + printf('Token part of speech: %s' . PHP_EOL, $token['partOfSpeech']['tag']); + printf(PHP_EOL); + } } # [END analyze_syntax_from_file] diff --git a/language/api/test/lanaguageTest.php b/language/api/test/lanaguageTest.php deleted file mode 100644 index 466923e02f..0000000000 --- a/language/api/test/lanaguageTest.php +++ /dev/null @@ -1,145 +0,0 @@ - 0; - } - - public function setUp() - { - if (!self::$hasCredentials) { - $this->markTestSkipped('No application credentials were found.'); - } - } - - public function testAll() - { - $output = $this->runCommand('all', 'Do you know the way to San Jose?'); - - $expectedPatterns = [ - '/content: know/', - '/type: PROPER/' - ]; - - foreach ($expectedPatterns as $expectedPattern) { - $this->assertRegExp($expectedPattern, $output); - } - } - - public function testAllFromStorageObject() - { - if (!$gcsFile = getenv('GOOGLE_LANGUAGE_GCS_FILE')) { - $this->markTestSkipped('No GCS file.'); - } - - $output = $this->runCommand('all', $gcsFile); - - $expectedPatterns = [ - '/content: know/', - '/type: PROPER/' - ]; - - foreach ($expectedPatterns as $expectedPattern) { - $this->assertRegExp($expectedPattern, $output); - } - } - - public function testEntities() - { - $output = $this->runCommand('entities', 'Do you know the way to San Jose?'); - - $this->assertRegExp('/type: PROPER/', $output); - } - - public function testEntitiesFromStorageObject() - { - if (!$gcsFile = getenv('GOOGLE_LANGUAGE_GCS_FILE')) { - $this->markTestSkipped('No GCS file.'); - } - - $output = $this->runCommand('entities', $gcsFile); - - $this->assertRegExp('/type: PROPER/', $output); - } - - public function testSentiment() - { - $output = $this->runCommand('sentiment', 'Do you know the way to San Jose?'); - - $this->assertRegExp('/sentiment/', $output); - } - - public function testSentimentFromStorageObject() - { - if (!$gcsFile = getenv('GOOGLE_LANGUAGE_GCS_FILE')) { - $this->markTestSkipped('No GCS file.'); - } - - $output = $this->runCommand('sentiment', $gcsFile); - - $this->assertRegExp('/sentiment/', $output); - } - - public function testSyntax() - { - $output = $this->runCommand('syntax', 'Do you know the way to San Jose?'); - - $this->assertRegExp('/Do you know the way/', $output); - } - - public function testSyntaxFromStorageObject() - { - if (!$gcsFile = getenv('GOOGLE_LANGUAGE_GCS_FILE')) { - $this->markTestSkipped('No GCS file.'); - } - - $output = $this->runCommand('syntax', $gcsFile); - - $this->assertRegExp('/Do you know the way/', $output); - } - - private function runCommand($commandName, $content) - { - $application = require __DIR__ . '/../language.php'; - $command = $application->get($commandName); - $commandTester = new CommandTester($command); - - $commandTester->execute([ - 'content' => $content, - ], [ - 'interactive' => false - ]); - - return $commandTester->getDisplay(); - } -} diff --git a/language/api/test/languageTest.php b/language/api/test/languageTest.php new file mode 100644 index 0000000000..d66ed510f8 --- /dev/null +++ b/language/api/test/languageTest.php @@ -0,0 +1,243 @@ + 0; + } + + public function setUp() + { + if (!self::$hasCredentials) { + $this->markTestSkipped('No application credentials were found.'); + } + } + + public function testAll() + { + $output = $this->runCommand('all', 'Barack Obama lives in Washington D.C.'); + $this->assertContains('Name: Barack Obama', $output); + $this->assertContains('Type: PERSON', $output); + $this->assertContains('Salience:', $output); + $this->assertContains('Wikipedia URL: https://en.wikipedia.org/wiki/Barack_Obama', $output); + $this->assertContains('Knowledge Graph MID:', $output); + $this->assertContains('Name: Washington D.C.', $output); + $this->assertContains('Wikipedia URL: https://en.wikipedia.org/wiki/Washington,_D.C.', $output); + $this->assertContains('Document Sentiment:', $output); + $this->assertContains('Magnitude:', $output); + $this->assertContains('Score:', $output); + $this->assertContains('Sentence: Barack Obama lives in Washington D.C.', $output); + $this->assertContains('Sentence Sentiment:', $output); + $this->assertContains(' Magnitude:', $output); + $this->assertContains(' Score:', $output); + $this->assertContains('Token text: Barack', $output); + $this->assertContains('Token part of speech: NOUN', $output); + $this->assertContains('Token text: Obama', $output); + $this->assertContains('Token part of speech: NOUN', $output); + $this->assertContains('Token text: lives', $output); + $this->assertContains('Token part of speech: VERB', $output); + $this->assertContains('Token text: in', $output); + $this->assertContains('Token part of speech: ADP', $output); + $this->assertContains('Token text: Washington', $output); + $this->assertContains('Token part of speech: NOUN', $output); + $this->assertContains('Token text: D.C.', $output); + $this->assertContains('Token part of speech: NOUN', $output); + } + + public function testAllFromStorageObject() + { + if (!$gcsFile = getenv('GOOGLE_LANGUAGE_GCS_FILE')) { + $this->markTestSkipped('No GCS file.'); + } + $output = $this->runCommand('all', $gcsFile); + $this->assertContains('Name: Barack Obama', $output); + $this->assertContains('Type: PERSON', $output); + $this->assertContains('Salience:', $output); + $this->assertContains('Wikipedia URL: https://en.wikipedia.org/wiki/Barack_Obama', $output); + $this->assertContains('Knowledge Graph MID:', $output); + $this->assertContains('Name: Washington D.C.', $output); + $this->assertContains('Wikipedia URL: https://en.wikipedia.org/wiki/Washington,_D.C.', $output); + $this->assertContains('Document Sentiment:', $output); + $this->assertContains('Magnitude:', $output); + $this->assertContains('Score:', $output); + $this->assertContains('Sentence: Barack Obama lives in Washington D.C.', $output); + $this->assertContains('Sentence Sentiment:', $output); + $this->assertContains(' Magnitude:', $output); + $this->assertContains(' Score:', $output); + $this->assertContains('Token text: Barack', $output); + $this->assertContains('Token part of speech: NOUN', $output); + $this->assertContains('Token text: Obama', $output); + $this->assertContains('Token part of speech: NOUN', $output); + $this->assertContains('Token text: lives', $output); + $this->assertContains('Token part of speech: VERB', $output); + $this->assertContains('Token text: in', $output); + $this->assertContains('Token part of speech: ADP', $output); + $this->assertContains('Token text: Washington', $output); + $this->assertContains('Token part of speech: NOUN', $output); + $this->assertContains('Token text: D.C.', $output); + $this->assertContains('Token part of speech: NOUN', $output); + } + + public function testEntities() + { + $output = $this->runCommand('entities', 'Barack Obama lives in Washington D.C.'); + $this->assertContains('Name: Barack Obama', $output); + $this->assertContains('Type: PERSON', $output); + $this->assertContains('Salience:', $output); + $this->assertContains('Wikipedia URL: https://en.wikipedia.org/wiki/Barack_Obama', $output); + $this->assertContains('Knowledge Graph MID:', $output); + $this->assertContains('Name: Washington D.C.', $output); + $this->assertContains('Wikipedia URL: https://en.wikipedia.org/wiki/Washington,_D.C.', $output); + } + + + public function testEntitiesFromStorageObject() + { + if (!$gcsFile = getenv('GOOGLE_LANGUAGE_GCS_FILE')) { + $this->markTestSkipped('No GCS file.'); + } + $output = $this->runCommand('entities', $gcsFile); + $this->assertContains('Name: Barack Obama', $output); + $this->assertContains('Type: PERSON', $output); + $this->assertContains('Salience:', $output); + $this->assertContains('Wikipedia URL: https://en.wikipedia.org/wiki/Barack_Obama', $output); + $this->assertContains('Knowledge Graph MID:', $output); + $this->assertContains('Name: Washington D.C.', $output); + $this->assertContains('Wikipedia URL: https://en.wikipedia.org/wiki/Washington,_D.C.', $output); + } + + public function testSentiment() + { + $output = $this->runCommand('sentiment', 'Barack Obama lives in Washington D.C.'); + $this->assertContains('Document Sentiment:', $output); + $this->assertContains('Magnitude:', $output); + $this->assertContains('Score:', $output); + $this->assertContains('Sentence: Barack Obama lives in Washington D.C.', $output); + $this->assertContains('Sentence Sentiment:', $output); + $this->assertContains(' Magnitude:', $output); + $this->assertContains(' Score:', $output); + } + + + public function testSentimentFromStorageObject() + { + if (!$gcsFile = getenv('GOOGLE_LANGUAGE_GCS_FILE')) { + $this->markTestSkipped('No GCS file.'); + } + $output = $this->runCommand('sentiment', $gcsFile); + $this->assertContains('Document Sentiment:', $output); + $this->assertContains('Magnitude:', $output); + $this->assertContains('Score:', $output); + $this->assertContains('Sentence: Barack Obama lives in Washington D.C.', $output); + $this->assertContains('Sentence Sentiment:', $output); + $this->assertContains(' Magnitude:', $output); + $this->assertContains(' Score:', $output); + } + + public function testSyntax() + { + $output = $this->runCommand('syntax', 'Barack Obama lives in Washington D.C.'); + $this->assertContains('Token text: Barack', $output); + $this->assertContains('Token part of speech: NOUN', $output); + $this->assertContains('Token text: Obama', $output); + $this->assertContains('Token part of speech: NOUN', $output); + $this->assertContains('Token text: lives', $output); + $this->assertContains('Token part of speech: VERB', $output); + $this->assertContains('Token text: in', $output); + $this->assertContains('Token part of speech: ADP', $output); + $this->assertContains('Token text: Washington', $output); + $this->assertContains('Token part of speech: NOUN', $output); + $this->assertContains('Token text: D.C.', $output); + $this->assertContains('Token part of speech: NOUN', $output); + } + + public function testSyntaxFromStorageObject() + { + if (!$gcsFile = getenv('GOOGLE_LANGUAGE_GCS_FILE')) { + $this->markTestSkipped('No GCS file.'); + } + $output = $this->runCommand('syntax', $gcsFile); + $this->assertContains('Token text: Barack', $output); + $this->assertContains('Token part of speech: NOUN', $output); + $this->assertContains('Token text: Obama', $output); + $this->assertContains('Token part of speech: NOUN', $output); + $this->assertContains('Token text: lives', $output); + $this->assertContains('Token part of speech: VERB', $output); + $this->assertContains('Token text: in', $output); + $this->assertContains('Token part of speech: ADP', $output); + $this->assertContains('Token text: Washington', $output); + $this->assertContains('Token part of speech: NOUN', $output); + $this->assertContains('Token text: D.C.', $output); + $this->assertContains('Token part of speech: NOUN', $output); + } + + public function testEntitySentiment() + { + $output = $this->runCommand('entity-sentiment', 'Barack Obama lives in Washington D.C.'); + $this->assertContains('Entity Name: Barack Obama', $output); + $this->assertContains('Entity Type: PERSON', $output); + $this->assertContains('Entity Salience:', $output); + $this->assertContains('Entity Magnitude:', $output); + $this->assertContains('Entity Score:', $output); + $this->assertContains('Entity Name: Washington D.C.', $output); + $this->assertContains('Entity Type: LOCATION', $output); + } + + public function testEntitySentimentFromStorageObject() + { + if (!$gcsFile = getenv('GOOGLE_LANGUAGE_GCS_FILE')) { + $this->markTestSkipped('No GCS file.'); + } + $output = $this->runCommand('entity-sentiment', $gcsFile); + $this->assertContains('Entity Name: Barack Obama', $output); + $this->assertContains('Entity Type: PERSON', $output); + $this->assertContains('Entity Salience:', $output); + $this->assertContains('Entity Magnitude:', $output); + $this->assertContains('Entity Score:', $output); + $this->assertContains('Entity Name: Washington D.C.', $output); + $this->assertContains('Entity Type: LOCATION', $output); + } + + private function runCommand($commandName, $content) + { + $application = require __DIR__ . '/../language.php'; + $command = $application->get($commandName); + $commandTester = new CommandTester($command); + + ob_start(); + $commandTester->execute([ + 'content' => $content, + ], [ + 'interactive' => false + ]); + return ob_get_clean(); + } +}