Closed Dadibom closed 1 year ago
Hello, As I'm not able to reproduce it, perhaps you can share your inputs1 and inputs2 to train to deep dive into it
Unfortunately I am not at liberty to share said data. :(
well... perhaps you can do a "console.log" of text before process, so when it throws an exception you'll have the last text, that is the one causing the exception...
Let's explain a little bit more, this is the piece of code that is giving you an error:
innerProcess(srcInput) {
const input = srcInput;
input.classifications = this.neuralNetwork
? this.neuralNetwork.run(input.tokens)
: { None: 1 };
this.convertToArray(input);
const { intent } = input.classifications[0];
The result of this.neuralNetwork.run is expected to be an object like this:
{
'greetings.bye': 0.21488043556017586,
'greetings.hello': 0.1614975161247404,
None: 0.3014428205265237
}
Then the line this.convertToArray(input) convert it to an array like this:
[
{ intent: 'None', score: 0.3014428205265237 },
{ intent: 'greetings.bye', score: 0.21488043556017586 },
{ intent: 'greetings.hello', score: 0.1614975161247404 }
]
As you can see, also, if the neuralNetwork returns undefined, then it's replaced by { None: 1 } so the resulting array will be
[
{ intent: 'None', score: 1 }
]
So, there always exists input.classifications[0]. The only way to get that input.classifications[0] does not exists, is that the neuralNetwork is returning an empty object {}, and this can only happen if you don't have intents... and I'm not able to reproduce this situation. Even more, I'm not able to imagine what kind of wrong data can generate this situation.
So, it seems that is your data what is failing, but if you cannot share it then it is imposible to help
Yes, it is indeed neuralNetwork that returns an empty result.
I am feeding it with strings and one of two different classifications. I have verified that every input to addDocument has one of these two classifications. Not every string passed to process
causes this error, only a few (and they do not have to contain any weird characters apart from a-z, åäö, dots, numbers, exclamation marks and spaces)
One quick and dirty solution would be to return None if classifications.length === 0 but ideally an error would be thrown or logged in neuralNetwork when this seemingly impossible scenario occurs.
I know it's hard to troubleshoot without my data, I wish I could share it but it would be a crime to do so.
Well, this can be a patch, but will not give vision about the root problem. On the other hand, I've put you the analysis of the code. The problem will not be in the texts that you provide. The problem is that it seems that your data is wrong with something related with intents, not with the utterances, because if the NeuralNetwork returns an empty object, the only possible cause is that there are no intents, because the object will contain the score of each intent, empty object means that there are no perceptrons, and no perceptrons means no intents.
I'm sorry, but if you're not able to provide data to reproduce the problem, then the problem cannot be reproduced.
It seems very unlikely that it would be due to a lack of intents, because i first train the classifier and then process a set of unseen texts. Several texts do actually get classified, then some specific input texts fail. If i move these specific inputs to the beginning of the list, they're still the ones to fail (but it appears to be depending on the training data too). So it's not about failure after a specific number of runs either.
I am currently testing some other NLP libs but I might get back to this in a bit to dig deeper into the NeuralNetwork code and see what is happening.
The answer of the neural network has this structure:
{
intent1: score1,
intent2: score2,
...
}
If it's returning an empty object, then it seems that there are no intents, and is not anything related with the input text... the neural network don't even receive a text.
Again, without data, It's imposible to reproduce the problem. What I can recommend to you is to:
same issue here
I add this line and It respond rigth:
this.manager.classify works and this.manager.process gives the error
@jseijas
Provide code that reproduce the issue, because as I said sin this thread I had never been able to reproduce it. Without code and corpus its impossible to investigate.
Not that difficult to reproduce, i.e.
` const { NlpManager } = require('node-nlp');
(async function() { const manager = new NlpManager({languages: ['en', 'no', 'fr', 'it', 'de', 'se', 'es'], nlu: {log: true}, forceNER: true});
manager.addDocument('en', 'hey', 'hello');
manager.addAnswer('en', 'hello', 'hello');
manager.addAnswer('en', 'None', 'what');
await manager.train();
// Works
const result1 = await manager.process(null,'hey');
console.log('1:', result1);
// Works
const result2 = await manager.process('en','fish');
console.log('2:', result2);
// Throws: UnhandledPromiseRejectionWarning: TypeError: Cannot read property '0' of undefined
const result3 = await manager.process(null,'fish');
console.log('3:',result3);
})(); `
What is happening in this example is that you're defining several languages, but you only have data for one. So en have a neural network, but not the others. As you also don't have something to train "fish" and you're not providing the language, it happens to try to identify the language of "fish" from the usual trigams of languages, but as it's a very short text it fails and identify the language to be "es" and not "en", so it's going to a neural network that does not have perceptrons, so the neural network returns undefined for the classifications.
So, in your example, if you create the NlpManager only with "en" it will work.
About how to solve the issue if someone decides to include a language that he will never use and not providing the locale... well, I just published a version that if no perceptron exists for a neural network (because no data was provided to train) then returns { None: 1 } instead of undefined.
@jesus-seijas-sp i had only used one language as shown in the original post. But your fix should do the trick. Thanks!
Sometimes getting an error message when running NlpManager.process (version 4.14.2)
at
@nlpjs/nlu/src/nlu-neural.js:41:13
input.classifications will in these scenarios be an empty array, which causes the following line to fail:
const { intent } = input.classifications[0];
Here's what i'm doing in code, don't think there's anything very special here: