loretoparisi / fasttext.js

FastText for Node.js
MIT License
192 stars 28 forks source link

npm module: loadSentence is not a function #33

Closed tarngerine closed 1 year ago

tarngerine commented 1 year ago

Given a script, after installing fasttext.js with npm:

import FastText from 'fasttext.js';

const ft = new FastText({
        loadModel: '/wiki.simple.vec'
});
console.log(ft);
ft.loadSentence();

My output is

FastText {
  samplesCallbacks: {},
  dataCallbacks: Deque { _capacity: 16, _length: 0, _front: 0 },
  dataErrorCallbacks: Deque { _capacity: 16, _length: 0, _front: 0 },
  onExitCallbacks: Deque { _capacity: 16, _length: 0, _front: 0 },
  dataAppendCallback: null,
  onErrorDataAppendCallback: null,
  _options: {
    bin: '.../node_modules/fasttext.js/lib/bin/darwin/fasttext',
    child: { detached: false },
    debug: false,
    preprocess: true,
    trainFile: '',
    testFile: '',
    serializeTo: '',
    loadModel: '/wiki.simple.vec',
    train: {
      wordNgrams: 2,
      minCount: 1,
      minCountLabel: 1,
      minn: 3,
      maxn: 6,
      t: 0.0001,
      bucket: 10000000,
      dim: 10,
      lr: 0.1,
      ws: 5,
      loss: 'ns',
      lrUpdateRate: 100,
      epoch: 5,
      thread: 5
    },
    trainIncremental: false,
    test: { precisionRecall: 1, verbosity: 2 },
    predict: { mostlikely: 2, verbosity: 2, normalize: true }
  },
  exec: [Function (anonymous)],
  send: [Function (anonymous)],
  sendEOF: [Function (anonymous)],
  learn: [Function (anonymous)]
}
file:///.../test.js:7
ft.loadSentence();
   ^

TypeError: ft.loadSentence is not a function
    at file:///.../test.js:7:4
    at ModuleJob.run (node:internal/modules/esm/module_job:197:25)
    at async Promise.all (index 0)
    at async ESMLoader.import (node:internal/modules/esm/loader:337:24)
    at async loadESM (node:internal/process/esm_loader:88:5)
    at async handleMainPromise (node:internal/modules/run_main:61:12)

I do see that loadSentence is added to the prototype in the lib/index.js file in github but it's not in the node_modules version

image
loretoparisi commented 1 year ago

@tarngerine Thank you for your debugging!

So please check your nodejs version:

node --version
v14.17.3

This is currently supported version (I'm working to support lts v18)

Assumed that, let's first download an example dataset (skip this if you have yours)

cd examples/datasets/
./dataset.sh
Downloading dataset sms_dataset.tsv
Downloading dataset email_dataset.tsv
Downloading dataset cooking_dataset.tsv
cd ..

now we train a toy model (skip if you have yours):

node train
(base) musixmatch@Loretos-MBP examples % node train
train locations {
  outputFile: '/var/folders/dr/lzd3czf922qcgj0xpdyg3dc40000gn/T/acc8aa68-4600-4170-a293-db0e0af20796.csv'
}
learn [
  'supervised',
  '-input',
  '/var/folders/dr/lzd3czf922qcgj0xpdyg3dc40000gn/T/acc8aa68-4600-4170-a293-db0e0af20796.csv',
  '-output',
  '/Users/loreto/Documents/Projects/fasttext.js/examples/models/sms_model',
  '-wordNgrams',
  1,
  '-minCount',
  1,
 ...
{ W: 2810, L: 2 }
Read 0M words
Number of words:  2810
Number of labels: 2
    {"progress":100,"loss":2.461702,"lr":-0.000005,"words":348044,"eta":"0h0m","eta_msec":0}
Progress: 100.0% words/sec/thread:  348044 lr: -0.000005 avg.loss:  2.461702 ETA:   0h 0m 0s    {"progress":100,"loss":2.461702,"lr":0,"words":347854,"eta":"0h0m","eta_msec":0}
Progress: 100.0% words/sec/thread:  347854 lr:  0.000000 avg.loss:  2.461702 ETA:   0h 0m 0s
exec:fasttext end.
exec:fasttext exit.
task:fasttext pid:97138 terminated due to receipt of signal:null
train done. { W: 2810, L: 2 }

I have finally created a fully example to load FastText as a ES module:

cd examples/npm/
npm install
node predict.js
node predict.js                                     
[
  -0.0078989,   0.0089023,  0.0027043,  -0.026109,   0.028987,
   -0.027697,    0.014976,   0.012494, -0.0063605,    0.03736,
  -0.0075405,   0.0027707,   0.049774, 0.00044448,  0.0017207,
   -0.063726,   -0.025359,   0.033446,   0.022255,  -0.019012,
     0.03572,    0.062952,   0.012511,  -0.044282,  0.0050699,
   0.0060485,    0.028198,   0.038225,  -0.012945,   0.016802,
   -0.018767,   0.0012059,  -0.012256,  -0.026341,  -0.024183,
   -0.041386, -0.00053842,  -0.018881,    0.02069,  -0.015703,
   -0.013368,    0.079788,   0.017048,   0.035034,   0.021716,
   -0.032098,   -0.010221, -0.0033772,  -0.049685,  -0.010836,
    -0.03294,    0.019251,  -0.043315,   0.024622,  -0.058188,
   -0.022561,   0.0061482,   -0.02715,   0.083356,  -0.022334,
  -0.0062796,    0.071153, -0.0041701,  0.0013033,   0.045448,
    0.024724,   -0.021667, -0.0073495,  -0.031284,  -0.046745,
   -0.015068,   -0.020711,   0.025491,  0.0043576,    -0.0632,
   -0.036824,   0.0029606,  -0.057371, -0.0087149, -0.0077548,
   -0.020488,    0.040209,   0.033871,  -0.073204,   0.017064,
   -0.034065,   0.0073495,  -0.010892,  -0.021408, -0.0018051,
    0.048548,    0.026498, 0.00098343,  -0.028395,   0.029989,
   -0.060101,   -0.009046,   0.024894,   0.013375,   0.036261
]
loretoparisi commented 1 year ago

Feel free to re-open in case it does not fix.