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 gguuss committed Sep 19, 2017
1 parent e83f4c1 commit 5468b07
Show file tree
Hide file tree
Showing 8 changed files with 237 additions and 6,991 deletions.
39 changes: 22 additions & 17 deletions language/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,14 @@ __Usage:__ `node analyze.v1.js --help`

```
Commands:
sentiment-text <text> Detects sentiment of a string.
sentiment-file <bucketName> <fileName> Detects sentiment in a file in Google Cloud Storage.
entities-text <text> Detects entities in a string.
entities-file <bucketName> <fileName> Detects entities in a file in Google Cloud Storage.
syntax-text <text> Detects syntax of a string.
syntax-file <bucketName> <fileName> Detects syntax in a file in Google Cloud Storage.
sentiment-text <text> Detects sentiment of a string.
sentiment-file <bucketName> <fileName> Detects sentiment in a file in Google Cloud Storage.
entities-text <text> Detects entities in a string.
entities-file <bucketName> <fileName> Detects entities in a file in Google Cloud Storage.
syntax-text <text> Detects syntax of a string.
syntax-file <bucketName> <fileName> Detects syntax in a file in Google Cloud Storage.
entity-sentiment-text <text> Detects sentiment of the entities in a string.
entity-sentiment-file <bucketName> <fileName> Detects sentiment of the entities in a file in Google Cloud Storage.
Options:
--help Show help [boolean]
Expand All @@ -58,6 +60,8 @@ Examples:
node analyze.v1.js entities-file my-bucket file.txt Detects entities in gs://my-bucket/file.txt
node analyze.v1.js syntax-text "President Obama is speaking at the White House."
node analyze.v1.js syntax-file my-bucket file.txt Detects syntax in gs://my-bucket/file.txt
node analyze.v1.js entity-sentiment-text "President Obama is speaking at the White House."
node analyze.v1.js entity-sentiment-file my-bucket file.txt Detects sentiment of entities in gs://my-bucket/file.txt
For more information, see https://cloud.google.com/natural-language/docs
```
Expand All @@ -73,14 +77,14 @@ __Usage:__ `node analyze.v1beta2.js --help`

```
Commands:
sentiment-text <text> Detects sentiment of a string.
sentiment-file <bucketName> <fileName> Detects sentiment in a file in Google Cloud Storage.
entities-text <text> Detects entities in a string.
entities-file <bucketName> <fileName> Detects entities in a file in Google Cloud Storage.
syntax-text <text> Detects syntax of a string.
syntax-file <bucketName> <fileName> Detects syntax in a file in Google Cloud Storage.
entity-sentiment-text <text> Detects sentiment of the entities in a string.
entity-sentiment-file <bucketName> <fileName> Detects sentiment of the entities in a file in Google Cloud Storage.
sentiment-text <text> Detects sentiment of a string.
sentiment-file <bucketName> <fileName> Detects sentiment in a file in Google Cloud Storage.
entities-text <text> Detects entities in a string.
entities-file <bucketName> <fileName> Detects entities in a file in Google Cloud Storage.
syntax-text <text> Detects syntax of a string.
syntax-file <bucketName> <fileName> Detects syntax in a file in Google Cloud Storage.
classify-text <text> Classifies text of a string.
classify-file <bucketName> <fileName> Classifies text in a file in Google Cloud Storage.
Options:
--help Show help [boolean]
Expand All @@ -92,9 +96,10 @@ Examples:
node analyze.v1beta2.js entities-file my-bucket file.txt Detects entities in gs://my-bucket/file.txt
node analyze.v1beta2.js syntax-text "President Obama is speaking at the White House."
node analyze.v1beta2.js syntax-file my-bucket file.txt Detects syntax in gs://my-bucket/file.txt
node analyze.v1beta2.js entity-sentiment-text "President Obama is speaking at the White House."
node analyze.v1beta2.js entity-sentiment-file my-bucket Detects sentiment of entities in gs://my-bucket/file.txt
file.txt
node analyze.v1beta2.js classify-text "Currently the API requires 20 tokens in order \
to return non-empty results. Let's use a longer piece of text for the sample in order to win."
node analyze.v1beta2.js classify-file my-bucket Detects syntax in gs://my-bucket/android_text.txt
android_text.txt
For more information, see https://cloud.google.com/natural-language/docs
```
Expand Down
93 changes: 93 additions & 0 deletions language/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 language/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 5468b07

Please sign in to comment.