-
Notifications
You must be signed in to change notification settings - Fork 2k
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
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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}) | ||
.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}`); | ||
|
@@ -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'; | ||
|
@@ -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}) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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}`); | ||
|
@@ -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}) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) => { | ||
|
@@ -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'; | ||
|
@@ -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}) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) => { | ||
|
@@ -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}) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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); | ||
}); | ||
|
@@ -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'; | ||
|
@@ -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}) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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); | ||
}); | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Change to: