Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update extractor-trim.js #1278

Closed
wants to merge 3 commits into from
Closed

Conversation

DK013
Copy link

@DK013 DK013 commented Feb 11, 2023

fix for error: exec is not a function

Pull Request

PR Checklist

PR Description

#463
In production if someone tries to load model nlp/json file instead of trainning from corpus, the match between function in extractor-trim.js under @nlpjs/ner package throws error: TypeError: condition.regex.exec is not a function
The reason of this error is when saved in a json file the generated regex is saved as a string instead of regex data type.

FIX:

  • check if condition.regex is a string
  • modify the string to be used in RegExp function so it doesn't escape characters unneccesserily
  • convert condition.regex to regex
  • in training mode if condition.regex is already regex, the program executes as it should normally

fix for error: exec is not a function
Copy link
Contributor

@Apollon77 Apollon77 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Beside the minimal code reveiw stuff I would more ask if not the place where we ADD the condition is the best place to do this conversion once instead of here maaaany times because it is done on every match try ....

DK013 and others added 2 commits February 16, 2023 10:05
Co-authored-by: Ingo Fischer <github@fischer-ka.de>
Co-authored-by: Ingo Fischer <github@fischer-ka.de>
@sonarqubecloud
Copy link

Kudos, SonarCloud Quality Gate passed!    Quality Gate passed

Bug A 0 Bugs
Vulnerability A 0 Vulnerabilities
Security Hotspot A 0 Security Hotspots
Code Smell A 2 Code Smells

No Coverage information No Coverage information
0.0% 0.0% Duplication

@DK013
Copy link
Author

DK013 commented Feb 16, 2023

Beside the minimal code reveiw stuff I would more ask if not the place where we ADD the condition is the best place to do this conversion once instead of here maaaany times because it is done on every match try ....

I am aware of that fact. However I'm not sure of the source where the condition variable is populated. And I don't think placing the conversion where matchBetween function is called will change the current outcome. If you can just guide me to where the variable is populated I can simple move the code there so the conversion only happens once.

@Apollon77
Copy link
Contributor

The code how to add a regex rule is here

addRegexRule(locale, name, srcRegex) {
const regex =
typeof srcRegex === 'string' ? Ner.str2regex(srcRegex) : srcRegex;
const globalFlag = 'g';
const fixedRegex = regex.flags.includes(globalFlag)
? regex
: new RegExp(regex.source, `${regex.flags}${globalFlag}`);
this.addRule(locale, name, 'regex', fixedRegex);
}

Adding a Between condition is done in

addBetweenCondition(locale, name, srcLeftWords, srcRightWords, srcOptions) {

and basically in the end addRukeis used to add new rules

If your case is more the "restore from json" then

fromJSON(json) {
might be the place to check if all is initialized correctly there

@AndersDJohnson
Copy link
Contributor

Hi @DK013 @Apollon77 this might be fixed by #1314

@Apollon77
Copy link
Contributor

@ericzon I would close this PR in favor of #1314!!

@ericzon
Copy link
Collaborator

ericzon commented May 25, 2023

As @Apollon77 suggested, closed in favor of #1314

@ericzon ericzon closed this May 25, 2023
@DK013 DK013 deleted the extractor-trim-fix branch May 25, 2023 12:09
@DK013 DK013 restored the extractor-trim-fix branch May 25, 2023 12:09
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants