Skip to content

Commit

Permalink
feat(intersections): fix bug (#50)
Browse files Browse the repository at this point in the history
  • Loading branch information
missinglink authored Jun 12, 2019
1 parent 5db7432 commit 7ecfc65
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 22 deletions.
1 change: 1 addition & 0 deletions classifier/CompositeClassifier.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ class CompositeClassifier extends SectionClassifier {
let phrases = section.graph.findAll('phrase')

// sort phrases so shorter phrases are matched first
// note: this mutates the original array
phrases.sort((a, b) => a.norm.length - b.norm.length)

this.schemes.forEach(s => {
Expand Down
4 changes: 2 additions & 2 deletions classifier/scheme/intersection.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ module.exports = [
{
is: ['AlphaClassification', 'NumericClassification', 'OrdinalClassification'],
not: ['IntersectionClassification', 'StreetSuffixClassification'],
confidence: 0.51,
confidence: 0.81,
Class: StreetClassification
},
{
Expand All @@ -43,7 +43,7 @@ module.exports = [
{
is: ['AlphaClassification', 'NumericClassification', 'OrdinalClassification'],
not: ['IntersectionClassification'],
confidence: 0.52,
confidence: 0.82,
Class: StreetClassification
}
]
Expand Down
41 changes: 21 additions & 20 deletions solver/MultiStreetSolver.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
const HashMapSolver = require('./super/HashMapSolver')
const Solution = require('./Solution')

/**
* If a 'multistreet' classification was detected then
* add a new solution which covers all streets included.
*/

class MultiStreetSolver extends HashMapSolver {
solve (tokenizer) {
Expand All @@ -8,36 +14,31 @@ class MultiStreetSolver extends HashMapSolver {
if (!map.hasOwnProperty('multistreet')) { return }
if (!map.hasOwnProperty('street') || map.street.pair.length < 2) { return }

let multi = map.multistreet.pair[0]
let candidates = map.street.copy()

// add the second street to existing solutions
for (let s = 0; s < tokenizer.solution.length; s++) {
let sol = tokenizer.solution[s].copy() // make a copy

// remove any pairs which are more granular than street (not applicable for intersections)
sol.pair = sol.pair.filter(p => p.classification.constructor.name !== 'HouseNumberClassification')
sol.pair = sol.pair.filter(p => p.classification.constructor.name !== 'PlaceClassification')
// sort streets from longest to shortest string length.
// this favours longer street names over their shorter substrings.
let streets = map.street.copy()
streets.pair = streets.pair.sort((a, b) => b.span.norm.length - a.span.norm.length)

let success = false

for (let i = 0; i < candidates.pair.length; i++) {
let s = candidates.pair[i]
map.multistreet.pair.forEach(multi => {
let sol = new Solution()

// find all street tokens which intersect the 'multistreet' span
// and also do not overlap an existing pair in this solution.
streets.pair.forEach(s => {
if ((
s.span.intersects(multi.span) &&
(s.classification.constructor.name === 'StreetClassification') &&
!sol.pair.some(sp => sp.span.intersects(s.span))
)) {
sol.pair.push(s)
success = true
break
}
}
if (success) {
})

// if more than one street was detected then add this as a new solution
if (sol.pair.length > 1) {
tokenizer.solution.push(sol)
candidates.pair = candidates.pair.filter(c => c === sol[sol.length - 1])
}
}
})
}
}

Expand Down
8 changes: 8 additions & 0 deletions test/intersection.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,14 @@ const testcase = (test, common) => {
{ street: 'SW 6th' }, { street: 'Pine' }
], true)

assert('9th and Lambert', [
{ street: '9th' }, { street: 'Lambert' }
], true)

assert('filbert & 32nd', [
{ street: 'filbert' }, { street: '32nd' }
], true)

// Should not detect this as an intersection
// assert('University of Hawaii at Hilo', [
// { street: 'SW 6th' }, { street: 'Pine' }
Expand Down

0 comments on commit 7ecfc65

Please sign in to comment.