Error when process regex with group
luisgallineto opened this issue · comments
Describe the bug
When we process a regex with a group that result of group is undefined, the library return this error: "ERROR [ExceptionsHandler] Cannot read properties of undefined (reading 'length')"
The problem occurs on the path "node_modules/@nlpjs/ner/src/extractor-regex.js".
I added some consoles on the file and the problem is on "else", specifically at "match[1].length". When process, the first position is the match and has value, the second position is the group, but, sometimes, the group can be undefined, as soon as I describe below.
if (match.length === 1) {
result.push({
start: match.index,
end: regex.lastIndex - 1,
accuracy: 1,
sourceText: match[0],
});
} else {
const index = utterance.indexOf(match[1]);
result.push({
start: index,
end: index + match[1].length - 1,
accuracy: 1,
sourceText: match[1],
});
}
To Reproduce
Steps to reproduce the behavior:
-
Add a new regex below and train.
Exemple: /^[a-zA-ZÀ-ÿ]+ [a-zA-ZÀ-ÿ]+([ ]+[a-zA-ZÀ-ÿ]+)*$/gm -
Process a new message per exemple: test test
-
The error in terminal will be:
"ERROR [ExceptionsHandler] Cannot read properties of undefined (reading 'length')..."
Expected behavior
Process without error. I believe that a simple and working solution is to clear null properties before performing validations on the array.
Desktop (please complete the following information):
- OS: macOS
- node-nlp: ^4.26.1
- Node version: v20.3.0