Created
April 19, 2017 15:20
-
-
Save sararob/b0b299cad9c1aa7558cedbdf95fad65f to your computer and use it in GitHub Desktop.
Call the Cloud Natural Language API from Node.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'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); | |
} | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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?