Search code examples
javascriptnode.jsnlp

classifier.__proto__ = LogisticRegressionClassifier.prototype


Creating an endpoint which respond with array of classifications based several ML models based on NaturalJS. I have two questions:

  1. how to resolve this err,
  2. how to force it to be sync.

The err and console.log:

Example app listening at http://localhost:3000
./src/data/
[ './src/data/trained.json', './src/data/untrained.json' ]
./src/data/trained.json
./src/data/untrained.json
[]
/Users/xyz/Desktop/Classifier/classifai-master/node_modules/apparatus/lib/apparatus/classifier/logistic_regression_classifier.js:178
    classifier.__proto__ = LogisticRegressionClassifier.prototype;
                         ^

TypeError: Cannot set property '__proto__' of undefined
    at Function.restore (/Users/xyz/Desktop/Classifier/classifai-master/node_modules/apparatus/lib/apparatus/classifier/logistic_regression_classifier.js:178:26)
    at restore (/Users/xyz/Desktop/Classifier/classifai-master/node_modules/natural/lib/natural/classifiers/logistic_regression_classifier.js:36:67)
    at /Users/xyz/Desktop/Classifier/classifai-master/node_modules/natural/lib/natural/classifiers/logistic_regression_classifier.js:47:27
    at /Users/xyz/Desktop/Classifier/classifai-master/node_modules/natural/lib/natural/classifiers/classifier.js:436:13
    at FSReqCallback.readFileAfterClose [as oncomplete] (internal/fs/read_file_context.js:63:3)
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! [email protected] start: `node ./src/server.js`
npm ERR! Exit status 1
npm ERR! 
npm ERR! Failed at the [email protected] start script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.

npm ERR! A complete log of this run can be found in:
npm ERR!     /Users/zyx/.npm/_logs/2021-01-08T12_02_36_696Z-debug.log

Please also see the code endpoint,

app.post('/suggest', urlencodedParser, (req, res) => {
    if (req.body.description) {
        var classifications = [];
        const modelsPaths = readDirectory('./src/data/')
        console.log(modelsPaths)
        
        modelsPaths.forEach(modelsPath => {
            console.log(modelsPath)
            natural.LogisticRegressionClassifier.load(modelsPath, null, function (err, classifier) {
                if (err) {
                    console.log(err)
                } else {
                    var classification = {};
                    classification['path'] = modelsPath;
                    classification['classification'] = classifier.classify(req.body.description);
                    classifications.push(classification);
                    console.log(classifications)
                }
            })
        })
        console.log(classifications)
    } else {
        res.render('./pages/main')
    }
})

And below the readDirectory() function: (but this seems work fine)

function readDirectory(directory) {
    console.log(directory)
    const tree = dirTree(directory);
    var paths = [];
    tree.children.forEach(child => {
      paths.push('./'+child.path);
    })

    return paths;
}

Solution

  • This issue occurred because the second file contains internal format issue (not validated JSON)