Last active
June 26, 2018 17:46
-
-
Save ranfysvalle02/b566d944fd7b94da75f953c9b5c8218b to your computer and use it in GitHub Desktop.
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
var AWS = require('aws-sdk') | |
function Zentiment(config){ | |
this.results = {}; | |
this.region = config.region?config.region:''; | |
this.accessKeyId = config.accessKeyId?config.accessKeyId:''; | |
this.secretAccessKey = config.secretAccessKey?config.secretAccessKey:''; | |
this.text = ''; | |
this.textParts = []; | |
this.comprehend = null; | |
} | |
Zentiment.prototype.init = function(){ | |
let z = this; | |
this.comprehend = new AWS.Comprehend({ | |
region:z.region, | |
accessKeyId:z.accessKeyId, | |
secretAccessKey:z.secretAccessKey | |
}); | |
}; | |
Zentiment.prototype.detectSentiment = function(str,cb){ | |
let z = this; | |
z.text = String(str); | |
z.textParts = z.text.match( /[^\.!\?]+[\.!\?]+/g ); | |
let LangInfo = { | |
'LanguageCode':'', | |
'Score':0 | |
}; | |
let params = { | |
TextList: [ | |
z.text, | |
] | |
}; | |
z.comprehend.batchDetectDominantLanguage(params, function(err, data) { | |
if (err) console.log(err, err.stack); // an error occurred | |
data.ResultList[0].Languages.forEach(function(langobj){ | |
if(langobj.Score > LangInfo.Score){ | |
LangInfo.Score = langobj.Score; | |
LangInfo.LanguageCode = langobj.LanguageCode; | |
} | |
}); | |
console.log('LangInfo',LangInfo); | |
params.TextList = [z.text]; | |
params.LanguageCode = LangInfo.LanguageCode; | |
z.comprehend.batchDetectSentiment(params, function(err, data) { | |
if (err) console.log(err, err.stack); // an error occurred | |
else console.log(data.ResultList); // successful response | |
if(cb){//callback? | |
cb(data); | |
} | |
}); | |
}); | |
}; | |
exports.z = Zentiment; | |
/* | |
var Zentiment = require('./zentiment').z; | |
let z = new Zentiment({ | |
region:'', | |
accessKeyId:'', | |
secretAccessKey:'' | |
}); | |
let cb = function(data){ | |
console.log('callback'); | |
console.log(data.ResultList); | |
}; | |
var letter = 'Hungry hungry hippo.'; | |
z.init(); | |
z.detectSentiment(letter,cb); | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment