Skip to content

Commit

Permalink
Merge pull request #1314 from AndersDJohnson/patch-1
Browse files Browse the repository at this point in the history
fix(ner): deserialize regex from JSON
  • Loading branch information
ericzon committed May 25, 2023
2 parents aa5735d + c12f55f commit 71e29e2
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 5 deletions.
16 changes: 11 additions & 5 deletions packages/ner/src/ner.js
Original file line number Diff line number Diff line change
Expand Up @@ -546,11 +546,17 @@ class Ner extends Clonable {
const entityKeys = Object.keys(json.rules[rKey]);

entityKeys.forEach((eKey) => {
if (json.rules[rKey][eKey].type === 'regex') {
json.rules[rKey][eKey].rules = json.rules[rKey][eKey].rules.map(
(rule) => Ner.str2regex(rule)
);
}
json.rules[rKey][eKey].rules =
json.rules[rKey][eKey].type === 'regex'
? json.rules[rKey][eKey].rules.map((rule) => Ner.str2regex(rule))
: json.rules[rKey][eKey].rules.map((rule) =>
typeof rule.regex === 'string'
? {
...rule,
regex: Ner.str2regex(rule.regex),
}
: rule
);
});
});

Expand Down
25 changes: 25 additions & 0 deletions packages/ner/test/ner.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -485,4 +485,29 @@ describe('NER', () => {
]);
});
});
describe('To & from JSON', () => {
test('It deserializes regular expressions', () => {
const instance = new Ner({ container });
instance.addBetweenCondition('en', 'test', 'from', 'to');
const json = JSON.stringify(instance.toJSON());
const instance2 = new Ner({ container });
instance2.fromJSON(JSON.parse(json));
const actual = instance2.getRules('en');
expect(actual).toEqual([
{
name: 'test',
rules: [
{
type: 'between',
leftWords: ['from'],
rightWords: ['to'],
regex: /(?<= from )(.*)(?= to )/gi,
options: {},
},
],
type: 'trim',
},
]);
});
});
});

0 comments on commit 71e29e2

Please sign in to comment.