-
-
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(must_not_follow_filter): do not allow locality to follow region (#…
…23)
- Loading branch information
1 parent
93cd613
commit 8780796
Showing
4 changed files
with
87 additions
and
1 deletion.
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,31 @@ | ||
// enforce that a object classification cannot follow subject classification | ||
// @todo: handle case of multiple object classifications matching | ||
|
||
class MustNotFollowFilter { | ||
constructor (objectClassification, subjectClassification) { | ||
this.classification = { | ||
object: objectClassification, | ||
subject: subjectClassification | ||
} | ||
} | ||
solve (tokenizer) { | ||
tokenizer.solution = tokenizer.solution.filter(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) | ||
return s.pair.length > 0 | ||
} | ||
} | ||
|
||
return true | ||
}) | ||
} | ||
} | ||
|
||
module.exports = MustNotFollowFilter |
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 MustNotFollowFilter = require('./MustNotFollowFilter') | ||
|
||
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 MustNotFollowFilter('StreetClassification', 'PostcodeClassification') | ||
c.solve(tok) | ||
|
||
t.deepEquals(tok.solution.length, 1) | ||
t.deepEquals(tok.solution[0].pair.length, 1) | ||
t.deepEquals(tok.solution[0].pair[0], sp1) | ||
t.end() | ||
}) | ||
} | ||
|
||
module.exports.all = (tape, common) => { | ||
function test (name, testFunction) { | ||
return tape(`MustNotFollowFilter: ${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