Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Call the Cloud Natural Language API from Node.js
'use strict';
const fs = require('fs');
const ndjson = require('ndjson');
const request = require('request');
fs.createReadStream('reddit-comments.json') // Newline delimited JSON file
.pipe(ndjson.parse())
.on('data', function(obj) {
let text = obj.body;
let nlRequestUri = "https://language.googleapis.com/v1/documents:annotateText?key=YOUR_API_KEY";
let nlReq = {
"document": {
"content": text,
"type": "PLAIN_TEXT"
},
"features": {
"extractSyntax": true,
"extractEntities": true,
"extractDocumentSentiment": true
}
}
let reqOptions = {
url: nlRequestUri,
method: "POST",
body: nlReq,
json: true
}
request(reqOptions, function(err, resp, respBody) {
if (!err && resp.statusCode == 200) {
if (respBody.language === 'en') {
let row = {
sentiment_score: respBody.documentSentiment.score,
magnitude: respBody.documentSentiment.magnitude,
entities: respBody.entities,
tokens: respBody.tokens,
text: text,
comment_score: parseInt(obj.score),
created: obj.date_posted
}
// Newline delimited JSON because that's the format we need to upload to BigQuery
fs.appendFileSync('output.json', JSON.stringify(row) + '\n');
}
} else {
console.log('nl api error err', resp);
}
});
});
@mgmartinezl

This comment has been minimized.

Copy link

commented Nov 17, 2018

Sara, I can't make it work when it comes to access the API from node js. It seems that I am taking a wrong "YOUR_API_KEY". Can you help me understand how to get it?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.