diff --git a/common/models/templates/src/trie-model.ts b/common/models/templates/src/trie-model.ts index cbbe9181623..2cac0429c09 100644 --- a/common/models/templates/src/trie-model.ts +++ b/common/models/templates/src/trie-model.ts @@ -109,7 +109,7 @@ class Traversal implements LexiconTraversal { // Split into individual code units. let steps = char.split(''); - let traversal: ReturnType = this; + let traversal: Traversal | undefined = this; while(steps.length > 0 && traversal) { const step: string = steps.shift()!; @@ -148,6 +148,10 @@ class Traversal implements LexiconTraversal { *children(): Generator<{char: USVString, traversal: () => LexiconTraversal}> { let root = this.root; + + // We refer to the field multiple times in this method, and it doesn't change. + // This also assists minification a bit, since we can't minify when re-accessing + // through `this.`. const totalWeight = this.totalWeight; if(root.type == 'internal') { @@ -220,11 +224,10 @@ class Traversal implements LexiconTraversal { } get entries() { - const totalWeight = this.totalWeight; - const entryMapper = function(value: Entry) { + const entryMapper = (value: Entry) => { return { text: value.content, - p: value.weight / totalWeight + p: value.weight / this.totalWeight } } @@ -425,7 +428,7 @@ interface Entry { * Wrapper class for the trie and its nodes. */ class Trie { - private root: Node; + public readonly root: Node; /** The total weight of the entire trie. */ readonly totalWeight: number; /** diff --git a/common/models/templates/test/test-trie-traversal.js b/common/models/templates/test/test-trie-traversal.js index aed35d654b9..09237ee9a0e 100644 --- a/common/models/templates/test/test-trie-traversal.js +++ b/common/models/templates/test/test-trie-traversal.js @@ -13,6 +13,12 @@ var smpForUnicode = function(code){ return String.fromCharCode(H, L); } +// Prob: entry weight / total weight +// "the" is the highest-weighted word in the fixture. +const PROB_OF_THE = 1000 / 500500; +const PROB_OF_TRUE = 607 / 500500; +const PROB_OF_TROUBLE = 267 / 500500; + describe('Trie traversal abstractions', function() { it('root-level iteration over child nodes', function() { var model = new TrieModel(jsonFixture('tries/english-1000')); @@ -39,10 +45,6 @@ describe('Trie traversal abstractions', function() { it('traversal with simple internal nodes', function() { var model = new TrieModel(jsonFixture('tries/english-1000')); - // Prob: entry weight / total weight - // "the" is the highest-weighted word in the fixture. - const PROB_OF_THE = 1000 / 500500; - let rootTraversal = model.traverseFromRoot(); assert.isDefined(rootTraversal); @@ -139,7 +141,6 @@ describe('Trie traversal abstractions', function() { it('traversal over compact leaf node', function() { var model = new TrieModel(jsonFixture('tries/english-1000')); - const PROB_OF_TROUBLE = 267 / 500500; let rootTraversal = model.traverseFromRoot(); assert.isDefined(rootTraversal); @@ -153,7 +154,7 @@ describe('Trie traversal abstractions', function() { assert.isDefined(traversalInner1); assert.isArray(child.traversal().entries); assert.isEmpty(child.traversal().entries); - assert.equal(traversalInner1.p, 1000 / 500500 /* prob of 'the' */); + assert.equal(traversalInner1.p, PROB_OF_THE); for(let tChild of traversalInner1.children()) { if(tChild.char == 'r') { @@ -161,7 +162,7 @@ describe('Trie traversal abstractions', function() { assert.isDefined(traversalInner2); assert.isArray(tChild.traversal().entries); assert.isEmpty(tChild.traversal().entries); - assert.equal(traversalInner2.p, 607 / 500500 /* prob of 'true', the best 'tr-' entry */); + assert.equal(traversalInner2.p, PROB_OF_TRUE); for(let rChild of traversalInner2.children()) { if(rChild.char == 'o') { diff --git a/common/models/types/index.d.ts b/common/models/types/index.d.ts index 2fc1c92cb33..a75e6a046f9 100644 --- a/common/models/types/index.d.ts +++ b/common/models/types/index.d.ts @@ -68,10 +68,9 @@ declare interface LexiconTraversal { children(): Generator<{char: USVString, traversal: () => LexiconTraversal}>; /** - * Allows direct access to the traversal state that results when appending a - * `char` representing one or more individual UTF-16 codepoints to the - * current traversal state's prefix. This bypasses the need to iterate - * among all legal child Traversals. + * Allows direct access to the traversal state that results when appending one + * or more codepoints encoded in UTF-16 to the current traversal state's prefix. + * This allows bypassing iteration among all legal child Traversals. * * If such a traversal state is not supported, returns `undefined`. *