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?