-
Notifications
You must be signed in to change notification settings - Fork 2k
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
Add sample for Natural Language 1.1 launch #352
Merged
Changes from 3 commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -32,7 +32,7 @@ function analyzeSentimentOfText (text) { | |
// Detects the sentiment of the document | ||
document.detectSentiment() | ||
.then((results) => { | ||
const sentiment = results[0]; | ||
const sentiment = results[1].documentSentiment; | ||
console.log(`Score: ${sentiment.score}`); | ||
console.log(`Magnitude: ${sentiment.magnitude}`); | ||
}) | ||
|
@@ -67,7 +67,7 @@ function analyzeSentimentInFile (bucketName, fileName) { | |
// Detects the sentiment of the document | ||
document.detectSentiment() | ||
.then((results) => { | ||
const sentiment = results[0]; | ||
const sentiment = results[1].documentSentiment; | ||
console.log(`Score: ${sentiment.score}`); | ||
console.log(`Magnitude: ${sentiment.magnitude}`); | ||
}) | ||
|
@@ -94,7 +94,7 @@ function analyzeEntitiesOfText (text) { | |
// Detects entities in the document | ||
document.detectEntities() | ||
.then((results) => { | ||
const entities = results[0]; | ||
const entities = results[1].entities; | ||
|
||
console.log('Entities:'); | ||
entities.forEach((entity) => { | ||
|
@@ -215,6 +215,87 @@ function analyzeSyntaxInFile (bucketName, fileName) { | |
// [END language_syntax_file] | ||
} | ||
|
||
function analyzeEntitySentimentOfText (text) { | ||
// [START language_entity_sentiment_string] | ||
// Imports the Google Cloud client library | ||
const Language = require('@google-cloud/language').v1beta2(); | ||
|
||
// Instantiates a client | ||
const language = Language.languageServiceClient(); | ||
|
||
// The text to analyze, e.g. "Hello, world!" | ||
// const text = 'Hello, world!'; | ||
|
||
// Configure a request containing a string | ||
const request = { | ||
document: { | ||
type: 'PLAIN_TEXT', | ||
content: text | ||
} | ||
}; | ||
|
||
// Detects sentiment of entities in the document | ||
language.analyzeEntitySentiment(request) | ||
.then((results) => { | ||
const entities = results[0].entities; | ||
|
||
console.log(`Entities and sentiments:`) | ||
entities.forEach((entity) => { | ||
console.log(` Name: ${entity.name}`); | ||
console.log(` Type: ${entity.type}`); | ||
console.log(` Score: ${entity.sentiment.score}`); | ||
console.log(` Magnitude: ${entity.sentiment.magnitude}`); | ||
}); | ||
}) | ||
.catch((err) => { | ||
console.error('ERROR:', err); | ||
}); | ||
// [END language_entity_sentiment_string] | ||
} | ||
|
||
function analyzeEntitySentimentInFile (bucketName, fileName) { | ||
// [START language_entity_sentiment_file] | ||
// Imports the Google Cloud client libraries | ||
const Language = require('@google-cloud/language').v1beta2(); | ||
const Storage = require('@google-cloud/storage'); | ||
|
||
// Instantiates the clients | ||
const language = Language.languageServiceClient(); | ||
const storage = Storage(); | ||
|
||
// The name of the bucket where the file resides, e.g. "my-bucket" | ||
// const bucketName = 'my-bucket'; | ||
|
||
// The name of the file to analyze, e.g. "file.txt" | ||
// const fileName = 'file.txt'; | ||
|
||
// Configure a request containing a string | ||
const request = { | ||
document: { | ||
type: 'PLAIN_TEXT', | ||
gcsContentUri: `gs://${bucketName}/${fileName}` | ||
} | ||
}; | ||
|
||
// Detects sentiment of entities in the document | ||
language.analyzeEntitySentiment(request) | ||
.then((results) => { | ||
const entities = results[0].entities; | ||
|
||
console.log(`Entities and sentiments:`) | ||
entities.forEach((entity) => { | ||
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. Don't forget the mentions in entity sentiment: from Python:
|
||
console.log(` Name: ${entity.name}`); | ||
console.log(` Type: ${entity.type}`); | ||
console.log(` Score: ${entity.sentiment.score}`); | ||
console.log(` Magnitude: ${entity.sentiment.magnitude}`); | ||
}); | ||
}) | ||
.catch((err) => { | ||
console.error('ERROR:', err); | ||
}); | ||
// [END language_entity_sentiment_file] | ||
} | ||
|
||
require(`yargs`) | ||
.demand(1) | ||
.command( | ||
|
@@ -253,12 +334,26 @@ require(`yargs`) | |
{}, | ||
(opts) => analyzeSyntaxInFile(opts.bucketName, opts.fileName) | ||
) | ||
.command( | ||
`entity-sentiment-text <text>`, | ||
`Detects sentiment of the entities in a string.`, | ||
{}, | ||
(opts) => analyzeEntitySentimentOfText(opts.text) | ||
) | ||
.command( | ||
`entity-sentiment-file <bucketName> <fileName>`, | ||
`Detects sentiment of the entities in a file in Google Cloud Storage.`, | ||
{}, | ||
(opts) => analyzeEntitySentimentInFile(opts.bucketName, 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`) | ||
.example(`node $0 entity-sentiment-text "President Obama is speaking at the White House."`) | ||
.example(`node $0 entity-sentiment-file my-bucket file.txt`, `Detects sentiment of entities in gs://my-bucket/file.txt`) | ||
.wrap(120) | ||
.recommendCommands() | ||
.epilogue(`For more information, see https://cloud.google.com/natural-language/docs`) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Is it just that the first result is missing sentiment? I'm seeing that not all the entities returned have sentiment.