Skip to content

Commit

Permalink
feat(solver): add MustNotPreceedFilter
Browse files Browse the repository at this point in the history
  • Loading branch information
missinglink committed May 3, 2019
1 parent 97f58c3 commit 6b8d8d9
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 5 deletions.
12 changes: 12 additions & 0 deletions parser/AddressParser.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ const ExclusiveCartesianSolver = require('../solver/ExclusiveCartesianSolver')
const LeadingAreaDeclassifier = require('../solver/LeadingAreaDeclassifier')
const MultiStreetSolver = require('../solver/MultiStreetSolver')
const TokenDistanceFilter = require('../solver/TokenDistanceFilter')
const MustNotPreceedFilter = require('../solver/MustNotPreceedFilter')
const SubsetFilter = require('../solver/SubsetFilter')

class AddressParser extends Parser {
Expand Down Expand Up @@ -61,6 +62,17 @@ class AddressParser extends Parser {
new LeadingAreaDeclassifier(),
new MultiStreetSolver(),
new TokenDistanceFilter(),
new MustNotPreceedFilter('PostcodeClassification', 'HouseNumberClassification'),
new MustNotPreceedFilter('PostcodeClassification', 'StreetClassification'),
new MustNotPreceedFilter('LocalityClassification', 'HouseNumberClassification'),
new MustNotPreceedFilter('LocalityClassification', 'StreetClassification'),
new MustNotPreceedFilter('RegionClassification', 'HouseNumberClassification'),
new MustNotPreceedFilter('RegionClassification', 'StreetClassification'),
new MustNotPreceedFilter('CountryClassification', 'RegionClassification'),
new MustNotPreceedFilter('CountryClassification', 'LocalityClassification'),
new MustNotPreceedFilter('CountryClassification', 'PostcodeClassification'),
new MustNotPreceedFilter('CountryClassification', 'StreetClassification'),
new MustNotPreceedFilter('CountryClassification', 'HouseNumberClassification'),
new SubsetFilter()
]
)
Expand Down
28 changes: 28 additions & 0 deletions solver/MustNotPreceedFilter.js
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
46 changes: 46 additions & 0 deletions solver/MustNotPreceedFilter.test.js
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)
}
}
5 changes: 0 additions & 5 deletions test/addressit.usa.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,11 +64,6 @@ const testcase = (test, common) => {
{ locality: 'Atlanta' }, { region: 'Georgia' },
{ postcode: '31035' }
],
[
{ housenumber: '754' }, { street: 'Pharr Rd' },
{ locality: 'Atlanta' }, { country: 'Georgia' },
{ postcode: '31035' }
],
[
{ housenumber: '754' }, { street: 'Pharr Rd' },
{ locality: 'Georgia' }, { postcode: '31035' }
Expand Down

0 comments on commit 6b8d8d9

Please sign in to comment.