-
-
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.
feat(classifier): Add personal title classifier (fra addresses)
- Loading branch information
Showing
9 changed files
with
205 additions
and
6 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 |
---|---|---|
@@ -0,0 +1,10 @@ | ||
const Classification = require('../classification/Classification') | ||
|
||
class PersonalTitleClassification extends Classification { | ||
constructor (confidence, meta) { | ||
super(confidence, meta) | ||
this.label = 'personal_title' | ||
} | ||
} | ||
|
||
module.exports = PersonalTitleClassification |
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,24 @@ | ||
const Classification = require('./PersonalTitleClassification') | ||
|
||
module.exports.tests = {} | ||
|
||
module.exports.tests.constructor = (test) => { | ||
test('constructor', (t) => { | ||
let c = new Classification() | ||
t.false(c.public) | ||
t.equals(c.label, 'personal_title') | ||
t.equals(c.confidence, 1.0) | ||
t.deepEqual(c.meta, {}) | ||
t.end() | ||
}) | ||
} | ||
|
||
module.exports.all = (tape, common) => { | ||
function test (name, testFunction) { | ||
return tape(`PersonalTitleClassification: ${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
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,27 @@ | ||
const PhraseClassifier = require('./super/PhraseClassifier') | ||
const PersonalTitleClassification = require('../classification/PersonalTitleClassification') | ||
const libpostal = require('../resources/libpostal/libpostal') | ||
|
||
// dictionaries sourced from the libpostal project | ||
// see: https://github.com/openvenues/libpostal | ||
|
||
class PersonalTitleClassifier extends PhraseClassifier { | ||
setup () { | ||
this.index = {} | ||
libpostal.load(this.index, libpostal.languages, 'personal_titles.txt', { | ||
replace: [/\.$/, ''] | ||
}) | ||
} | ||
|
||
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.replace(/\.$/, ''))) { | ||
span.classify(new PersonalTitleClassification(1)) | ||
} | ||
} | ||
} | ||
|
||
module.exports = PersonalTitleClassifier |
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,50 @@ | ||
const PersonalTitleClassifier = require('./PersonalTitleClassifier') | ||
const PersonalTitleClassification = require('../classification/PersonalTitleClassification') | ||
const Span = require('../tokenization/Span') | ||
|
||
module.exports.tests = {} | ||
|
||
function classify (body) { | ||
let c = new PersonalTitleClassifier() | ||
let s = new Span(body) | ||
c.each(s, null, 1) | ||
return s | ||
} | ||
|
||
module.exports.tests.contains_numerals = (test) => { | ||
test('contains numerals: honours contains.numerals boolean', (t) => { | ||
let c = new PersonalTitleClassifier() | ||
let s = new Span('example') | ||
s.contains.numerals = true | ||
c.each(s, null, 1) | ||
t.deepEqual(s.classifications, {}) | ||
t.end() | ||
}) | ||
} | ||
|
||
module.exports.tests.classify = (test) => { | ||
let valid = [ | ||
'Général', 'General', 'gal', | ||
'Saint', 'st', 'cdt' | ||
] | ||
|
||
valid.forEach(token => { | ||
test(`classify: ${token}`, (t) => { | ||
let s = classify(token) | ||
t.deepEqual(s.classifications, { | ||
PersonalTitleClassification: new PersonalTitleClassification(1.0) | ||
}) | ||
t.end() | ||
}) | ||
}) | ||
} | ||
|
||
module.exports.all = (tape, common) => { | ||
function test (name, testFunction) { | ||
return tape(`PersonalTitleClassifier: ${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
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