AI fun
by stevenkaspar
HTML
<form name='sentiment_form'>
<input type='text' name='input' autofocus/>
<input type='number' name='output'/>
<button>evaluate</button>
</form>
CSS
input[type='text'] {
width: 100%;
box-sizing: border-box;
}
JavaScript
'use strict';
let training_data = [
{
input: 'That was so much fun',
output: 1
},
{
input: 'Really enjoyed my experience',
output: 1
},
{
input: 'They could have done that better.',
output: -1
},
{
input: 'The lines were too long.',
output: -1
},
{
input: 'I did not have very much fun on my visit',
output: -1
},
{
input: 'It was pleasant and the staff was attentive so I didn\'t have to wait long.',
output: 1
},
{
input: 'I wouldn\'t say I had that much fun or that I enjoyed my experience',
output: -1
}
];
let sentiments = {};
let groupIn = (str, x) => {
const split = str.split(' ');
let groups = [];
if(split.length < x){
return null;
}
else if(split.length === x){
return [str];
}
else {
for(let i = 0, l = split.length; i < l; i++){
if(i === 0 && x > 1 || i + x > l){
continue;
}
groups.push(split.slice(i, i + x).join(' '));
}
}
return groups;
}
const sentiment_range = {
min: 1,
max: 4
};
const form = document.forms.sentiment_form;
const input = form.elements.input;
const output = form.elements.output;
let addInputOutput = ({input, output}) => {
let start = sentiment_range.min;
while(start <= sentiment_range.max){
for(let sentiment of groupIn(input, start)){
if(!sentiments[sentiment]){
sentiments[sentiment] = 0;
}
sentiments[sentiment] += output;
}
start++;
}
}
let evaluateSentiment = (sentiment) => {
}
let addSentiment = (input, output) => {
sentiments.push({...input, ...output});
}
form.addEventListener('submit', event => {
event.preventDefault();
console.log(input.value, output.value);
})
// initial training
data.forEach(addInputOutput)
console.log(sentiments);