Skip to content

Commit

Permalink
feat: autocomplete jitter fixes (#44)
Browse files Browse the repository at this point in the history
* fix: remove unused scheme

* fix: improve street_name scheme

* feat(CompositeClassifier): improvements to composite classifier, see PR notes

* feat(test): add autocomplete jitter test cases
  • Loading branch information
missinglink authored Jun 6, 2019
1 parent 14a1efe commit 0ed71c5
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 18 deletions.
16 changes: 15 additions & 1 deletion classifier/CompositeClassifier.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,11 @@ class CompositeClassifier extends SectionClassifier {
}

each (section) {
let phrases = section.graph.findAll('phrase')

// sort phrases so shorter phrases are matched first
phrases.sort((a, b) => a.norm.length - b.norm.length)

this.schemes.forEach(s => {
// invalid scheme
if (!Array.isArray(s.scheme)) { return }
Expand All @@ -57,7 +62,6 @@ class CompositeClassifier extends SectionClassifier {
let candidates = []

// compute candidate lists
let phrases = section.graph.findAll('phrase')
candidates = s.scheme.map(s => phrases.filter(this.match.bind(null, s)))

// no candidates were found for one or more schemes
Expand All @@ -79,6 +83,16 @@ class CompositeClassifier extends SectionClassifier {
} else if (prev && curr.graph.findOne('child:first').graph.findOne('prev') !== prev.graph.findOne('child:last')) {
return false
}

// avoid adding tokens to the front of a street classification
// that begins with a street prefix.
// eg. 'A + Ave B' (ave is both a valid prefix & suffix)
if (next && next.classifications.hasOwnProperty('StreetClassification')) {
let firstChild = next.graph.findOne('child')
if (firstChild && firstChild.classifications.hasOwnProperty('StreetPrefixClassification')) {
return false
}
}
}
return true
})
Expand Down
15 changes: 0 additions & 15 deletions classifier/scheme/street.js
Original file line number Diff line number Diff line change
Expand Up @@ -303,21 +303,6 @@ module.exports = [
}
]
},
{
// West Main Street
confidence: 0.84,
Class: StreetClassification,
scheme: [
{
is: ['DirectionalClassification'],
not: ['StreetClassification', 'IntersectionClassification']
},
{
is: ['StreetClassification'],
not: ['DirectionalClassification']
}
]
},
{
// Main Street West
confidence: 0.88,
Expand Down
6 changes: 4 additions & 2 deletions classifier/scheme/street_name.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ module.exports = [
Class: StreetNameClassification,
scheme: [
{
is: ['StopWordClassification']
is: ['StopWordClassification'],
not: ['DirectionalClassification']
},
{
is: ['AlphaClassification', 'PersonClassification'],
Expand All @@ -25,7 +26,8 @@ module.exports = [
not: ['StreetClassification', 'IntersectionClassification', 'StopWordClassification', 'StreetPrefixClassification']
},
{
is: ['StopWordClassification']
is: ['StopWordClassification'],
not: ['DirectionalClassification']
},
{
is: ['AlphaClassification', 'PersonClassification'],
Expand Down
18 changes: 18 additions & 0 deletions test/address.usa.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,24 @@ const testcase = (test, common) => {

// postcode not allowed in first position otherwise
assert('90210 Foo', [])

// autocomplete street name jitter
// note: we are only testing the street name stays the same throughout
assert('N FISKE AVE', [{ street: 'N FISKE AVE' }], true)
assert('N FISKE AVE P', [{ street: 'N FISKE AVE' }], true)
assert('N FISKE AVE Po', [{ street: 'N FISKE AVE' }, { region: 'Po' }], true)
assert('N FISKE AVE Por', [{ street: 'N FISKE AVE' }, { region: 'Por' }], true)
assert('N FISKE AVE Port', [{ street: 'N FISKE AVE' }, { locality: 'Port' }], true)
assert('N FISKE AVE Portl', [{ street: 'N FISKE AVE' }], true)
assert('N FISKE AVE Portla', [{ street: 'N FISKE AVE' }], true)
assert('N FISKE AVE Portlan', [{ street: 'N FISKE AVE' }], true)
assert('N FISKE AVE Portland', [{ street: 'N FISKE AVE' }, { locality: 'Portland' }], true)
assert('N DWIGHT AVE Portland O', [{ street: 'N DWIGHT AVE' }, { locality: 'Portland' }], true)
assert('N DWIGHT AVE Portland Or', [{ street: 'N DWIGHT AVE' }, { locality: 'Portland' }, { region: 'Or' }], true)
assert('N DWIGHT AVE Portland Ore', [{ street: 'N DWIGHT AVE' }, { locality: 'Portland' }], true)
assert('N DWIGHT AVE Portland Oreg', [{ street: 'N DWIGHT AVE' }, { locality: 'Portland' }], true)
assert('N DWIGHT AVE Portland Orego', [{ street: 'N DWIGHT AVE' }, { locality: 'Portland' }], true)
assert('N DWIGHT AVE Portland Oregon', [{ street: 'N DWIGHT AVE' }, { locality: 'Portland' }, { region: 'Oregon' }], true)
}

module.exports.all = (tape, common) => {
Expand Down

0 comments on commit 0ed71c5

Please sign in to comment.