-
-
Notifications
You must be signed in to change notification settings - Fork 29
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
68cd415
commit 8043856
Showing
13 changed files
with
230 additions
and
19 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,5 +7,6 @@ | |
"undef": true, | ||
"unused": true, | ||
"trailing": true, | ||
"asi": true | ||
"asi": true, | ||
"loopfunc": true | ||
} |
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
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,10 @@ | ||
const Classification = require('../classification/Classification') | ||
|
||
class IntersectionClassification extends Classification { | ||
constructor (confidence, meta) { | ||
super(confidence, meta) | ||
this.label = 'intersection' | ||
} | ||
} | ||
|
||
module.exports = IntersectionClassification |
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,11 @@ | ||
const Classification = require('./Classification') | ||
|
||
class MultiStreetClassification extends Classification { | ||
constructor (confidence, meta) { | ||
super(confidence, meta) | ||
this.public = false | ||
this.label = 'multistreet' | ||
} | ||
} | ||
|
||
module.exports = MultiStreetClassification |
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,27 @@ | ||
const PermutationClassifier = require('./super/PermutationClassifier') | ||
const IntersectionClassification = require('../classification/IntersectionClassification') | ||
const libpostal = require('../resources/libpostal/libpostal') | ||
|
||
// dictionaries sourced from the libpostal project | ||
// see: https://github.com/openvenues/libpostal | ||
|
||
const languages = libpostal.languages | ||
|
||
class IntersectionClassifier extends PermutationClassifier { | ||
setup () { | ||
this.index = {} | ||
libpostal.load(this.index, languages, 'cross_streets.txt') | ||
} | ||
|
||
each (span) { | ||
// skip spans which contain numbers | ||
if (span.contains.numerals) { return } | ||
|
||
// use an inverted index for full token matching as it's O(1) | ||
if (this.index.hasOwnProperty(span.norm)) { | ||
span.classify(new IntersectionClassification(1)) | ||
} | ||
} | ||
} | ||
|
||
module.exports = IntersectionClassifier |
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,79 @@ | ||
const SectionClassifier = require('./super/SectionClassifier') | ||
const MultiStreetClassification = require('../classification/MultiStreetClassification') | ||
|
||
class MultiStreetClassifier extends SectionClassifier { | ||
each (section) { | ||
let children = { | ||
all: [], | ||
street: [], | ||
intersection: [] | ||
} | ||
let lastOffset = 0 | ||
|
||
// find a span of multiple children in this section | ||
// who are either classified as street or intersection /or | ||
// are part of a permutation classes as such | ||
section.child.forEach((c, o) => { | ||
if ( | ||
c.classifications.hasOwnProperty('StreetClassification') || | ||
section.permutation.some(p => { | ||
return ( | ||
p.classifications.hasOwnProperty('StreetClassification') && | ||
p.child.some(pc => pc === c) | ||
) | ||
}) | ||
) { | ||
if (children.street.length === 0 || o === lastOffset + 1) { | ||
children.all.push(c) | ||
children.street.push(c) | ||
lastOffset = o | ||
} | ||
} else if ( | ||
c.classifications.hasOwnProperty('IntersectionClassification') || | ||
section.permutation.some(p => { | ||
return ( | ||
p.classifications.hasOwnProperty('IntersectionClassification') && | ||
p.child.some(pc => pc === c) | ||
) | ||
}) | ||
) { | ||
if (children.intersection.length === 0 || o === lastOffset + 1) { | ||
children.all.push(c) | ||
children.intersection.push(c) | ||
lastOffset = o | ||
} | ||
} | ||
}) | ||
|
||
// validate the child arrays | ||
if (( | ||
children.all.length < 3 || | ||
children.intersection.length < 1 || | ||
children.street.length < 2 | ||
)) { | ||
return | ||
} | ||
|
||
// @todo: ensure that at least one IntersectionClassification exists | ||
|
||
let matches = section.permutation.map(p => { | ||
if ( | ||
// every child must be part of the set above | ||
p.child.every(pc => children.all.includes(pc)) | ||
) { | ||
return p | ||
} | ||
}) | ||
|
||
if (matches.length) { | ||
matches.sort((a, b) => { | ||
return (a.end - a.start) > (b.end - b.start) | ||
}) | ||
|
||
// only classify the longest match | ||
matches[0].classify(new MultiStreetClassification(1.0)) | ||
} | ||
} | ||
} | ||
|
||
module.exports = MultiStreetClassifier |
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,38 @@ | ||
const HashMapSolver = require('./super/HashMapSolver') | ||
|
||
class MultiStreetSolver extends HashMapSolver { | ||
solve (tokenizer) { | ||
let map = this.generateHashMap(tokenizer, true) | ||
|
||
// sanity checking | ||
if (!map.hasOwnProperty('multistreet')) { return } | ||
if (!map.hasOwnProperty('street') || map.street.length < 2) { return } | ||
|
||
let multi = map.multistreet[0] | ||
let candidates = map.street.slice(0) | ||
|
||
// add the second street to existing solutions | ||
for (let s = 0; s < tokenizer.solution.length; s++) { | ||
let sol = tokenizer.solution[s].slice(0) // make a copy | ||
let success = false | ||
|
||
for (let i = 0; i < candidates.length; i++) { | ||
let s = candidates[i] | ||
if (( | ||
s.span.intersects(multi.span) && | ||
!sol.some(ss => ss.span.intersects(s.span)) | ||
)) { | ||
sol.push(s) | ||
success = true | ||
break | ||
} | ||
} | ||
if (success) { | ||
tokenizer.solution.push(sol) | ||
candidates = candidates.filter(c => c === sol[sol.length - 1]) | ||
} | ||
} | ||
} | ||
} | ||
|
||
module.exports = MultiStreetSolver |
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
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