-
-
Notifications
You must be signed in to change notification settings - Fork 111
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
refactor(common/models): move core trie-compilation code into common/models/templates 📖 #12083
Merged
jahorton
merged 6 commits into
epic/user-dict
from
refactor/common/models/trie-construction
Aug 12, 2024
Merged
Changes from 2 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
54bdf73
refactor(common/models): move primary trie-compilation code into comm…
jahorton 32a31bc
fix(developer): add 'build' dependency link to models/templates
jahorton c95b414
chore(common/models): Merge branch 'refactor/developer/build-trie' in…
jahorton a568ebb
chore(common/models): Merge branch 'epic/user-dict' into refactor/com…
jahorton f6e87f8
fix(developer): sets models-templates as package.json dependency
jahorton 894721d
chore(common): update package-lock.json
jahorton File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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,155 @@ | ||
import { SENTINEL_CODE_UNIT, Wordform2Key } from "./common.js"; | ||
import { Entry, InternalNode, Leaf, Node, Trie } from "./trie.js"; | ||
|
||
function createRootNode(): Node { | ||
return { | ||
type: 'leaf', | ||
weight: 0, | ||
entries: [] | ||
}; | ||
} | ||
|
||
/** | ||
* Adds an entry to the trie. | ||
* | ||
* Note that the trie will likely be unsorted after the add occurs. Before | ||
* performing a lookup on the trie, use call sortTrie() on the root note! | ||
* | ||
* @param node Which node should the entry be added to? | ||
* @param entry the wordform/weight/key to add to the trie | ||
* @param index the index in the key and also the trie depth. Should be set to | ||
* zero when adding onto the root node of the trie. | ||
*/ | ||
export function addUnsorted(node: Node, entry: Entry, index: number = 0) { | ||
// Each node stores the MAXIMUM weight out of all of its decesdents, to | ||
// enable a greedy search through the trie. | ||
node.weight = Math.max(node.weight, entry.weight); | ||
|
||
// When should a leaf become an interior node? | ||
// When it already has a value, but the key of the current value is longer | ||
// than the prefix. | ||
if (node.type === 'leaf' && index < entry.key.length && node.entries.length >= 1) { | ||
convertLeafToInternalNode(node, index); | ||
} | ||
|
||
if (node.type === 'leaf') { | ||
// The key matches this leaf node, so add yet another entry. | ||
addItemToLeaf(node, entry); | ||
} else { | ||
// Push the node down to a lower node. | ||
addItemToInternalNode(node, entry, index); | ||
} | ||
|
||
node.unsorted = true; | ||
} | ||
|
||
/** | ||
* Adds an item to the internal node at a given depth. | ||
* @param item | ||
* @param index | ||
*/ | ||
function addItemToInternalNode(node: InternalNode, item: Entry, index: number) { | ||
let char = item.key[index]; | ||
// If an internal node is the proper site for item, it belongs under the | ||
// corresponding (sentinel, internal-use) child node signifying this. | ||
if(char == undefined) { | ||
char = SENTINEL_CODE_UNIT; | ||
} | ||
if (!node.children[char]) { | ||
node.children[char] = createRootNode(); | ||
node.values.push(char); | ||
} | ||
addUnsorted(node.children[char], item, index + 1); | ||
} | ||
|
||
function addItemToLeaf(leaf: Leaf, item: Entry) { | ||
leaf.entries.push(item); | ||
} | ||
|
||
/** | ||
* Mutates the given Leaf to turn it into an InternalNode. | ||
* | ||
* NOTE: the node passed in will be DESTRUCTIVELY CHANGED into a different | ||
* type when passed into this function! | ||
* | ||
* @param depth depth of the trie at this level. | ||
*/ | ||
function convertLeafToInternalNode(leaf: Leaf, depth: number): void { | ||
let entries = leaf.entries; | ||
|
||
// Alias the current node, as the desired type. | ||
let internal = (<unknown> leaf) as InternalNode; | ||
internal.type = 'internal'; | ||
|
||
delete (leaf as Partial<Leaf>).entries; | ||
internal.values = []; | ||
internal.children = {}; | ||
|
||
// Convert the old values array into the format for interior nodes. | ||
for (let item of entries) { | ||
let char: string; | ||
if (depth < item.key.length) { | ||
char = item.key[depth]; | ||
} else { | ||
char = SENTINEL_CODE_UNIT; | ||
} | ||
|
||
if (!internal.children[char]) { | ||
internal.children[char] = createRootNode(); | ||
internal.values.push(char); | ||
} | ||
addUnsorted(internal.children[char], item, depth + 1); | ||
} | ||
|
||
internal.unsorted = true; | ||
} | ||
|
||
/** | ||
* Recursively sort the trie, in descending order of weight. | ||
* @param node any node in the trie | ||
*/ | ||
function sortTrie(node: Node) { | ||
if (node.type === 'leaf') { | ||
if (!node.unsorted) { | ||
return; | ||
} | ||
|
||
node.entries.sort(function (a, b) { return b.weight - a.weight; }); | ||
} else { | ||
// We MUST recurse and sort children before returning. | ||
for (let char of node.values) { | ||
sortTrie(node.children[char]); | ||
} | ||
|
||
if (!node.unsorted) { | ||
return; | ||
} | ||
|
||
node.values.sort((a, b) => { | ||
return node.children[b].weight - node.children[a].weight; | ||
}); | ||
} | ||
|
||
delete node.unsorted; | ||
} | ||
|
||
/** | ||
* Wrapper class for the trie and its nodes. | ||
*/ | ||
export class TrieBuilder extends Trie { | ||
/** The total weight of the entire trie. */ | ||
totalWeight: number; | ||
|
||
constructor(toKey: Wordform2Key) { | ||
super(createRootNode(), 0, toKey); | ||
this.totalWeight = 0; | ||
} | ||
|
||
sort() { | ||
sortTrie(this.root); | ||
} | ||
|
||
getRoot(): Node { | ||
return this.root; | ||
} | ||
} |
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
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I found this by referencing the original original implementation:
keyman/developer/src/kmc-model/src/build-trie.ts
Lines 207 to 208 in dd12e73
keyman/developer/src/kmc-model/src/build-trie.ts
Line 234 in dd12e73
This may seem "pulled up a tad bit too early", but there's a reason. If we 'construct' the Trie while live, modifications can make part of the Trie unsorted. We want to track that and correct for that.
The followup then changes this block, integrating it within the Trie traversal structure. Pulling it in early like this allows for code comparison between the two approaches.