Skip to content
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

Upgrades language samples to semi-gapic client #435

Merged
merged 5 commits into from
Jul 28, 2017
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
76 changes: 39 additions & 37 deletions language/analyze.v1.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,18 +26,20 @@ function analyzeSentimentOfText (text) {
// The text to analyze, e.g. "Hello, world!"
// const text = 'Hello, world!';

// Instantiates a Document, representing the provided text
const document = language.document({ content: text });
const document = {
'content': text,
type: 'PLAIN_TEXT'
};

// Detects the sentiment of the document
document.detectSentiment()
language.analyzeSentiment({'document': document})
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Change to:

language.analyzeSentiment({ document: document })

.then((results) => {
const sentiment = results[1].documentSentiment;
const sentiment = results[0].documentSentiment;
console.log(`Document sentiment:`);
console.log(` Score: ${sentiment.score}`);
console.log(` Magnitude: ${sentiment.magnitude}`);

const sentences = results[1].sentences;
const sentences = results[0].sentences;
sentences.forEach((sentence) => {
console.log(`Sentence: ${sentence.text.content}`);
console.log(` Score: ${sentence.sentiment.score}`);
Expand All @@ -54,11 +56,9 @@ function analyzeSentimentInFile (bucketName, fileName) {
// [START language_sentiment_file]
// Imports the Google Cloud client libraries
const Language = require('@google-cloud/language');
const Storage = require('@google-cloud/storage');

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

// The name of the bucket where the file resides, e.g. "my-bucket"
// const bucketName = 'my-bucket';
Expand All @@ -67,20 +67,20 @@ function analyzeSentimentInFile (bucketName, fileName) {
// const fileName = 'file.txt';

// Instantiates a Document, representing a text file in Cloud Storage
const document = language.document({
// The Google Cloud Storage file
content: storage.bucket(bucketName).file(fileName)
});
const document = {
gcsContentUri: `gs://${bucketName}/${fileName}`,
type: 'PLAIN_TEXT'
};

// Detects the sentiment of the document
document.detectSentiment()
language.analyzeSentiment({'document': document})
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Change to:

language.analyzeSentiment({ document: document })

.then((results) => {
const sentiment = results[1].documentSentiment;
const sentiment = results[0].documentSentiment;
console.log(`Document sentiment:`);
console.log(` Score: ${sentiment.score}`);
console.log(` Magnitude: ${sentiment.magnitude}`);

const sentences = results[1].sentences;
const sentences = results[0].sentences;
sentences.forEach((sentence) => {
console.log(`Sentence: ${sentence.text.content}`);
console.log(` Score: ${sentence.sentiment.score}`);
Expand All @@ -105,12 +105,15 @@ function analyzeEntitiesOfText (text) {
// const text = 'Hello, world!';

// Instantiates a Document, representing the provided text
const document = language.document({ content: text });
const document = {
'content': text,
type: 'PLAIN_TEXT'
};

// Detects entities in the document
document.detectEntities()
language.analyzeEntities({'document': document})
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Change to:

language.analyzeEntities({ document: document })

.then((results) => {
const entities = results[1].entities;
const entities = results[0].entities;

console.log('Entities:');
entities.forEach((entity) => {
Expand All @@ -131,11 +134,9 @@ function analyzeEntitiesInFile (bucketName, fileName) {
// [START language_entities_file]
// Imports the Google Cloud client libraries
const Language = require('@google-cloud/language');
const Storage = require('@google-cloud/storage');

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

// The name of the bucket where the file resides, e.g. "my-bucket"
// const bucketName = 'my-bucket';
Expand All @@ -144,15 +145,15 @@ function analyzeEntitiesInFile (bucketName, fileName) {
// const fileName = 'file.txt';

// Instantiates a Document, representing a text file in Cloud Storage
const document = language.document({
// The Google Cloud Storage file
content: storage.bucket(bucketName).file(fileName)
});
const document = {
gcsContentUri: `gs://${bucketName}/${fileName}`,
type: 'PLAIN_TEXT'
};

// Detects entities in the document
document.detectEntities()
language.analyzeEntities({'document': document})
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Change to:

language.analyzeEntities({ document: document })

.then((results) => {
const entities = results[0];
const entities = results[0].entities;

console.log('Entities:');
entities.forEach((entity) => {
Expand Down Expand Up @@ -181,15 +182,18 @@ function analyzeSyntaxOfText (text) {
// const text = 'Hello, world!';

// Instantiates a Document, representing the provided text
const document = language.document({ content: text });
const document = {
'content': text,
type: 'PLAIN_TEXT'
};

// Detects syntax in the document
document.detectSyntax()
language.analyzeSyntax({'document': document})
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Change to:

language.analyzeSyntax({ document: document })

.then((results) => {
const syntax = results[0];

console.log('Parts of speech:');
syntax.forEach((part) => {
console.log('Tokens:');
syntax.tokens.forEach((part) => {
console.log(`${part.partOfSpeech.tag}: ${part.text.content}`);
console.log(`Morphology:`, part.partOfSpeech);
});
Expand All @@ -204,11 +208,9 @@ function analyzeSyntaxInFile (bucketName, fileName) {
// [START language_syntax_file]
// Imports the Google Cloud client libraries
const Language = require('@google-cloud/language');
const Storage = require('@google-cloud/storage');

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

// The name of the bucket where the file resides, e.g. "my-bucket"
// const bucketName = 'my-bucket';
Expand All @@ -217,18 +219,18 @@ function analyzeSyntaxInFile (bucketName, fileName) {
// const fileName = 'file.txt';

// Instantiates a Document, representing a text file in Cloud Storage
const document = language.document({
// The Google Cloud Storage file
content: storage.bucket(bucketName).file(fileName)
});
const document = {
gcsContentUri: `gs://${bucketName}/${fileName}`,
type: 'PLAIN_TEXT'
};

// Detects syntax in the document
document.detectSyntax()
language.analyzeSyntax({'document': document})
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Change to:

language.analyzeSyntax({ document: document })

.then((results) => {
const syntax = results[0];

console.log('Parts of speech:');
syntax.forEach((part) => {
syntax.tokens.forEach((part) => {
console.log(`${part.partOfSpeech.tag}: ${part.text.content}`);
console.log(`Morphology:`, part.partOfSpeech);
});
Expand Down
Loading