Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions language/api/composer.json
Original file line number Diff line number Diff line change
@@ -1,17 +1,21 @@
{
"require": {
"google/cloud": "0.8",
"google/cloud": "0.9",
"symfony/console": "^3.0"
},
"autoload": {
"psr-4": {
"Google\\Cloud\\Samples\\Language\\": "src/"
},
"files": [
"src/functions/analyze_all.php",
"src/functions/analyze_all_from_file.php",
"src/functions/analyze_entities.php",
"src/functions/analyze_everything.php",
"src/functions/analyze_entities_from_file.php",
"src/functions/analyze_sentiment.php",
"src/functions/analyze_sentiment_from_file.php",
"src/functions/analyze_syntax.php",
"src/functions/analyze_syntax_from_file.php",
"src/functions/print_annotation.php"
]
}
Expand Down
87 changes: 73 additions & 14 deletions language/api/composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 8 additions & 8 deletions language/api/language.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@

require __DIR__ . '/vendor/autoload.php';

use Google\Cloud\Samples\Language\AnalyzeEntitiesCommand;
use Google\Cloud\Samples\Language\AnalyzeEverythingCommand;
use Google\Cloud\Samples\Language\AnalyzeSentimentCommand;
use Google\Cloud\Samples\Language\AnalyzeSyntaxCommand;
use Google\Cloud\Samples\Language\AllCommand;
use Google\Cloud\Samples\Language\EntitiesCommand;
use Google\Cloud\Samples\Language\SentimentCommand;
use Google\Cloud\Samples\Language\SyntaxCommand;
use Symfony\Component\Console\Application;

$application = new Application();
$application->add(new AnalyzeEntitiesCommand());
$application->add(new AnalyzeEverythingCommand());
$application->add(new AnalyzeSentimentCommand());
$application->add(new AnalyzeSyntaxCommand());
$application->add(new AllCommand());
$application->add(new EntitiesCommand());
$application->add(new SentimentCommand());
$application->add(new SyntaxCommand());
$application->run();
Original file line number Diff line number Diff line change
Expand Up @@ -23,36 +23,44 @@
use Symfony\Component\Console\Output\OutputInterface;

/**
* Command line utility to transcribe.
* Command line utility for the Natural Language APIs.
*
* Usage: php speech.php transcribe
* Usage: php language.php all TEXT
*/
class AnalyzeEverythingCommand extends Command
class AllCommand extends Command
{
protected function configure()
{
$this
->setName('everything')
->setDescription('Analyze some natural language text.')
->setName('all')
->setDescription('Analyze syntax, sentiment and entities in text.')
->setHelp(<<<EOF
The <info>%command.name%</info> command analyzes text using the Google Cloud Natural Language API.

<info>php %command.full_name% Text to analyze.</info>

<info>php %command.full_name% gs://my_bucket/file_with_text.txt</info>

EOF
)
->addArgument(
'text',
'content',
InputArgument::IS_ARRAY | InputArgument::REQUIRED,
'Text to analyze'
'Text or path to Cloud Storage file'
)
;
}

protected function execute(InputInterface $input, OutputInterface $output)
{
$text = implode(" ", $input->getArgument('text'));
$result = analyze_everything($text);
$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)) {
$result = analyze_all_from_file($matches[1], $matches[2]);
} else {
$result = analyze_all($content);
}
print_annotation($result);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,36 +23,44 @@
use Symfony\Component\Console\Output\OutputInterface;

/**
* Command line utility to transcribe.
* Command line utility for the Natural Language APIs.
Copy link
Contributor

Choose a reason for hiding this comment

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

Small nit: Is this really a utility? I'd call it command line sample or somthing

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It is both a utility and a sample. I do not think we need to change this.

*
* Usage: php speech.php transcribe
* Usage: php language.php entities TEXT
*/
class AnalyzeEntitiesCommand extends Command
class EntitiesCommand extends Command
{
protected function configure()
{
$this
->setName('entities')
->setDescription('Analyze some natural language text.')
->setDescription('Analyze entities in text.')
->setHelp(<<<EOF
The <info>%command.name%</info> command analyzes text using the Google Cloud Natural Language API.

<info>php %command.full_name% Text to analyze.</info>

<info>php %command.full_name% gs://my_bucket/file_with_text.txt</info>

EOF
)
->addArgument(
'text',
'content',
InputArgument::IS_ARRAY | InputArgument::REQUIRED,
'Text to analyze'
'Text or path to Cloud Storage file'
Copy link
Contributor

Choose a reason for hiding this comment

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

I feel little bit weird to use the same argument for the text and the gs path.
I think it's simpler if you make the text argument OPTIONAL (actually it's conditionally required), and add an option --gs-path or something?

If the text contains the sub-text "gs://your-bucket/your-object", does it match the regex?
For example, can you analyze the following text?

To analyze the text stored in cloud storage, specify the gs path like gs://your-bucket/your-object as the argument.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good catch on the regex! This is now fixed.

As for the --gcs-path, I experimented with this but I found it to be cumbersome. I think the current approach works better.

)
;
}

protected function execute(InputInterface $input, OutputInterface $output)
{
$text = implode(" ", $input->getArgument('text'));
$result = analyze_entities($text);
$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)) {
$result = analyze_entities_from_file($matches[1], $matches[2]);
} else {
$result = analyze_entities($content);
}
print_annotation($result);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,36 +23,45 @@
use Symfony\Component\Console\Output\OutputInterface;

/**
* Command line utility to transcribe.
* Command line utility for the Natural Language APIs.
*
* Usage: php speech.php transcribe
* Usage: php language.php sentiment TEXT
*/
class AnalyzeSentimentCommand extends Command
class SentimentCommand extends Command
{
protected function configure()
{
$this
->setName('sentiment')
->setDescription('Analyze some natural language text.')
->setDescription('Analyze sentiment in text.')
->setHelp(<<<EOF
The <info>%command.name%</info> command analyzes text using the Google Cloud Natural Language API.

<info>php %command.full_name% Text to analyze.</info>

<info>php %command.full_name% gs://my_bucket/file_with_text.txt</info>

EOF
)
->addArgument(
'text',
'content',
InputArgument::IS_ARRAY | InputArgument::REQUIRED,
'Text to analyze'
'Text or path to Cloud Storage file'
)
;
}

protected function execute(InputInterface $input, OutputInterface $output)
{
$text = implode(" ", $input->getArgument('text'));
$result = analyze_sentiment($text);
$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)) {
$result = analyze_sentiment_from_file($matches[1], $matches[2]);
} else {
$result = analyze_sentiment($content);
}

print_annotation($result);
}
}
Loading