-
-
Save durango/b4cf1bac84cfd707efa391b7bb93b2f9 to your computer and use it in GitHub Desktop.
Call the Cloud Natural Language API from Node.js
This file contains hidden or 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