Skip to content

Commit

Permalink
[NL] v1 and v1beta2 updates (#470)
Browse files Browse the repository at this point in the history
* Move entity-level sentiment samples to v1.

* Add text classification.

* Adds longer text to README.
  • Loading branch information
jmdobry authored and NimJay committed Nov 10, 2022
1 parent 32f619a commit 6df4e98
Show file tree
Hide file tree
Showing 7 changed files with 215 additions and 6,974 deletions.
93 changes: 93 additions & 0 deletions cloud-language/snippets/analyze.v1.js
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,85 @@ 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');

// Instantiates the clients
const language = Language();

// 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 library
const Language = require('@google-cloud/language');

// Instantiates the clients
const language = Language();

// 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) => {
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`) // eslint-disable-line
.demand(1)
.command(
Expand Down Expand Up @@ -279,12 +358,26 @@ require(`yargs`) // eslint-disable-line
{},
(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`)
Expand Down
110 changes: 59 additions & 51 deletions cloud-language/snippets/analyze.v1beta2.js
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,10 @@ function analyzeSentimentInFile (bucketName, fileName) {
function analyzeEntitiesOfText (text) {
// [START language_entities_string]
// Imports the Google Cloud client library
const language = require('@google-cloud/language').v1beta2();
const Language = require('@google-cloud/language');

// Instantiates the clients
const language = Language({ apiVersion: 'v1beta2' });

// The text to analyze, e.g. "Hello, world!"
// const text = 'Hello, world!';
Expand Down Expand Up @@ -131,7 +134,10 @@ function analyzeEntitiesOfText (text) {
function analyzeEntitiesInFile (bucketName, fileName) {
// [START language_entities_file]
// Imports the Google Cloud client libraries
const language = require('@google-cloud/language').v1beta2();
const Language = require('@google-cloud/language');

// Instantiates the clients
const language = Language({ apiVersion: 'v1beta2' });

// The name of the bucket where the file resides, e.g. "my-bucket"
// const bucketName = 'my-bucket';
Expand Down Expand Up @@ -168,7 +174,10 @@ function analyzeEntitiesInFile (bucketName, fileName) {
function analyzeSyntaxOfText (text) {
// [START language_syntax_string]
// Imports the Google Cloud client library
const language = require('@google-cloud/language').v1beta2();
const Language = require('@google-cloud/language');

// Instantiates the clients
const language = Language({ apiVersion: 'v1beta2' });

// The text to analyze, e.g. "Hello, world!"
// const text = 'Hello, world!';
Expand Down Expand Up @@ -199,7 +208,10 @@ function analyzeSyntaxOfText (text) {
function analyzeSyntaxInFile (bucketName, fileName) {
// [START language_syntax_file]
// Imports the Google Cloud client libraries
const language = require('@google-cloud/language').v1beta2();
const Language = require('@google-cloud/language');

// Instantiates the clients
const language = Language({ apiVersion: 'v1beta2' });

// The name of the bucket where the file resides, e.g. "my-bucket"
// const bucketName = 'my-bucket';
Expand Down Expand Up @@ -230,77 +242,73 @@ function analyzeSyntaxInFile (bucketName, fileName) {
// [END language_syntax_file]
}

function analyzeEntitySentimentOfText (text) {
// [START language_entity_sentiment_string]
function classifyTextOfText (text) {
// [START language_classify_string]
// Imports the Google Cloud client library
const language = require('@google-cloud/language').v1beta2();
const Language = require('@google-cloud/language');

// Creates a client
const language = Language.v1beta2();

// 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
}
// Instantiates a Document, representing the provided text
const document = {
'content': text,
type: 'PLAIN_TEXT'
};

// Detects sentiment of entities in the document
language.analyzeEntitySentiment(request)
// Classifies text in the document
language.classifyText({ document: document })
.then((results) => {
const entities = results[0].entities;
const classification = results[0];

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}`);
console.log('Categories:');
classification.categories.forEach((category) => {
console.log(`Name: ${category.name}, Confidence: ${category.confidence}`);
});
})
.catch((err) => {
console.error('ERROR:', err);
});
// [END language_entity_sentiment_string]
// [END language_classify_string]
}

function analyzeEntitySentimentInFile (bucketName, fileName) {
// [START language_entity_sentiment_file]
// Imports the Google Cloud client library
const language = require('@google-cloud/language').v1beta2();
function classifyTextInFile (bucketName, fileName) {
// [START language_classify_file]
// Imports the Google Cloud client libraries
const Language = require('@google-cloud/language');

// Creates a client
const language = Language.v1beta2();

// 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}`
}
// Instantiates a Document, representing a text file in Cloud Storage
const document = {
gcsContentUri: `gs://${bucketName}/${fileName}`,
type: 'PLAIN_TEXT'
};

// Detects sentiment of entities in the document
language.analyzeEntitySentiment(request)
// Classifies text in the document
language.classifyText({ document: document })
.then((results) => {
const entities = results[0].entities;
const classification = results[0];

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}`);
console.log('Categories:');
classification.categories.forEach((category) => {
console.log(`Name: ${category.name}, Confidence: ${category.confidence}`);
});
})
.catch((err) => {
console.error('ERROR:', err);
});
// [END language_entity_sentiment_file]
// [END language_classify_file]
}

require(`yargs`) // eslint-disable-line
Expand Down Expand Up @@ -342,25 +350,25 @@ require(`yargs`) // eslint-disable-line
(opts) => analyzeSyntaxInFile(opts.bucketName, opts.fileName)
)
.command(
`entity-sentiment-text <text>`,
`Detects sentiment of the entities in a string.`,
`classify-text <text>`,
`Classifies text of a string.`,
{},
(opts) => analyzeEntitySentimentOfText(opts.text)
(opts) => classifyTextOfText(opts.text)
)
.command(
`entity-sentiment-file <bucketName> <fileName>`,
`Detects sentiment of the entities in a file in Google Cloud Storage.`,
`classify-file <bucketName> <fileName>`,
`Classifies text in a file in Google Cloud Storage.`,
{},
(opts) => analyzeEntitySentimentInFile(opts.bucketName, opts.fileName)
(opts) => classifyTextInFile(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`)
.example(`node $0 classify-text "Android is a mobile operating system developed by Google."`)
.example(`node $0 classify-file my-bucket android_text.txt`, `Detects syntax in gs://my-bucket/android_text.txt`)
.wrap(120)
.recommendCommands()
.epilogue(`For more information, see https://cloud.google.com/natural-language/docs`)
Expand Down
Loading

0 comments on commit 6df4e98

Please sign in to comment.