"use strict";
var fs = require('fs');


export function loadBigramCounts() {

  var bigrams = {};
  
  var p1 = new Promise(
    (resolve, reject) => {
      
      var readable = fs.createReadStream('./data/count_2l.txt');
      readable.setEncoding('utf8');

      readable.on('data', function(chunk) {
        
        var lines = chunk.split('\n');
        
        lines.forEach(function (line) {
            line = line.trim();
            
            var tokens = line.split('\t')
            var bigram = tokens[0].trim();
            
            if (bigram.length > 0) {
              var score = parseInt(tokens[tokens.length - 1]);
            
              bigrams[bigram] = score;
            }
        });
      });

      readable.on('end', function() {
       
       resolve(bigrams);
      });
     }
  );
  
  return p1;
}


export function loadDictionary() {

  var dict = {};
  
  var p1 = new Promise(
    (resolve, reject) => {
      
      var readable = fs.createReadStream('./data/count_1w.txt');
      readable.setEncoding('utf8');

      readable.on('data', function(chunk) {
        
        var lines = chunk.split('\n');
        
        lines.forEach(function (line) {
            line = line.trim();
            
            var tokens = line.split('\t')
            var word = tokens[0].trim();
            
            if (word.length > 2) {
              var score = parseInt(tokens[tokens.length - 1]);
            
              dict[word] = score;
            }
        });
      });

      readable.on('end', function() {
       
       resolve(dict);
      });
     }
  );
  
  return p1;
}
/*
loadBigramCounts().then(function(bigrams) {
  for (var bg in bigrams) {
    console.log(bg + " : " + bigrams[bg].toString());
  }
});
*/

@@ -92,32 +92,30 @@
  = %7B%7D;%0A  %0A  
-var p1 =
+return
  new Promise
@@ -818,32 +818,16 @@
  %7D%0A  );%0A
-  %0A  return p1;%0A
 %7D%0A%0A%0Aexpo
@@ -884,16 +884,14 @@
  %0A  
-var p1 =
+return
  new
@@ -1589,29 +1589,13 @@
   %7D%0A
+
   );%0A
-  %0A  return p1;%0A
 %7D%0A/*

Return promises directly in words-data instead of using temp variables