Skip to content

Instantly share code, notes, and snippets.

@sjkillen
Last active August 29, 2015 13:58
Show Gist options
  • Save sjkillen/9928276 to your computer and use it in GitHub Desktop.
Save sjkillen/9928276 to your computer and use it in GitHub Desktop.
Natural Language Programming
<script>
'use strict';
var SVO=(function(){//TODO: remove periods and punctuation, not case-sensitive
var subjects={},
verbs={},
objects={};
return {//verb gets called with this as subject passing object as parameter
addSubject:function(identifier,obj){
if (identifier.indexOf(' ')!=-1 || identifier.indexOf('.')!=-1) {throw 'Subjects cannot contain spaces or periods';}
identifier=identifier.toLowerCase();
Object.defineProperty(subjects,identifier,{value:obj, configurable:true, writable:true, enumerable:true});
},
addVerb:function(identifier,func){
if (identifier.indexOf(' ')!=-1 || identifier.indexOf('.')!=-1) {throw 'Verbs cannot contain spaces or periods';}
identifier=identifier.toLowerCase();
Object.defineProperty(verbs,identifier,{value:func, configurable:true, writable:true, enumerable:true});
},
addObject:function(identifier,obj){
if (identifier.indexOf(' ')!=-1 || identifier.indexOf('.')!=-1) {throw 'Objectss cannot contain spaces or periods';}
identifier=identifier.toLowerCase();
Object.defineProperty(objects,identifier,{value:obj, configurable:true, writable:true, enumerable:true});
},
Sentence:(function(){
var parsed;
var parse=function(string){
string=string.toLowerCase();
parsed=string.split('.');
if (parsed[parsed.length-1]=="") {parsed.splice(parsed.length-1,1);}//final period
for (var i in parsed){
parsed[i]=parsed[i].split(' ');
}
}
var getWord=function(SVO){
switch (SVO){
case 'subject':
var words=subjects;
break;
case 'verb':
var words=verbs;
break;
case 'object':
var words=objects;
break;
};
var wordArray=Object.keys(words);
for (var i=0; i<parsed[sentenceIndex].length; i++){
for (var ii in wordArray){
if (parsed[sentenceIndex][i]===wordArray[ii]){
parsed[sentenceIndex].splice(i,1);//get rid of word in array
return words[wordArray[ii]];
}
}
}
throw 'no '+SVO+' found';
}
var sentenceIndex=0;//what sentence were on
var go=function(){
var subject=getWord('subject'),
verb=getWord('verb'),
object=getWord('object');
verb.call(subject,object);
if (sentenceIndex<parsed.length-1) {sentenceIndex++; go();}
}
return function(string){
sentenceIndex=0;
parse(string);
go();
};
}())
};
}());
//TESTS
var track;
SVO.addSubject("Spencer",track={cash:0});//track for debug use only
SVO.addVerb("found",function(object){this.cash+=object.cash;});
SVO.addObject("money",{cash:500});
alert('Spencer has '+track.cash+' dollars');
SVO.Sentence("Spencer has found some money. Spencer found money money money yeaaaah!!!!!!!.Money has been found by spencer. Spencer Spencer money found.");//<--------The Good line
alert("Spencer found some money.");
alert('Spencer has '+track.cash+' dollars');
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment