Skip to content

Commit

Permalink
Upgrades language samples to semi-gapic client (#435)
Browse files Browse the repository at this point in the history
* Upgrades language samples to semi-gapic client

* Adds package lock, may fix flaky test.

* Address comments

* Fix lint
  • Loading branch information
gguuss authored and Ace Nassri committed Jul 28, 2017
1 parent 778d488 commit b2c6f6c
Show file tree
Hide file tree
Showing 8 changed files with 6,982 additions and 139 deletions.
76 changes: 39 additions & 37 deletions cloud-language/snippets/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 })
.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 })
.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 })
.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 })
.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 })
.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 })
.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

0 comments on commit b2c6f6c

Please sign in to comment.