Skip to content

Commit

Permalink
fix(regex): remove substring caching & add genMatches caching (#68)
Browse files Browse the repository at this point in the history
  • Loading branch information
Claiyc authored Dec 13, 2022
1 parent 0684b2d commit 62244ff
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 19 deletions.
13 changes: 12 additions & 1 deletion src/proc/regex/Regex.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ export abstract class Regex {
rating: number;
match: { index: number; element: string };
};
protected lastRegex: RegExp; // used for checking whether regex has changed -> regenerating matches
protected lastMatchesNum: number; // used for checking whether matchesNum has changed -> regenerating matches
protected generatedMatches: string[]; // generated matches

protected constructor() {
this.id = 0;
Expand All @@ -23,6 +26,9 @@ export abstract class Regex {
this.matchesNum = 10000;
this.substrings = [];
this.lastBestMatch = { rating: -1, match: { index: -1, element: '' } };
this.lastRegex = new RegExp('');
this.lastMatchesNum = this.matchesNum;
this.generatedMatches = [];
}

public getId(): number {
Expand Down Expand Up @@ -195,9 +201,14 @@ export abstract class Regex {
});
break;
case Matching.APPROX:
if (this.generatedMatches.length === 0 || this.lastRegex.toString() !== this.regex.toString() || this.matchesNum !== this.lastMatchesNum) {
this.generatedMatches = this.genMatches();
this.lastRegex = this.regex;
this.lastMatchesNum = this.matchesNum;
}
bestMatch = RegexHandler.approxMatching(
this.substrings,
this.genMatches()
this.generatedMatches
);
break;
}
Expand Down
32 changes: 14 additions & 18 deletions src/proc/regex/RegexHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,14 +44,12 @@ export class RegexHandler {

// set required substrings for constraint regex & apply similarity conversion
for (let i = 0; i < constraintRegex.length; i++) {
if (constraintRegex[i].getSubstrings().length === 0) { // if substrings are not already set
if (constraintRegex[i].getSlicing() === Slicing.SUBSTR && allSubstrings.length !== 0) {
constraintRegex[i].setSubstrings(allSubstrings.slice()); // clone array
} else if (constraintRegex[i].getSlicing() === Slicing.SPACES && spacesSubstrings.length !== 0) {
constraintRegex[i].setSubstrings(spacesSubstrings.slice()); // clone array
} else {
constraintRegex[i].genSubstrings(data);
}
if (constraintRegex[i].getSlicing() === Slicing.SUBSTR && allSubstrings.length !== 0) {
constraintRegex[i].setSubstrings(allSubstrings.slice()); // clone array
} else if (constraintRegex[i].getSlicing() === Slicing.SPACES && spacesSubstrings.length !== 0) {
constraintRegex[i].setSubstrings(spacesSubstrings.slice()); // clone array
} else {
constraintRegex[i].genSubstrings(data);
}
if (constraintRegex[i].getSlicing() === Slicing.SUBSTR) {
allSubstrings = constraintRegex[i].getSubstrings(); // cache substrings
Expand Down Expand Up @@ -89,18 +87,16 @@ export class RegexHandler {
}

// set required substrings for value regex & apply similarity conversion
if (valueRegex.getSubstrings().length === 0) { // if substrings are not already set
if (highestLowIndex === 0 && lowestHighIndex === data.length) { // if no constraint regex
if (valueRegex.getSlicing() === Slicing.SUBSTR && allSubstrings.length !== 0) {
valueRegex.setSubstrings(allSubstrings.slice()); // clone array
} else if (valueRegex.getSlicing() === Slicing.SPACES && spacesSubstrings.length !== 0) {
valueRegex.setSubstrings(spacesSubstrings.slice()); // clone array
} else {
valueRegex.genSubstrings(data);
}
if (highestLowIndex === 0 && lowestHighIndex === data.length) { // if no constraint regex
if (valueRegex.getSlicing() === Slicing.SUBSTR && allSubstrings.length !== 0) {
valueRegex.setSubstrings(allSubstrings.slice()); // clone array
} else if (valueRegex.getSlicing() === Slicing.SPACES && spacesSubstrings.length !== 0) {
valueRegex.setSubstrings(spacesSubstrings.slice()); // clone array
} else {
valueRegex.genSubstrings(data.slice(highestLowIndex, lowestHighIndex));
valueRegex.genSubstrings(data);
}
} else {
valueRegex.genSubstrings(data.slice(highestLowIndex, lowestHighIndex));
}

valueRegex.applySimilarity();
Expand Down

0 comments on commit 62244ff

Please sign in to comment.