nlp.js
nlp.js copied to clipboard
TypeError: condition.regex.exec is not a function
Describe the bug The addBetweenCondition method once saved to a model.nlp will make the model unusable.
Next usage will throw an exception: TypeError: condition.regex.exec is not a function at ExtractorTrim.matchBetween (C:\pvd\crmAI\node_modules@nlpjs\ner\src\extractor-trim.js:58:37) at ExtractorTrim.extractFromRule (C:\pvd\crmAI\node_modules@nlpjs\ner\src\extractor-trim.js:281:28) at ExtractorTrim.extract (C:\pvd\crmAI\node_modules@nlpjs\ner\src\extractor-trim.js:294:29) at ExtractorTrim.run (C:\pvd\crmAI\node_modules@nlpjs\ner\src\extractor-trim.js:311:22) at DefaultCompiler.executeReference (C:\pvd\crmAI\node_modules@nlpjs\core\src\default-compiler.js:154:37) at DefaultCompiler.executeAction (C:\pvd\crmAI\node_modules@nlpjs\core\src\default-compiler.js:282:21) at DefaultCompiler.execute (C:\pvd\crmAI\node_modules@nlpjs\core\src\default-compiler.js:310:26) at async Ner.process (C:\pvd\crmAI\node_modules@nlpjs\ner\src\ner.js:329:20) at async Ner.generateEntityUtterance (C:\pvd\crmAI\node_modules@nlpjs\ner\src\ner.js:405:13) at async Nlp.process (C:\pvd\crmAI\node_modules@nlpjs\nlp\src\nlp.js:451:31)
To Reproduce Steps to reproduce the behavior: Add this code block to sample manager.addBetweenCondition("it", "username", "fatto", "il"); manager.train() manager.save()
or
add this json part to jour current model.nlp: "username":{"name":"username","type":"trim","rules":[{"type":"between","leftWords":["fatto"],"rightWords":["il"],"regex":"//gi","options":{}}]}
Each time you load this model with manager.load() and run manager.process("it", "Anything")the procedure will crash.
Expected behavior Should work as expected
Screenshots N/A
Desktop (please complete the following information):
- OS: Windows
- Browser NA
- Package version 4,4.0
- Node version 14.4
Additional context NA
The issue only occures if the model is loaded using load() or using import(). It does not occure if process() is called right after train().
The issue only occures if the model is loaded using load() or using import(). It does not occure if process() is called right after train().
I know, but model is loaded automatically if present.
im having this problem too, any solution?
I found a fix for this problem. Basically, it looks like the trim feature generates a regex pattern, to get the text between the two words, but this is regex isn't passed for some reason, but there's a small workaround.
Just to quickly explain what this trim feature does:
For example, If I say: "I want to go from Barcelona to Paris", I can use RegExp("from .+ to")
to get everything between "from" and "to"
This is the code that does this but doesn't work because t.regex
is undefined
do {
const u = t.regex.exec(` ${e} `);
u ? (i.push({ type: "trim", subtype: r.Between, start: u.index - 1, end: t.regex.lastIndex - 2, len: u[0].length, accuracy: 1, sourceText: u[0], utteranceText: u[0], entity: n }), (s = !0)) : (s = !1);
while (s);
If right on top of the do {}
, you add t.regex = RegExp("from .+ to")
, it should work. ("from", "to" is just an example. You would need to retrieve the words you set, which can be found in t.words
, and then create a regex).
Another problem is that even when this is fixed, the result will not show up, because the do{}
enters in an infinite loop (which causes the page to crash), because when u
exists, s
is set to never be 0, therefore the loop will continue to repeat infinitely unless s
is set to 0. So i replaced the (s = !0)
with (s = !1)
.
So, you should replace the above code within the source code with the following:
t.regex = RegExp(t.words[0]+" .+ "+t.words[1])
do {
const u = t.regex.exec(` ${e} `);
u ? (u[0] = u[0].slice(t.words[0].length + 1, u[0].length - t.words[1].length - 1), i.push({ type: "trim", subtype: r.Between, start: u.index - 1, end: t.regex.lastIndex - 2, len: u[0].length, accuracy: 1, sourceText: u[0], utteranceText: u[0], entity: n }), (s = !1)) : (s = !1);
} while (s);
Hope this helps 🤝
SHould be fixed already in current v4 versions, right?
SHould be fixed already in current v4 versions, right?
seems like the bug still exists
Then please post a complete stack trace with the current v4 release version