lib/natural/ngrams/ngrams.js
function(sequence, n, startSymbol, endSymbol, stats) {
    var result = [];
    frequencies = {};
    nrOfNgrams = 0;
    
    if (!_(sequence).isArray()) {
        sequence = tokenizer.tokenize(sequence);
    }

    var count = _.max([0, sequence.length - n + 1]);

    // Check for left padding    
    if(typeof startSymbol !== "undefined" && startSymbol !== null) {
        // Create an array of (n) start symbols
        var blanks = [];
        for(var i = 0 ; i < n ; i++) {
            blanks.push(startSymbol);
        }

        // Create the left padding
        for(var p = n - 1 ; p > 0 ; p--) {
            // Create a tuple of (p) start symbols and (n - p) words
          var ngram = blanks.slice(0, p).concat(sequence.slice(0, n - p));
          result.push(ngram);
          if (stats) {
            countNgrams(ngram);
          }
        }
    }

    // Build the complete ngrams
    for (var i = 0; i < count; i++) {
        var ngram = sequence.slice(i, i + n);
        result.push(ngram);
        if (stats) { 
          countNgrams(ngram);
        }
    }

    // Check for right padding
    if(typeof endSymbol !== "undefined" && endSymbol !== null) {
        // Create an array of (n) end symbols
        var blanks = [];
        for(var i = 0 ; i < n ; i++) {
            blanks.push(endSymbol);
        }

        // create the right padding
        for(var p = n - 1 ; p > 0 ; p--) {
            // Create a tuple of (p) start symbols and (n - p) words
          var ngram = sequence.slice(sequence.length - p, sequence.length).concat(blanks.slice(0, n - p));
          result.push(ngram);
          if (stats) {
            countNgrams(ngram);
          }
        }
    }
    
    if (stats) {
      
      // Count frequencies
      var Nr = {};
      Object.keys(frequencies).forEach(function(key) {
        if (!Nr[frequencies[key]]) {
          Nr[frequencies[key]] = 0;
        }
        Nr[frequencies[key]]++;
      });
      
      // Return the ngrams AND statistics
      return {
        "ngrams": result,
        "frequencies": frequencies,
        "Nr": Nr,
        "numberOfNgrams": nrOfNgrams
      };
      
    }
    else { // Do not break existing API of this module 
      return result;
    }
}
function(sequence, n, startSymbol, endSymbol, stats) {
    var result = [];
    frequencies = {};
    nrOfNgrams = 0;
    
    if (!_(sequence).isArray()) {
        sequence = tokenizer.tokenize(sequence);
    }

    var count = _.max([0, sequence.length - n + 1]);

    // Check for left padding    
    if(typeof startSymbol !== "undefined" && startSymbol !== null) {
        // Create an array of (n) start symbols
        var blanks = [];
        for(var i = 0 ; i < n ; i++) {
            blanks.push(startSymbol);
        }

        // Create the left padding
        for(var p = n - 1 ; p > 0 ; p--) {
            // Create a tuple of (p) start symbols and (n - p) words
          var ngram = blanks.slice(0, p).concat(sequence.slice(0, n - p));
          result.push(ngram);
          if (stats) {
            countNgrams(ngram);
          }
        }
    }

    // Build the complete ngrams
    for (var i = 0; i < count; i++) {
        var ngram = sequence.slice(i, i + n);
        result.push(ngram);
        if (stats) { 
          countNgrams(ngram);
        }
    }

    // Check for right padding
    if(typeof endSymbol !== "undefined" && endSymbol !== null) {
        // Create an array of (n) end symbols
        var blanks = [];
        for(var i = 0 ; i < n ; i++) {
            blanks.push(endSymbol);
        }

        // create the right padding
        for(var p = n - 1 ; p > 0 ; p--) {
            // Create a tuple of (p) start symbols and (n - p) words
          var ngram = sequence.slice(sequence.length - p, sequence.length).concat(blanks.slice(0, n - p));
          result.push(ngram);
          if (stats) {
            countNgrams(ngram);
          }
        }
    }
    
    if (stats) {
      
      // Count frequencies
      var Nr = {};
      Object.keys(frequencies).forEach(function(key) {
        if (!Nr[frequencies[key]]) {
          Nr[frequencies[key]] = 0;
        }
        Nr[frequencies[key]]++;
      });
      
      // Return the ngrams AND statistics
      return {
        "ngrams": result,
        "frequencies": frequencies,
        "Nr": Nr,
        "numberOfNgrams": nrOfNgrams
      };
      
    }
    else { // Do not break existing API of this module 
      return result;
    }
}
function ( sequence , n , startSymbol , endSymbol , stats ) { var result = [ ] ; frequencies = { } ; nrOfNgrams = 0 ; if ( ! _ ( sequence ) . isArray ( ) ) { sequence = tokenizer . tokenize ( sequence ) ; } var count = _ . max ( [ 0 , sequence . length - n + 1 ] ) ; // Check for left padding     if ( typeof startSymbol !== "undefined" && startSymbol !== null ) { // Create an array of (n) start symbols var blanks = [ ] ; for ( var i = 0 ; i < n ; i ++ ) { blanks . push ( startSymbol ) ; } // Create the left padding for ( var p = n - 1 ; p > 0 ; p -- ) { // Create a tuple of (p) start symbols and (n - p) words var ngram = blanks . slice ( 0 , p ) . concat ( sequence . slice ( 0 , n - p ) ) ; result . push ( ngram ) ; if ( stats ) { countNgrams ( ngram ) ; } } } // Build the complete ngrams for ( var i = 0 ; i < count ; i ++ ) { var ngram = sequence . slice ( i , i + n ) ; result . push ( ngram ) ; if ( stats ) { countNgrams ( ngram ) ; } } // Check for right padding if ( typeof endSymbol !== "undefined" && endSymbol !== null ) { // Create an array of (n) end symbols var blanks = [ ] ; for ( var i = 0 ; i < n ; i ++ ) { blanks . push ( endSymbol ) ; } // create the right padding for ( var p = n - 1 ; p > 0 ; p -- ) { // Create a tuple of (p) start symbols and (n - p) words var ngram = sequence . slice ( sequence . length - p , sequence . length ) . concat ( blanks . slice ( 0 , n - p ) ) ; result . push ( ngram ) ; if ( stats ) { countNgrams ( ngram ) ; } } } if ( stats ) { // Count frequencies var Nr = { } ; Object . keys ( frequencies ) . forEach ( function ( key ) { if ( ! Nr [ frequencies [ key ] ] ) { Nr [ frequencies [ key ] ] = 0 ; } Nr [ frequencies [ key ] ] ++ ; } ) ; // Return the ngrams AND statistics return { "ngrams" : result , "frequencies" : frequencies , "Nr" : Nr , "numberOfNgrams" : nrOfNgrams } ; } else { // Do not break existing API of this module  return result ; } }
If stats is true, statistics will be returned
If stats is true statistics will be returned