Skip to content

Conversation

@SurferJeffAtGoogle
Copy link
Contributor

No description provided.

Copy link
Contributor

@bshaffer bshaffer left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is all awesome. A handful of small changes is all.

*/
function analyze_everything($text, $options = [])
{
$builder = new ServiceBuilder();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We decided to stop using the ServiceBuilder in examples, and instead just instantiate the client directly, i.e.

$language = new NaturalLanguageClient();

* analyze_everything($projectId, 'Do you know the way to San Jose?');
* ```.
*
* @param string $projectId The Google project ID.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove unused $projectId param from docblock/example

{
$text = implode(" ", $input->getArgument('text'));
$result = analyze_entities($text);
print_r($result->info());
Copy link
Contributor

@bshaffer bshaffer Sep 19, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In the other samples, we have the output take place inside the included functions. This is so that the copy/paste sample outputs something meaningful by default.

print_r is pretty generic, could we output something more structured? Something like:

printf('Name: %s' . PHP_EOL, $annotation['name']);
printf('Tag: %s' . PHP_EOL, $annotation['tag']);
printf('Sentiment: polarity of %s with magnitude of %s' . PHP_EOL,
    $annotation['documentSentiment']['polarity'],
    $annotation['documentSentiment']['magnitude']);

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What if we did something like this for syntax:

print('TOKENS' . PHP_EOL);
$info = $result->info();
foreach ($info['tokens'] as $token) {
    printf('%s - %s' . PHP_EOL, $token['text']['content'], $token['text']['partOfSpeech']);
}

And something similar for entities?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I feel a tug of war between good coding discipline and a good demonstration of the code. Coding discipline dictates that a function should return values, not print values. Command handlers should print values. In other samples, I think the functions were quite illustrative. But in this sample, the functions are only 3 lines with no branches. What do you think of just moving the code into the commands and deleting the functions? The unit tests that test the commands already 100% cover the functions.

Regarding the print_r, will PHP programmers be familiar with the print_r output? As a reader, I definitely want to see every annotation that language API can give me, and print_r shows me every detail, and exactly where to find it. Also, writing custom print statements means more code for us to maintain, and that print code would be unlikely to be copied and pasted into user code.

@bshaffer
Copy link
Contributor

See the output from php-cs-fixer to fix the CS. You can install php-cs-fixer and run it like this:

cd php-docs-samples/language/api
php-cs-fixer fix --config-file=../../.php_cs

Copy link
Contributor

@bshaffer bshaffer left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Linter fixes, but otherwise LGTM!

}

if (isset($info['documentSentiment']))
{
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will cause the linter to fail. They need to be cuddly!


use Google\Cloud\NaturalLanguage\Annotation;

function print_annotation(Annotation $annotation) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Curly brace here needs to be on its own line.

print "entities:\n";
foreach ($info['entities'] as $entity) {
print " " . $entity['name'];
if (isset($entity['metadata']['wikipedia_url']))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Always use curly braces even for one-liners.

function print_annotation(Annotation $annotation) {
$info = $annotation->info();
if (isset($info['language'])) {
print "language: " . $info['language'] . "\n";
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We've been using single quotes for all strings, and PHP_EOL instead of \n

@SurferJeffAtGoogle SurferJeffAtGoogle merged commit 23cdf02 into master Sep 20, 2016
@SurferJeffAtGoogle SurferJeffAtGoogle deleted the cherrypick-language branch September 20, 2016 19:23
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants