Skip to content

Commit 10c7c11

Browse files
authored
Add gcs example to language (GoogleCloudPlatform#177)
1 parent 347ea2b commit 10c7c11

20 files changed

+571
-149
lines changed

language/api/composer.json

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,21 @@
11
{
22
"require": {
3-
"google/cloud": "0.8",
3+
"google/cloud": "0.9",
44
"symfony/console": "^3.0"
55
},
66
"autoload": {
77
"psr-4": {
88
"Google\\Cloud\\Samples\\Language\\": "src/"
99
},
1010
"files": [
11+
"src/functions/analyze_all.php",
12+
"src/functions/analyze_all_from_file.php",
1113
"src/functions/analyze_entities.php",
12-
"src/functions/analyze_everything.php",
14+
"src/functions/analyze_entities_from_file.php",
1315
"src/functions/analyze_sentiment.php",
16+
"src/functions/analyze_sentiment_from_file.php",
1417
"src/functions/analyze_syntax.php",
18+
"src/functions/analyze_syntax_from_file.php",
1519
"src/functions/print_annotation.php"
1620
]
1721
}

language/api/composer.lock

Lines changed: 73 additions & 14 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

language/api/language.php

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,15 @@
22

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

5-
use Google\Cloud\Samples\Language\AnalyzeEntitiesCommand;
6-
use Google\Cloud\Samples\Language\AnalyzeEverythingCommand;
7-
use Google\Cloud\Samples\Language\AnalyzeSentimentCommand;
8-
use Google\Cloud\Samples\Language\AnalyzeSyntaxCommand;
5+
use Google\Cloud\Samples\Language\AllCommand;
6+
use Google\Cloud\Samples\Language\EntitiesCommand;
7+
use Google\Cloud\Samples\Language\SentimentCommand;
8+
use Google\Cloud\Samples\Language\SyntaxCommand;
99
use Symfony\Component\Console\Application;
1010

1111
$application = new Application();
12-
$application->add(new AnalyzeEntitiesCommand());
13-
$application->add(new AnalyzeEverythingCommand());
14-
$application->add(new AnalyzeSentimentCommand());
15-
$application->add(new AnalyzeSyntaxCommand());
12+
$application->add(new AllCommand());
13+
$application->add(new EntitiesCommand());
14+
$application->add(new SentimentCommand());
15+
$application->add(new SyntaxCommand());
1616
$application->run();

language/api/src/AnalyzeEverythingCommand.php renamed to language/api/src/AllCommand.php

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -23,36 +23,44 @@
2323
use Symfony\Component\Console\Output\OutputInterface;
2424

2525
/**
26-
* Command line utility to transcribe.
26+
* Command line utility for the Natural Language APIs.
2727
*
28-
* Usage: php speech.php transcribe
28+
* Usage: php language.php all TEXT
2929
*/
30-
class AnalyzeEverythingCommand extends Command
30+
class AllCommand extends Command
3131
{
3232
protected function configure()
3333
{
3434
$this
35-
->setName('everything')
36-
->setDescription('Analyze some natural language text.')
35+
->setName('all')
36+
->setDescription('Analyze syntax, sentiment and entities in text.')
3737
->setHelp(<<<EOF
3838
The <info>%command.name%</info> command analyzes text using the Google Cloud Natural Language API.
3939
4040
<info>php %command.full_name% Text to analyze.</info>
4141
42+
<info>php %command.full_name% gs://my_bucket/file_with_text.txt</info>
43+
4244
EOF
4345
)
4446
->addArgument(
45-
'text',
47+
'content',
4648
InputArgument::IS_ARRAY | InputArgument::REQUIRED,
47-
'Text to analyze'
49+
'Text or path to Cloud Storage file'
4850
)
4951
;
5052
}
5153

5254
protected function execute(InputInterface $input, OutputInterface $output)
5355
{
54-
$text = implode(" ", $input->getArgument('text'));
55-
$result = analyze_everything($text);
56+
$content = implode(' ', (array) $input->getArgument('content'));
57+
// Regex to match a Cloud Storage path as the first argument
58+
// e.g "gs://my-bucket/file_with_text.txt"
59+
if (preg_match('/^gs:\/\/([a-z0-9\._\-]+)\/(\S+)$/', $content, $matches)) {
60+
$result = analyze_all_from_file($matches[1], $matches[2]);
61+
} else {
62+
$result = analyze_all($content);
63+
}
5664
print_annotation($result);
5765
}
5866
}

language/api/src/AnalyzeEntitiesCommand.php renamed to language/api/src/EntitiesCommand.php

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -23,36 +23,44 @@
2323
use Symfony\Component\Console\Output\OutputInterface;
2424

2525
/**
26-
* Command line utility to transcribe.
26+
* Command line utility for the Natural Language APIs.
2727
*
28-
* Usage: php speech.php transcribe
28+
* Usage: php language.php entities TEXT
2929
*/
30-
class AnalyzeEntitiesCommand extends Command
30+
class EntitiesCommand extends Command
3131
{
3232
protected function configure()
3333
{
3434
$this
3535
->setName('entities')
36-
->setDescription('Analyze some natural language text.')
36+
->setDescription('Analyze entities in text.')
3737
->setHelp(<<<EOF
3838
The <info>%command.name%</info> command analyzes text using the Google Cloud Natural Language API.
3939
4040
<info>php %command.full_name% Text to analyze.</info>
4141
42+
<info>php %command.full_name% gs://my_bucket/file_with_text.txt</info>
43+
4244
EOF
4345
)
4446
->addArgument(
45-
'text',
47+
'content',
4648
InputArgument::IS_ARRAY | InputArgument::REQUIRED,
47-
'Text to analyze'
49+
'Text or path to Cloud Storage file'
4850
)
4951
;
5052
}
5153

5254
protected function execute(InputInterface $input, OutputInterface $output)
5355
{
54-
$text = implode(" ", $input->getArgument('text'));
55-
$result = analyze_entities($text);
56+
$content = implode(' ', (array) $input->getArgument('content'));
57+
// Regex to match a Cloud Storage path as the first argument
58+
// e.g "gs://my-bucket/file_with_text.txt"
59+
if (preg_match('/^gs:\/\/([a-z0-9\._\-]+)\/(\S+)$/', $content, $matches)) {
60+
$result = analyze_entities_from_file($matches[1], $matches[2]);
61+
} else {
62+
$result = analyze_entities($content);
63+
}
5664
print_annotation($result);
5765
}
5866
}

language/api/src/AnalyzeSentimentCommand.php renamed to language/api/src/SentimentCommand.php

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -23,36 +23,45 @@
2323
use Symfony\Component\Console\Output\OutputInterface;
2424

2525
/**
26-
* Command line utility to transcribe.
26+
* Command line utility for the Natural Language APIs.
2727
*
28-
* Usage: php speech.php transcribe
28+
* Usage: php language.php sentiment TEXT
2929
*/
30-
class AnalyzeSentimentCommand extends Command
30+
class SentimentCommand extends Command
3131
{
3232
protected function configure()
3333
{
3434
$this
3535
->setName('sentiment')
36-
->setDescription('Analyze some natural language text.')
36+
->setDescription('Analyze sentiment in text.')
3737
->setHelp(<<<EOF
3838
The <info>%command.name%</info> command analyzes text using the Google Cloud Natural Language API.
3939
4040
<info>php %command.full_name% Text to analyze.</info>
4141
42+
<info>php %command.full_name% gs://my_bucket/file_with_text.txt</info>
43+
4244
EOF
4345
)
4446
->addArgument(
45-
'text',
47+
'content',
4648
InputArgument::IS_ARRAY | InputArgument::REQUIRED,
47-
'Text to analyze'
49+
'Text or path to Cloud Storage file'
4850
)
4951
;
5052
}
5153

5254
protected function execute(InputInterface $input, OutputInterface $output)
5355
{
54-
$text = implode(" ", $input->getArgument('text'));
55-
$result = analyze_sentiment($text);
56+
$content = implode(' ', (array) $input->getArgument('content'));
57+
// Regex to match a Cloud Storage path as the first argument
58+
// e.g "gs://my-bucket/file_with_text.txt"
59+
if (preg_match('/^gs:\/\/([a-z0-9\._\-]+)\/(\S+)$/', $content, $matches)) {
60+
$result = analyze_sentiment_from_file($matches[1], $matches[2]);
61+
} else {
62+
$result = analyze_sentiment($content);
63+
}
64+
5665
print_annotation($result);
5766
}
5867
}

0 commit comments

Comments
 (0)