Skip to content

Commit 55e5daf

Browse files
author
Ace Nassri
committed
Add analyzeEntitySentiment sample
1 parent 30b4da8 commit 55e5daf

File tree

2 files changed

+113
-0
lines changed

2 files changed

+113
-0
lines changed

language/analyze.js

+95
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,87 @@ function analyzeSyntaxInFile (bucketName, fileName) {
215215
// [END language_syntax_file]
216216
}
217217

218+
function analyzeEntitySentimentOfText (text) {
219+
// [START language_entity_sentiment_string]
220+
// Imports the Google Cloud client library
221+
const Language = require('@google-cloud/language').v1beta2();
222+
223+
// Instantiates a client
224+
const language = Language.languageServiceClient();
225+
226+
// The text to analyze, e.g. "Hello, world!"
227+
// const text = 'Hello, world!';
228+
229+
// Configure a request containing a string
230+
const request = {
231+
document: {
232+
type: 'PLAIN_TEXT',
233+
content: text
234+
}
235+
};
236+
237+
// Detects sentiment of entities in the document
238+
language.analyzeEntitySentiment(request)
239+
.then((results) => {
240+
const entities = results[0].entities;
241+
242+
console.log(`Entities and sentiments:`)
243+
entities.forEach((entity) => {
244+
console.log(` Name: ${entity.name}`);
245+
console.log(` Type: ${entity.type}`);
246+
console.log(` Score: ${entity.sentiment.score}`);
247+
console.log(` Magnitude: ${entity.sentiment.magnitude}`);
248+
});
249+
})
250+
.catch((err) => {
251+
console.error('ERROR:', err);
252+
});
253+
// [END language_entity_sentiment_string]
254+
}
255+
256+
function analyzeEntitySentimentInFile (bucketName, fileName) {
257+
// [START language_entity_sentiment_file]
258+
// Imports the Google Cloud client libraries
259+
const Language = require('@google-cloud/language').v1beta2();
260+
const Storage = require('@google-cloud/storage');
261+
262+
// Instantiates the clients
263+
const language = Language.languageServiceClient();
264+
const storage = Storage();
265+
266+
// The name of the bucket where the file resides, e.g. "my-bucket"
267+
// const bucketName = 'my-bucket';
268+
269+
// The name of the file to analyze, e.g. "file.txt"
270+
// const fileName = 'file.txt';
271+
272+
// Configure a request containing a string
273+
const request = {
274+
document: {
275+
type: 'PLAIN_TEXT',
276+
gcsContentUri: `gs://${bucketName}/${fileName}`
277+
}
278+
};
279+
280+
// Detects sentiment of entities in the document
281+
language.analyzeEntitySentiment(request)
282+
.then((results) => {
283+
const entities = results[0].entities;
284+
285+
console.log(`Entities and sentiments:`)
286+
entities.forEach((entity) => {
287+
console.log(` Name: ${entity.name}`);
288+
console.log(` Type: ${entity.type}`);
289+
console.log(` Score: ${entity.sentiment.score}`);
290+
console.log(` Magnitude: ${entity.sentiment.magnitude}`);
291+
});
292+
})
293+
.catch((err) => {
294+
console.error('ERROR:', err);
295+
});
296+
// [END language_entity_sentiment_file]
297+
}
298+
218299
require(`yargs`)
219300
.demand(1)
220301
.command(
@@ -253,12 +334,26 @@ require(`yargs`)
253334
{},
254335
(opts) => analyzeSyntaxInFile(opts.bucketName, opts.fileName)
255336
)
337+
.command(
338+
`entity-sentiment-text <text>`,
339+
`Detects sentiment of the entities in a string.`,
340+
{},
341+
(opts) => analyzeEntitySentimentOfText(opts.text)
342+
)
343+
.command(
344+
`entity-sentiment-file <bucketName> <fileName>`,
345+
`Detects sentiment of the entities in a file in Google Cloud Storage.`,
346+
{},
347+
(opts) => analyzeEntitySentimentInFile(opts.bucketName, opts.fileName)
348+
)
256349
.example(`node $0 sentiment-text "President Obama is speaking at the White House."`)
257350
.example(`node $0 sentiment-file my-bucket file.txt`, `Detects sentiment in gs://my-bucket/file.txt`)
258351
.example(`node $0 entities-text "President Obama is speaking at the White House."`)
259352
.example(`node $0 entities-file my-bucket file.txt`, `Detects entities in gs://my-bucket/file.txt`)
260353
.example(`node $0 syntax-text "President Obama is speaking at the White House."`)
261354
.example(`node $0 syntax-file my-bucket file.txt`, `Detects syntax in gs://my-bucket/file.txt`)
355+
.example(`node $0 entity-sentiment-text "President Obama is speaking at the White House."`)
356+
.example(`node $0 entity-sentiment-file my-bucket file.txt`, `Detects sentiment of entities in gs://my-bucket/file.txt`)
262357
.wrap(120)
263358
.recommendCommands()
264359
.epilogue(`For more information, see https://cloud.google.com/natural-language/docs`)

language/system-test/analyze.test.js

+18
Original file line numberDiff line numberDiff line change
@@ -86,3 +86,21 @@ test('should analyze syntax in a file', async (t) => {
8686
t.true(output.includes(`President`));
8787
t.true(output.includes(`Obama`));
8888
});
89+
90+
test(`should analyze entity sentiment in text`, async (t) => {
91+
const output = await runAsync(`${cmd} entity-sentiment-text "${text}"`, cwd);
92+
t.true(output.includes(`Entities and sentiments:`));
93+
t.true(output.includes(`Obama`));
94+
t.true(output.includes(`PERSON`));
95+
t.true(output.includes(`Score: 0`));
96+
t.true(output.includes(`Magnitude: 0`));
97+
});
98+
99+
test('should analyze entity sentiment in a file', async (t) => {
100+
const output = await runAsync(`${cmd} entity-sentiment-file ${bucketName} ${fileName}`, cwd);
101+
t.true(output.includes(`Entities and sentiments:`));
102+
t.true(output.includes(`Obama`));
103+
t.true(output.includes(`PERSON`));
104+
t.true(output.includes(`Score: 0`));
105+
t.true(output.includes(`Magnitude: 0`));
106+
});

0 commit comments

Comments
 (0)