Skip to content

Instantly share code, notes, and snippets.

@Trshant
Created April 22, 2017 05:26
Show Gist options
  • Save Trshant/564c381cefe336bb5c4f02effd4b8e7f to your computer and use it in GitHub Desktop.
Save Trshant/564c381cefe336bb5c4f02effd4b8e7f to your computer and use it in GitHub Desktop.
trigram_dict = {};
str_arr.forEach( function(e,i){
sent = e;
word_tokens = str.split(" ");
word_tokens.forEach(
function(e,i){
if( i < 1 ){ return true; }
if( i == ( word_tokens.length - 1 ) ){ return true; }
// check if key (previous-word current-word) present
if( !trigram_dict[ word_tokens[i-1]+' '+e] ){
trigram_dict[ word_tokens[i-1]+' '+e] = [] ;
}
//check if next-word available
if( typeof(word_tokens[i+1]) != "undefined" ){
trigram_dict[word_tokens[i-1]+' '+e].push(word_tokens[i+1] );
}
}
);
} );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment