-
-
Notifications
You must be signed in to change notification settings - Fork 28
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(solver): add MustNotPreceedFilter
- Loading branch information
1 parent
97f58c3
commit 6b8d8d9
Showing
4 changed files
with
86 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
// enforce that a object classification cannot preceed subject classification | ||
// @todo: handle case of multiple object classifications matching | ||
|
||
class MustNotPreceedFilter { | ||
constructor (objectClassification, subjectClassification) { | ||
this.classification = { | ||
object: objectClassification, | ||
subject: subjectClassification | ||
} | ||
} | ||
solve (tokenizer) { | ||
tokenizer.solution.forEach(s => { | ||
let object = s.pair.filter(p => p.classification.constructor.name === this.classification.object) | ||
let subject = s.pair.filter(p => p.classification.constructor.name === this.classification.subject) | ||
|
||
// solution contains both object & subject classifications | ||
if (object.length > 0 && subject.length > 0) { | ||
// the object comes before the subject(s) | ||
if (subject.some(p => p.span.start > object[0].span.end)) { | ||
// remove the object classification from this solution | ||
s.pair = s.pair.filter(p => p.classification.constructor.name !== this.classification.object) | ||
} | ||
} | ||
}) | ||
} | ||
} | ||
|
||
module.exports = MustNotPreceedFilter |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
const Tokenizer = require('../tokenization/Tokenizer') | ||
const Span = require('../tokenization/Span') | ||
const PostcodeClassification = require('../classification/PostcodeClassification') | ||
const StreetClassification = require('../classification/StreetClassification') | ||
const Solution = require('./Solution') | ||
const SolutionPair = require('./SolutionPair') | ||
const MustNotPreceedFilter = require('./MustNotPreceedFilter') | ||
|
||
module.exports.tests = {} | ||
|
||
module.exports.tests.postcode_preceeds_street = (test) => { | ||
test('postcode_preceeds_street: remove postcode', (t) => { | ||
let tok = new Tokenizer() | ||
|
||
let s1 = new Span('A') | ||
s1.start = 0 | ||
s1.end = 1 | ||
|
||
let s2 = new Span('B') | ||
s2.start = 3 | ||
s2.end = 4 | ||
|
||
let sp1 = new SolutionPair(s1, new PostcodeClassification(1.0)) | ||
let sp2 = new SolutionPair(s2, new StreetClassification(1.0)) | ||
|
||
tok.solution = [ new Solution([sp1, sp2]) ] | ||
|
||
let c = new MustNotPreceedFilter('PostcodeClassification', 'StreetClassification') | ||
c.solve(tok) | ||
|
||
t.deepEquals(tok.solution.length, 1) | ||
t.deepEquals(tok.solution[0].pair.length, 1) | ||
t.deepEquals(tok.solution[0].pair[0], sp2) | ||
t.end() | ||
}) | ||
} | ||
|
||
module.exports.all = (tape, common) => { | ||
function test (name, testFunction) { | ||
return tape(`MustNotPreceedFilter: ${name}`, testFunction) | ||
} | ||
|
||
for (var testCase in module.exports.tests) { | ||
module.exports.tests[testCase](test, common) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters