Skip to content

Commit

Permalink
chore(common/models/templates): Merge branch 'feat/common/models/dire…
Browse files Browse the repository at this point in the history
…ct-lexicon-traversal' into change/common/models/templates/trie-results-through-traversal
  • Loading branch information
jahorton committed Jul 8, 2024
2 parents 3423c63 + dc7c1f1 commit 9e2f0b1
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 16 deletions.
13 changes: 8 additions & 5 deletions common/models/templates/src/trie-model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ class Traversal implements LexiconTraversal {

// Split into individual code units.
let steps = char.split('');
let traversal: ReturnType<Traversal["_child"]> = this;
let traversal: Traversal | undefined = this;

while(steps.length > 0 && traversal) {
const step: string = steps.shift()!;
Expand Down Expand Up @@ -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') {
Expand Down Expand Up @@ -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
}
}

Expand Down Expand Up @@ -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;
/**
Expand Down
15 changes: 8 additions & 7 deletions common/models/templates/test/test-trie-traversal.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'));
Expand All @@ -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);

Expand Down Expand Up @@ -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);
Expand All @@ -153,15 +154,15 @@ 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') {
let traversalInner2 = tChild.traversal();
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') {
Expand Down
7 changes: 3 additions & 4 deletions common/models/types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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`.
*
Expand Down

0 comments on commit 9e2f0b1

Please sign in to comment.