-
Notifications
You must be signed in to change notification settings - Fork 1.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Update Translate samples, with some minor tweaks to Language samples. #255
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -33,7 +33,9 @@ function analyzeSentimentOfText (text) { | |
return document.detectSentiment() | ||
.then((results) => { | ||
const sentiment = results[0]; | ||
|
||
console.log(`Sentiment: ${sentiment >= 0 ? 'positive' : 'negative'}.`); | ||
|
||
return sentiment; | ||
}); | ||
} | ||
|
@@ -60,7 +62,9 @@ function analyzeSentimentInFile (bucketName, fileName) { | |
return document.detectSentiment() | ||
.then((results) => { | ||
const sentiment = results[0]; | ||
|
||
console.log(`Sentiment: ${sentiment >= 0 ? 'positive' : 'negative'}.`); | ||
|
||
return sentiment; | ||
}); | ||
} | ||
|
@@ -81,10 +85,12 @@ function analyzeEntitiesOfText (text) { | |
return document.detectEntities() | ||
.then((results) => { | ||
const entities = results[0]; | ||
|
||
console.log('Entities:'); | ||
for (let type in entities) { | ||
console.log(`${type}:`, entities[type]); | ||
} | ||
|
||
return entities; | ||
}); | ||
} | ||
|
@@ -111,10 +117,12 @@ function analyzeEntitiesInFile (bucketName, fileName) { | |
return document.detectEntities() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same is for entities. No entities printed on console. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. See #255 (comment) |
||
.then((results) => { | ||
const entities = results[0]; | ||
|
||
console.log('Entities:'); | ||
for (let type in entities) { | ||
console.log(`${type}:`, entities[type]); | ||
} | ||
|
||
return entities; | ||
}); | ||
} | ||
|
@@ -135,8 +143,10 @@ function analyzeSyntaxOfText (text) { | |
return document.detectSyntax() | ||
.then((results) => { | ||
const syntax = results[0]; | ||
|
||
console.log('Tags:'); | ||
syntax.forEach((part) => console.log(part.tag)); | ||
|
||
return syntax; | ||
}); | ||
} | ||
|
@@ -163,59 +173,62 @@ function analyzeSyntaxInFile (bucketName, fileName) { | |
return document.detectSyntax() | ||
.then((results) => { | ||
const syntax = results[0]; | ||
|
||
console.log('Tags:'); | ||
syntax.forEach((part) => console.log(part.tag)); | ||
|
||
return syntax; | ||
}); | ||
} | ||
// [END language_syntax_file] | ||
|
||
// The command-line program | ||
const cli = require(`yargs`); | ||
|
||
const program = module.exports = { | ||
analyzeSentimentOfText, | ||
analyzeSentimentInFile, | ||
analyzeEntitiesOfText, | ||
analyzeEntitiesInFile, | ||
analyzeSyntaxOfText, | ||
analyzeSyntaxInFile, | ||
main: (args) => { | ||
// Run the command-line program | ||
cli.help().strict().parse(args).argv; | ||
} | ||
}; | ||
|
||
cli | ||
require(`yargs`) | ||
.demand(1) | ||
.command(`sentimentOfText <text>`, `Detect the sentiment of a block of text.`, {}, (opts) => { | ||
program.analyzeSentimentOfText(opts.text); | ||
}) | ||
.command(`sentimentInFile <bucket> <filename>`, `Detect the sentiment of text in a GCS file.`, {}, (opts) => { | ||
program.analyzeSentimentInFile(opts.bucket, opts.filename); | ||
}) | ||
.command(`entitiesOfText <text>`, `Detect the entities of a block of text.`, {}, (opts) => { | ||
program.analyzeEntitiesOfText(opts.text); | ||
}) | ||
.command(`entitiesInFile <bucket> <filename>`, `Detect the entities of text in a GCS file.`, {}, (opts) => { | ||
program.analyzeEntitiesInFile(opts.bucket, opts.filename); | ||
}) | ||
.command(`syntaxOfText <text>`, `Detect the syntax of a block of text.`, {}, (opts) => { | ||
program.analyzeSyntaxOfText(opts.text); | ||
}) | ||
.command(`syntaxInFile <bucket> <filename>`, `Detect the syntax of text in a GCS file.`, {}, (opts) => { | ||
program.analyzeSyntaxInFile(opts.bucket, opts.filename); | ||
}) | ||
.example(`node $0 sentimentOfText "President Obama is speaking at the White House."`, ``) | ||
.example(`node $0 sentimentInFile my-bucket file.txt`, ``) | ||
.example(`node $0 entitiesOfText "President Obama is speaking at the White House."`, ``) | ||
.example(`node $0 entitiesInFile my-bucket file.txt`, ``) | ||
.example(`node $0 syntaxOfText "President Obama is speaking at the White House."`, ``) | ||
.example(`node $0 syntaxInFile my-bucket file.txt`, ``) | ||
.command( | ||
`sentiment-text <text>`, | ||
`Detects sentiment of a string.`, | ||
{}, | ||
(opts) => analyzeSentimentOfText(opts.text) | ||
) | ||
.command( | ||
`sentiment-file <bucket> <filename>`, | ||
`Detects sentiment in a file in Google Cloud Storage.`, | ||
{}, | ||
(opts) => analyzeSentimentInFile(opts.bucket, opts.filename) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. May be call it gcs_bucket here and everywhere to make it clear. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I am not seeing any output from $ node analyze sentiment-text "President Obama is speaking" for example. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. See #255 (comment) |
||
) | ||
.command( | ||
`entities-text <text>`, | ||
`Detects entities in a string.`, | ||
{}, | ||
(opts) => analyzeEntitiesOfText(opts.text) | ||
) | ||
.command( | ||
`entities-file <bucket> <filename>`, | ||
`Detects entities in a file in Google Cloud Storage.`, | ||
{}, | ||
(opts) => analyzeEntitiesInFile(opts.bucket, opts.filename) | ||
) | ||
.command( | ||
`syntax-text <text>`, | ||
`Detects syntax of a string.`, | ||
{}, | ||
(opts) => analyzeSyntaxOfText(opts.text) | ||
) | ||
.command( | ||
`syntax-file <bucket> <filename>`, | ||
`Detects syntax in a file in Google Cloud Storage.`, | ||
{}, | ||
(opts) => analyzeSyntaxInFile(opts.bucket, opts.filename) | ||
) | ||
.example(`node $0 sentiment-text "President Obama is speaking at the White House."`) | ||
.example(`node $0 sentiment-file my-bucket file.txt`, `Detects sentiment in gs://my-bucket/file.txt`) | ||
.example(`node $0 entities-text "President Obama is speaking at the White House."`) | ||
.example(`node $0 entities-file my-bucket file.txt`, `Detects entities in gs://my-bucket/file.txt`) | ||
.example(`node $0 syntax-text "President Obama is speaking at the White House."`) | ||
.example(`node $0 syntax-file my-bucket file.txt`, `Detects syntax in gs://my-bucket/file.txt`) | ||
.wrap(120) | ||
.recommendCommands() | ||
.epilogue(`For more information, see https://cloud.google.com/natural-language/docs`); | ||
|
||
if (module === require.main) { | ||
program.main(process.argv.slice(2)); | ||
} | ||
.epilogue(`For more information, see https://cloud.google.com/natural-language/docs`) | ||
.help() | ||
.strict() | ||
.argv; |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,16 +5,12 @@ | |
"license": "Apache Version 2.0", | ||
"author": "Google Inc.", | ||
"scripts": { | ||
"test": "mocha -R spec --require intelli-espower-loader ../test/_setup.js test/*.test.js", | ||
"system-test": "mocha -R spec --require intelli-espower-loader ../system-test/_setup.js system-test/*.test.js" | ||
"test": "cd ..; npm run st -- translate/system-test/*.test.js" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nit: I'm not a fan of There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This does not affect the user's current directory, it just assigns a "current working directory" to the npm script that's being executed. |
||
}, | ||
"dependencies": { | ||
"@google-cloud/translate": "^0.4.0", | ||
"@google-cloud/translate": "^0.5.0", | ||
"yargs": "^6.4.0" | ||
}, | ||
"devDependencies": { | ||
"mocha": "^3.1.2" | ||
}, | ||
"engines": { | ||
"node": ">=4.3.2" | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The program doesn't throw an error if GOOGLE_APPLICATION_CREDENTIALS is not set. It silently swallows it and return nothing. We should fix this behavior.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's not the fault of the program, it's the fault of google-cloud-node. There's nothing I can fix about that in this PR.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
True. I realized that now. But can we file an issue for those guys.