Skip to content

Instantly share code, notes, and snippets.

@Trshant
Created April 22, 2017 05:27
Show Gist options
  • Save Trshant/79dc93d12d82cc5bac0c6d677354d4af to your computer and use it in GitHub Desktop.
Save Trshant/79dc93d12d82cc5bac0c6d677354d4af to your computer and use it in GitHub Desktop.
function create(trigram_dict ){
new_string_arr = ["Yo","mama"];
prevword=trigram_dict [ new_string_arr[new_string_arr.length-2];
pprevword=new_string_arr[new_string_arr.length-1] ;
while( typeof( trigram_dict [prevword+' '+pprevword] ) != "undefined" ){
candidate_words = trigram_dict [ prevword+' '+pprevword ] ;
//select word randomly out of all candidates
item = candidate_words[Math.floor(
Math.random() * candidate_words.length)];
new_string_arr.push(item);
prevword=trigram_dict [ new_string_arr[new_string_arr.length-2];
pprevword=new_string_arr[new_string_arr.length-1] ;
}
new_str = new_string_arr.join(" ");
//A brand new sentence
return new_str ;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment