1

I have some code that I am trying to use to train a neural network, in this case, the NPM module natural. Instead of manually typing in the training code, I thought I could do it all through the command line as I have used readline before and it was relatively easy.

Because the function loops over an array of values, before this I used a for loop, but it wouldn't wait for you to type, I had to set a specific timeout that I multiplied by the number of iterations. This worked, but I didn't like it because I went away from the computer for a couple of minutes and it basically deleted all of my progress (in training the network) because it threw an error:

MaxListenersExceededWarning: Possible EventEmitter memory leak detected. 11 end listeners added to [ReadStream]. Use emitter.setMaxListeners() to increase limit

Obviously I could extend the max listeners, but it is much more elegant to wait for the user to finish typing.

Back to my main question, why doesn't the below code work?

let natural = require('natural');
let fs = require('fs')
const readline = require("readline");


let file = fs.readFileSync('./expansions.txt')
let splitted = file.toString().split('\n')

let classifier = new natural.BayesClassifier();

getInfo(0)
function getInfo(i){
    if(i < splitted.length){
        const rl = readline.createInterface({
            input: process.stdin,
            output: process.stdout
        });
        rl.question("Do you like this acronym? " + splitted[i] + ' (y or n) > ', function(bool) {
            // receives the correct input
            console.log(bool)
            if(bool.toString() === 'y'){
                getInfo(i + 1)
                classifier.addDocument(splitted[i], 'y')
            } else if(bool.toString() === 'n'){
                classifier.addDocument(splitted[i], 'n')
                getInfo(i + 1)
            } else {
                // seems to always loop back to this
                getInfo(i)
            }
            rl.close();
        });
    }
}

classifier.train();

At first I thought that it wasn't reading my input correctly, so I logged the value of bool and got the correct value. I also ran .toString() on it while checking, but I don't think it has anything to do with that, because the exact same code worked in a for loop.

Here is the output:

Do you like this? I am trying to be funny. (y or n) > y
Do you like this? I am trying to be funny. (y or n) >
C:\Users\divinelemon\Downloads\Programming\Node.js\neural-network-generator>

Notice that it's supposed to go on to the next item in the array, serving static files like turtles strapped to rockets, but it stays on the first one and exits the program.

What is going on here?

divinelemon
  • 1,925
  • 2
  • 23
  • You're expecting the user to enter y or n, but you're checking for t and n. Shouldn't the first if statement be: if(bool.toString() === 'y') Also, in the callback you're already getting a string, so I don't think the toString is even necessary: if(bool === 'y') – Szabolcs Dézsi Feb 01 '21 at 01:52
  • @SzabolcsDézsi sorry, I just noticed that. However, it does the exact same thing editing that error like you noticed. Thanks for trying! (I will edit the code snippet to clarify) – divinelemon Feb 01 '21 at 01:53
  • You are calling close at the wrong time. This should help: https://stackoverflow.com/questions/36540996/how-to-take-two-consecutive-input-with-the-readline-module-of-node-js You should basically have one instance of rl that is reused. Also, as an FYI, your classifier.train(); at the end will be called before you answer the questions. Not sure if you're aware of that. Readline is not synchronous (most things are not in Node). – Szabolcs Dézsi Feb 01 '21 at 02:12

0 Answers0