refactor(common/models): convert traversal.children method to return array 📖 #12089
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.
At present, our correction and prediction search methods are not leveraging the generator-based form of the
children()
field on theLexiconTraversal
type. When we ask for it, we always utilize every entry in the array in some form. So, why continue to maintain the generator pattern if we can just return an array instead?To support #11994, this also adds a new entry to the returned objects:
p
, the probability of the highest-frequency entry on the node's path. We've already been returning it on the node itself, but there can be benefits to making it available before constructing the traversal itself; we can compare against the value and avoid overhead if we decide not to take the path, rather than constructing it and only then checking.Part of the motivation here:
keyman/common/models/types/index.d.ts
Lines 46 to 70 in dd12e73
Examining the current specification of the method (within the linked block above)... note that nowhere within that spec is a requirement or guarantee that the entries be in sorted order. Granted, our current implementation certainly does enforce this for Trie wordlist models. Interestingly, upon further examination... there's not actually a need to even require that the entries be sorted. We generally toss them into a priority queue, and they'll be "sorted" there.
Specifying
p
here gives us the basis to enforce a sort if and needed, without having to call the traversal-generating functions in advance. There are other cases (such as for model-blending) where thep
value is needed more as a "filter" than as a sorting condition; it's useful for that as well.@keymanapp-test-bot skip