-
-
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(final_token_classifications): add final token classifications, i…
…mprove autocomplete parsing (#19)
- Loading branch information
1 parent
1522b2a
commit f590df2
Showing
9 changed files
with
190 additions
and
2 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 FinalTokenClassification extends Classification { | ||
constructor (confidence, meta) { | ||
super(confidence, meta) | ||
this.label = 'final_token' | ||
} | ||
} | ||
|
||
module.exports = FinalTokenClassification |
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('./FinalTokenClassification') | ||
|
||
module.exports.tests = {} | ||
|
||
module.exports.tests.constructor = (test) => { | ||
test('constructor', (t) => { | ||
let c = new Classification() | ||
t.false(c.public) | ||
t.equals(c.label, 'final_token') | ||
t.equals(c.confidence, 1.0) | ||
t.deepEqual(c.meta, {}) | ||
t.end() | ||
}) | ||
} | ||
|
||
module.exports.all = (tape, common) => { | ||
function test (name, testFunction) { | ||
return tape(`FinalTokenClassification: ${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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
const Classification = require('../classification/Classification') | ||
|
||
class FinalTokenSingleCharacterClassification extends Classification { | ||
constructor (confidence, meta) { | ||
super(confidence, meta) | ||
this.label = 'final_token_single_character' | ||
} | ||
} | ||
|
||
module.exports = FinalTokenSingleCharacterClassification |
24 changes: 24 additions & 0 deletions
24
classification/FinalTokenSingleCharacterClassification.test.js
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('./FinalTokenSingleCharacterClassification') | ||
|
||
module.exports.tests = {} | ||
|
||
module.exports.tests.constructor = (test) => { | ||
test('constructor', (t) => { | ||
let c = new Classification() | ||
t.false(c.public) | ||
t.equals(c.label, 'final_token_single_character') | ||
t.equals(c.confidence, 1.0) | ||
t.deepEqual(c.meta, {}) | ||
t.end() | ||
}) | ||
} | ||
|
||
module.exports.all = (tape, common) => { | ||
function test (name, testFunction) { | ||
return tape(`FinalTokenSingleCharacterClassification: ${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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
const BaseClassifier = require('./super/BaseClassifier') | ||
const FinalTokenClassification = require('../classification/FinalTokenClassification') | ||
const FinalTokenSingleCharacterClassification = require('../classification/FinalTokenSingleCharacterClassification') | ||
|
||
// classify the final token with 'FinalTokenClassification' | ||
// and also a 'FinalTokenSingleCharacterClassification' if its only | ||
// a single character in length. | ||
// note: this can be useful for improving autocomplete. | ||
|
||
class FinalTokenClassifier extends BaseClassifier { | ||
classify (tokenizer) { | ||
if (tokenizer.section.length < 1) { return } | ||
let lastSection = tokenizer.section[tokenizer.section.length - 1] | ||
let children = lastSection.graph.findAll('child') | ||
if (children.length < 1) { return } | ||
let lastChild = children[children.length - 1] | ||
lastChild.classify(new FinalTokenClassification(1.0)) | ||
if (lastChild.norm.length === 1) { | ||
lastChild.classify(new FinalTokenSingleCharacterClassification(1.0)) | ||
} | ||
} | ||
} | ||
|
||
module.exports = FinalTokenClassifier |
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,82 @@ | ||
const FinalTokenClassifier = require('./FinalTokenClassifier') | ||
const Tokenizer = require('../tokenization/Tokenizer') | ||
|
||
module.exports.tests = {} | ||
|
||
function classify (body) { | ||
let c = new FinalTokenClassifier() | ||
let t = new Tokenizer(body) | ||
c.classify(t) | ||
|
||
// generate an array containing all the spans | ||
// with a final token classification | ||
let classifications = { | ||
FinalTokenClassification: [], | ||
FinalTokenSingleCharacterClassification: [] | ||
} | ||
t.section.forEach(s => { | ||
s.graph.findAll('child').forEach(c => { | ||
if (c.classifications.hasOwnProperty('FinalTokenClassification')) { | ||
classifications.FinalTokenClassification.push(c) | ||
} | ||
if (c.classifications.hasOwnProperty('FinalTokenSingleCharacterClassification')) { | ||
classifications.FinalTokenSingleCharacterClassification.push(c) | ||
} | ||
}) | ||
}) | ||
return classifications | ||
} | ||
|
||
module.exports.tests.classify = (test) => { | ||
test('classify: empty string', (t) => { | ||
let c = classify('') | ||
t.equals(c.FinalTokenClassification.length, 0) | ||
t.equals(c.FinalTokenSingleCharacterClassification.length, 0) | ||
t.end() | ||
}) | ||
|
||
test('classify: A', (t) => { | ||
let c = classify('A') | ||
t.equals(c.FinalTokenClassification.length, 1) | ||
t.equals(c.FinalTokenClassification[0].body, 'A') | ||
t.equals(c.FinalTokenSingleCharacterClassification.length, 1) | ||
t.equals(c.FinalTokenSingleCharacterClassification[0].body, 'A') | ||
t.end() | ||
}) | ||
|
||
test('classify: A B', (t) => { | ||
let c = classify('A B') | ||
t.equals(c.FinalTokenClassification.length, 1) | ||
t.equals(c.FinalTokenClassification[0].body, 'B') | ||
t.equals(c.FinalTokenSingleCharacterClassification.length, 1) | ||
t.equals(c.FinalTokenSingleCharacterClassification[0].body, 'B') | ||
t.end() | ||
}) | ||
|
||
test('classify: A BC', (t) => { | ||
let c = classify('A BC') | ||
t.equals(c.FinalTokenClassification.length, 1) | ||
t.equals(c.FinalTokenClassification[0].body, 'BC') | ||
t.equals(c.FinalTokenSingleCharacterClassification.length, 0) | ||
t.end() | ||
}) | ||
|
||
test('classify: A BC, D', (t) => { | ||
let c = classify('A BC, D') | ||
t.equals(c.FinalTokenClassification.length, 1) | ||
t.equals(c.FinalTokenClassification[0].body, 'D') | ||
t.equals(c.FinalTokenSingleCharacterClassification.length, 1) | ||
t.equals(c.FinalTokenSingleCharacterClassification[0].body, 'D') | ||
t.end() | ||
}) | ||
} | ||
|
||
module.exports.all = (tape, common) => { | ||
function test (name, testFunction) { | ||
return tape(`FinalTokenClassifier: ${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