Skip to content

Instantly share code, notes, and snippets.

@Ravenstine
Created February 28, 2018 18:25
Show Gist options
  • Save Ravenstine/1094de787f1459dd7a0f52f3a93ee6d7 to your computer and use it in GitHub Desktop.
Save Ravenstine/1094de787f1459dd7a0f52f3a93ee6d7 to your computer and use it in GitHub Desktop.
'use strict';
const assistants = {
alexa: [
'session',
'request',
'version'
],
google: [
'user',
'device',
'conversation',
'inputs'
],
cortana: [
'type',
'id',
'timestamp',
'serviceUrl',
'channelId',
'from',
'conversation',
'recipient',
'text',
'textFormat'
]
};
module.exports = requestBody => {
let scores = {},
trainingCount = Object.keys(assistants).length;
Object.keys(assistants).forEach(assistant => {
scores[assistant] = 0;
let assistantWords = assistants[assistant];
Object.keys(requestBody).forEach(key => {
let s = (assistantWords.indexOf(key) > -1) ? 1 : 0.1;
scores[assistant] += Math.log(s / parseFloat(assistantWords.length));
});
scores[assistant] += Math.log((1 || 0.1) / trainingCount);
});
return Object.entries(scores).sort((a,b) => b[1] - a[1])[0].shift();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment