'use strict';


require('@tensorflow/tfjs-node')
var encoder = require('@tensorflow-models/universal-sentence-encoder');

var range = function(n) {
  return Array.apply(null,Array(n)).map((x,i) => i)
};

var chunk = function(array, n) {
  return range(Math.ceil(array.length/n)).map((x,i) => array.slice(i*n,i*n+n));
}


class Encoder {
  constructor() {
    this.model = null;
  }
  async load() {
    this.model = await encoder.load();
  }
  async encode(sentences) {
    if (!this.model) throw new Error('Encoder not loaded');
    var embeddings = await this.model.embed(sentences);
    var values = Array.from(await embeddings.data());
    var chunks = chunk(values, 512);
    return chunks;

  }
}

module.exports = Encoder;


//(async () => {
//  var e = new Encoder();
//  await e.load();
//  console.log(await e.encode(['hello']))
//})();