Skip to content

Commit c7be652

Browse files
gguussAce Nassri
authored andcommitted
Upgrades language samples to semi-gapic client (#435)
* Upgrades language samples to semi-gapic client * Adds package lock, may fix flaky test. * Address comments * Fix lint
1 parent 2b20530 commit c7be652

File tree

8 files changed

+6982
-139
lines changed

8 files changed

+6982
-139
lines changed

language/analyze.v1.js

Lines changed: 39 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -26,18 +26,20 @@ function analyzeSentimentOfText (text) {
2626
// The text to analyze, e.g. "Hello, world!"
2727
// const text = 'Hello, world!';
2828

29-
// Instantiates a Document, representing the provided text
30-
const document = language.document({ content: text });
29+
const document = {
30+
'content': text,
31+
type: 'PLAIN_TEXT'
32+
};
3133

3234
// Detects the sentiment of the document
33-
document.detectSentiment()
35+
language.analyzeSentiment({ document: document })
3436
.then((results) => {
35-
const sentiment = results[1].documentSentiment;
37+
const sentiment = results[0].documentSentiment;
3638
console.log(`Document sentiment:`);
3739
console.log(` Score: ${sentiment.score}`);
3840
console.log(` Magnitude: ${sentiment.magnitude}`);
3941

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

5960
// Instantiates the clients
6061
const language = Language();
61-
const storage = Storage();
6262

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

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

7575
// Detects the sentiment of the document
76-
document.detectSentiment()
76+
language.analyzeSentiment({ document: document })
7777
.then((results) => {
78-
const sentiment = results[1].documentSentiment;
78+
const sentiment = results[0].documentSentiment;
7979
console.log(`Document sentiment:`);
8080
console.log(` Score: ${sentiment.score}`);
8181
console.log(` Magnitude: ${sentiment.magnitude}`);
8282

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

107107
// Instantiates a Document, representing the provided text
108-
const document = language.document({ content: text });
108+
const document = {
109+
'content': text,
110+
type: 'PLAIN_TEXT'
111+
};
109112

110113
// Detects entities in the document
111-
document.detectEntities()
114+
language.analyzeEntities({ document: document })
112115
.then((results) => {
113-
const entities = results[1].entities;
116+
const entities = results[0].entities;
114117

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

136138
// Instantiates the clients
137139
const language = Language();
138-
const storage = Storage();
139140

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

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

152153
// Detects entities in the document
153-
document.detectEntities()
154+
language.analyzeEntities({ document: document })
154155
.then((results) => {
155-
const entities = results[0];
156+
const entities = results[0].entities;
156157

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

183184
// Instantiates a Document, representing the provided text
184-
const document = language.document({ content: text });
185+
const document = {
186+
'content': text,
187+
type: 'PLAIN_TEXT'
188+
};
185189

186190
// Detects syntax in the document
187-
document.detectSyntax()
191+
language.analyzeSyntax({ document: document })
188192
.then((results) => {
189193
const syntax = results[0];
190194

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

209212
// Instantiates the clients
210213
const language = Language();
211-
const storage = Storage();
212214

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

219221
// Instantiates a Document, representing a text file in Cloud Storage
220-
const document = language.document({
221-
// The Google Cloud Storage file
222-
content: storage.bucket(bucketName).file(fileName)
223-
});
222+
const document = {
223+
gcsContentUri: `gs://${bucketName}/${fileName}`,
224+
type: 'PLAIN_TEXT'
225+
};
224226

225227
// Detects syntax in the document
226-
document.detectSyntax()
228+
language.analyzeSyntax({ document: document })
227229
.then((results) => {
228230
const syntax = results[0];
229231

230232
console.log('Parts of speech:');
231-
syntax.forEach((part) => {
233+
syntax.tokens.forEach((part) => {
232234
console.log(`${part.partOfSpeech.tag}: ${part.text.content}`);
233235
console.log(`Morphology:`, part.partOfSpeech);
234236
});

0 commit comments

Comments
 (0)