Skip to content

Commit

Permalink
Add the classifyText method to language (v1). (#22)
Browse files Browse the repository at this point in the history
  • Loading branch information
lukesneeringer authored and NimJay committed Nov 10, 2022
1 parent 7d8948e commit e8c135a
Show file tree
Hide file tree
Showing 3 changed files with 163 additions and 66 deletions.
149 changes: 117 additions & 32 deletions cloud-language/snippets/analyze.v1.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,10 @@
function analyzeSentimentOfText(text) {
// [START language_sentiment_string]
// Imports the Google Cloud client library
const LanguageServiceClient = require('@google-cloud/language')
.LanguageServiceClient;
const language = require('@google-cloud/language');

// Creates a client
const language = new LanguageServiceClient();
const client = new language.LanguageServiceClient();

/**
* TODO(developer): Uncomment the following line to run this code.
Expand All @@ -36,7 +35,7 @@ function analyzeSentimentOfText(text) {
};

// Detects the sentiment of the document
language
client
.analyzeSentiment({document: document})
.then(results => {
const sentiment = results[0].documentSentiment;
Expand All @@ -60,11 +59,10 @@ function analyzeSentimentOfText(text) {
function analyzeSentimentInFile(bucketName, fileName) {
// [START language_sentiment_file]
// Imports the Google Cloud client library
const LanguageServiceClient = require('@google-cloud/language')
.LanguageServiceClient;
const language = require('@google-cloud/language');

// Creates a client
const language = new LanguageServiceClient();
const client = new language.LanguageServiceClient();

/**
* TODO(developer): Uncomment the following lines to run this code
Expand All @@ -79,7 +77,7 @@ function analyzeSentimentInFile(bucketName, fileName) {
};

// Detects the sentiment of the document
language
client
.analyzeSentiment({document: document})
.then(results => {
const sentiment = results[0].documentSentiment;
Expand All @@ -103,11 +101,10 @@ function analyzeSentimentInFile(bucketName, fileName) {
function analyzeEntitiesOfText(text) {
// [START language_entities_string]
// Imports the Google Cloud client library
const LanguageServiceClient = require('@google-cloud/language')
.LanguageServiceClient;
const language = require('@google-cloud/language');

// Creates a client
const language = new LanguageServiceClient();
const client = new language.LanguageServiceClient();

/**
* TODO(developer): Uncomment the following line to run this code.
Expand All @@ -121,7 +118,7 @@ function analyzeEntitiesOfText(text) {
};

// Detects entities in the document
language
client
.analyzeEntities({document: document})
.then(results => {
const entities = results[0].entities;
Expand All @@ -144,11 +141,10 @@ function analyzeEntitiesOfText(text) {
function analyzeEntitiesInFile(bucketName, fileName) {
// [START language_entities_file]
// Imports the Google Cloud client library
const LanguageServiceClient = require('@google-cloud/language')
.LanguageServiceClient;
const language = require('@google-cloud/language');

// Creates a client
const language = new LanguageServiceClient();
const client = new language.LanguageServiceClient();

/**
* TODO(developer): Uncomment the following lines to run this code
Expand All @@ -163,7 +159,7 @@ function analyzeEntitiesInFile(bucketName, fileName) {
};

// Detects entities in the document
language
client
.analyzeEntities({document: document})
.then(results => {
const entities = results[0].entities;
Expand All @@ -186,11 +182,10 @@ function analyzeEntitiesInFile(bucketName, fileName) {
function analyzeSyntaxOfText(text) {
// [START language_syntax_string]
// Imports the Google Cloud client library
const LanguageServiceClient = require('@google-cloud/language')
.LanguageServiceClient;
const language = require('@google-cloud/language');

// Creates a client
const language = new LanguageServiceClient();
const client = new language.LanguageServiceClient();

/**
* TODO(developer): Uncomment the following line to run this code.
Expand All @@ -204,7 +199,7 @@ function analyzeSyntaxOfText(text) {
};

// Detects syntax in the document
language
client
.analyzeSyntax({document: document})
.then(results => {
const syntax = results[0];
Expand All @@ -224,11 +219,10 @@ function analyzeSyntaxOfText(text) {
function analyzeSyntaxInFile(bucketName, fileName) {
// [START language_syntax_file]
// Imports the Google Cloud client library
const LanguageServiceClient = require('@google-cloud/language')
.LanguageServiceClient;
const language = require('@google-cloud/language');

// Creates a client
const language = new LanguageServiceClient();
const client = new language.LanguageServiceClient();

/**
* TODO(developer): Uncomment the following lines to run this code
Expand All @@ -243,7 +237,7 @@ function analyzeSyntaxInFile(bucketName, fileName) {
};

// Detects syntax in the document
language
client
.analyzeSyntax({document: document})
.then(results => {
const syntax = results[0];
Expand All @@ -263,11 +257,10 @@ function analyzeSyntaxInFile(bucketName, fileName) {
function analyzeEntitySentimentOfText(text) {
// [START language_entity_sentiment_string]
// Imports the Google Cloud client library
const LanguageServiceClient = require('@google-cloud/language')
.LanguageServiceClient;
const language = require('@google-cloud/language');

// Creates a client
const language = new LanguageServiceClient();
const client = new language.LanguageServiceClient();

/**
* TODO(developer): Uncomment the following line to run this code.
Expand All @@ -281,7 +274,7 @@ function analyzeEntitySentimentOfText(text) {
};

// Detects sentiment of entities in the document
language
client
.analyzeEntitySentiment({document: document})
.then(results => {
const entities = results[0].entities;
Expand All @@ -303,11 +296,10 @@ function analyzeEntitySentimentOfText(text) {
function analyzeEntitySentimentInFile(bucketName, fileName) {
// [START language_entity_sentiment_file]
// Imports the Google Cloud client library
const LanguageServiceClient = require('@google-cloud/language')
.LanguageServiceClient;
const language = require('@google-cloud/language');

// Creates a client
const language = new LanguageServiceClient();
const client = new language.LanguageServiceClient();

/**
* TODO(developer): Uncomment the following lines to run this code
Expand All @@ -322,7 +314,7 @@ function analyzeEntitySentimentInFile(bucketName, fileName) {
};

// Detects sentiment of entities in the document
language
client
.analyzeEntitySentiment({document: document})
.then(results => {
const entities = results[0].entities;
Expand All @@ -341,6 +333,83 @@ function analyzeEntitySentimentInFile(bucketName, fileName) {
// [END language_entity_sentiment_file]
}

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

// Creates a client
const client = new language.LanguageServiceClient();

/**
* TODO(developer): Uncomment the following line to run this code.
*/
// const text = 'Your text to analyze, e.g. Hello, world!';

// Prepares a document, representing the provided text
const document = {
content: text,
type: 'PLAIN_TEXT',
};

// Classifies text in the document
client
.classifyText({document: document})
.then(results => {
const classification = results[0];

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

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

// Creates a client.
const client = new language.LanguageServiceClient();

/**
* TODO(developer): Uncomment the following lines to run this code
*/
// const bucketName = 'Your bucket name, e.g. my-bucket';
// const fileName = 'Your file name, e.g. my-file.txt';

// Prepares a document, representing a text file in Cloud Storage
const document = {
gcsContentUri: `gs://${bucketName}/${fileName}`,
type: 'PLAIN_TEXT',
};

// Classifies text in the document
client
.classifyText({document: document})
.then(results => {
const classification = results[0];

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

require(`yargs`)
.demand(1)
.command(
Expand Down Expand Up @@ -385,6 +454,15 @@ require(`yargs`)
{},
opts => analyzeEntitySentimentInFile(opts.bucketName, opts.fileName)
)
.command(`classify-text <text>`, `Classifies text of a string.`, {}, opts =>
classifyTextOfText(opts.text)
)
.command(
`classify-file <bucketName> <fileName>`,
`Classifies text in a file in Google Cloud Storage.`,
{},
opts => classifyTextInFile(opts.bucketName, opts.fileName)
)
.example(
`node $0 sentiment-text "President Obama is speaking at the White House."`
)
Expand Down Expand Up @@ -413,6 +491,13 @@ require(`yargs`)
`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(
Expand Down
Loading

0 comments on commit e8c135a

Please sign in to comment.