-
-
Notifications
You must be signed in to change notification settings - Fork 132
refactor(web): differentiate search-space module types 🚂 #15413
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
Open
jahorton
wants to merge
5
commits into
refactor/web/use-interface-as-search-parent
Choose a base branch
from
refactor/web/differentiate-module-types
base: refactor/web/use-interface-as-search-parent
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+293
−90
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
348e876
refactor(web): differentiate search-space module types
jahorton 5a17050
change(web): improve nil-input case handling
jahorton a281711
refactor(web): create separate types for search graph root notes
jahorton dc383f8
Merge branch 'refactor/web/use-interface-as-search-parent' into refac…
jahorton 4807e35
docs(web): fix outdated doc-comments, typos per review
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 hidden or 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
61 changes: 61 additions & 0 deletions
61
web/src/engine/predictive-text/worker-thread/src/main/correction/legacy-quotient-root.ts
This file contains hidden or 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,61 @@ | ||
| import { PriorityQueue } from '@keymanapp/web-utils'; | ||
| import { LexicalModelTypes } from '@keymanapp/common-types'; | ||
|
|
||
| import { SearchQuotientRoot } from './search-quotient-root.js'; | ||
| import { QUEUE_NODE_COMPARATOR } from './search-quotient-spur.js'; | ||
| import { SearchNode, SearchResult } from './distance-modeler.js'; | ||
|
|
||
| import LexicalModel = LexicalModelTypes.LexicalModel; | ||
| import { PathResult } from './search-quotient-node.js'; | ||
|
|
||
| export class LegacyQuotientRoot extends SearchQuotientRoot { | ||
| private selectionQueue: PriorityQueue<SearchNode> = new PriorityQueue(QUEUE_NODE_COMPARATOR); | ||
| private processed: SearchResult[] = []; | ||
|
|
||
| constructor(model: LexicalModel) { | ||
| super(model); | ||
|
|
||
| this.selectionQueue.enqueue(this.rootNode); | ||
| } | ||
|
|
||
| // TODO: Remove when removing LegacyQuotientSpur! | ||
| // At that time, inserts should have their own devoted 'Spur' type and not be managed | ||
| // within the same pre-existing instance. | ||
| /** | ||
| * Retrieves the lowest-cost / lowest-distance edge from the selection queue, | ||
| * checks its validity as a correction to the input text, and reports on what | ||
| * sort of result the edge's destination node represents. | ||
| * @returns | ||
| */ | ||
| public handleNextNode(): PathResult { | ||
| const node = this.selectionQueue.dequeue(); | ||
|
|
||
| if(!node) { | ||
| return { | ||
| type: 'none' | ||
| }; | ||
| } | ||
|
|
||
| // The legacy variant includes 'insert' operations! | ||
| if(node.editCount < 2) { | ||
| let insertionEdges = node.buildInsertionEdges(); | ||
| this.selectionQueue.enqueueAll(insertionEdges); | ||
| } | ||
|
|
||
| this.processed.push(new SearchResult(node)); | ||
| return { | ||
| type: 'complete', | ||
| cost: node.currentCost, | ||
| finalNode: node, | ||
| spaceId: this.spaceId | ||
| }; | ||
| } | ||
|
|
||
| public get currentCost(): number { | ||
| return this.selectionQueue.peek()?.currentCost ?? Number.POSITIVE_INFINITY; | ||
| } | ||
|
|
||
| get previousResults(): SearchResult[] { | ||
| return this.processed.slice(); | ||
| } | ||
| } | ||
80 changes: 80 additions & 0 deletions
80
web/src/engine/predictive-text/worker-thread/src/main/correction/legacy-quotient-spur.ts
This file contains hidden or 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,80 @@ | ||
| /* | ||
| * Keyman is copyright (C) SIL Global. MIT License. | ||
| * | ||
| * Created by jahorton on 2025-10-09 | ||
| * | ||
| * This file defines tests for the predictive-text engine's SearchPath class, | ||
| * which is used to manage the search-space(s) for text corrections within the | ||
| * engine. | ||
| */ | ||
|
|
||
| import { LexicalModelTypes } from '@keymanapp/common-types'; | ||
|
|
||
| import { SearchNode } from './distance-modeler.js'; | ||
| import { PathResult, SearchQuotientNode } from './search-quotient-node.js'; | ||
| import { SearchQuotientSpur } from './search-quotient-spur.js'; | ||
|
|
||
| import Distribution = LexicalModelTypes.Distribution; | ||
| import Transform = LexicalModelTypes.Transform; | ||
|
|
||
| // The set of search spaces corresponding to the same 'context' for search. | ||
| // Whenever a wordbreak boundary is crossed, a new instance should be made. | ||
| export class LegacyQuotientSpur extends SearchQuotientSpur { | ||
| /** | ||
| * Constructs a fresh SearchQuotientNode instance for use in predictive-text | ||
| * correction and suggestion searches. | ||
| * @param space | ||
| * @param inputs | ||
| * @param bestProbFromSet | ||
| */ | ||
| constructor(space: SearchQuotientNode, inputs: Distribution<Transform>, bestProbFromSet: number) { | ||
| super(space, inputs, space.lowestPossibleSingleCost - Math.log(bestProbFromSet)); | ||
| this.queueNodes(this.buildEdgesForNodes(space.previousResults.map(r => r.node))); | ||
| return; | ||
| } | ||
|
|
||
| protected buildEdgesForNodes(baseNodes: ReadonlyArray<SearchNode>) { | ||
| // With a newly-available input, we can extend new input-dependent paths from | ||
| // our previously-reached 'extractedResults' nodes. | ||
| let outboundNodes = baseNodes.map((node) => { | ||
| // Hard restriction: no further edits will be supported. This helps keep the search | ||
| // more narrowly focused. | ||
| const substitutionsOnly = node.editCount == 2; | ||
|
|
||
| let deletionEdges: SearchNode[] = []; | ||
| if(!substitutionsOnly) { | ||
| deletionEdges = node.buildDeletionEdges(this.inputs, this.spaceId); | ||
| } | ||
| const substitutionEdges = node.buildSubstitutionEdges(this.inputs, this.spaceId); | ||
|
|
||
| // Skip the queue for the first pass; there will ALWAYS be at least one pass, | ||
| // and queue-enqueing does come with a cost - avoid unnecessary overhead here. | ||
| return substitutionEdges.flatMap(e => e.processSubsetEdge()).concat(deletionEdges); | ||
| }).flat(); | ||
|
|
||
| return outboundNodes; | ||
| } | ||
|
|
||
| /** | ||
| * Retrieves the lowest-cost / lowest-distance edge from the selection queue, | ||
| * checks its validity as a correction to the input text, and reports on what | ||
| * sort of result the edge's destination node represents. | ||
| * @returns | ||
| */ | ||
| public handleNextNode(): PathResult { | ||
| const result = super.handleNextNode(); | ||
|
|
||
| if(result.type == 'complete') { | ||
| const currentNode = result.finalNode; | ||
|
|
||
| // Forbid a raw edit-distance of greater than 2. | ||
| // Note: .knownCost is not scaled, while its contribution to .currentCost _is_ scaled. | ||
| if(currentNode.editCount < 2) { | ||
| let insertionEdges = currentNode.buildInsertionEdges(); | ||
| this.queueNodes(insertionEdges); | ||
| } | ||
| } | ||
|
|
||
| return result; | ||
| } | ||
| } |
92 changes: 92 additions & 0 deletions
92
web/src/engine/predictive-text/worker-thread/src/main/correction/search-quotient-root.ts
This file contains hidden or 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,92 @@ | ||
|
|
||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. missing header comment |
||
| import { LexicalModelTypes } from '@keymanapp/common-types'; | ||
|
|
||
| import { SearchNode, SearchResult } from './distance-modeler.js'; | ||
| import { generateSpaceSeed, PathResult, SearchQuotientNode } from './search-quotient-node.js'; | ||
|
|
||
| import LexicalModel = LexicalModelTypes.LexicalModel; | ||
|
|
||
| // The set of search spaces corresponding to the same 'context' for search. | ||
| // Whenever a wordbreak boundary is crossed, a new instance should be made. | ||
| export class SearchQuotientRoot implements SearchQuotientNode { | ||
| readonly rootNode: SearchNode; | ||
| private readonly rootResult: SearchResult; | ||
|
|
||
| readonly lowestPossibleSingleCost: number = 0; | ||
|
|
||
| readonly inputCount: number = 0; | ||
| readonly correctionsEnabled: boolean = false; | ||
|
|
||
| private hasBeenProcessed: boolean = false; | ||
|
|
||
| /** | ||
| * Constructs a fresh SearchQuotientRoot instance to be used as the root of | ||
| * the predictive-text correction / suggestion search process. | ||
| * @param baseSpaceId | ||
| * @param model | ||
| */ | ||
| constructor(model: LexicalModel) { | ||
| this.rootNode = new SearchNode(model.traverseFromRoot(), generateSpaceSeed(), t => model.toKey(t)); | ||
| this.rootResult = new SearchResult(this.rootNode); | ||
| } | ||
|
|
||
| get spaceId(): number { | ||
| return this.rootNode.spaceId; | ||
| } | ||
|
|
||
| hasInputs(keystrokeDistributions: LexicalModelTypes.Distribution<LexicalModelTypes.Transform>[]): boolean { | ||
| return keystrokeDistributions.length == 0; | ||
| } | ||
|
|
||
| // Return a new array each time; avoid aliasing potential! | ||
| get parents(): SearchQuotientNode[] { | ||
| return []; | ||
| } | ||
|
|
||
| // Return a new array each time; avoid aliasing potential! | ||
| get inputSequence(): LexicalModelTypes.Distribution<LexicalModelTypes.Transform>[] { | ||
| return []; | ||
| } | ||
|
|
||
| // Return a new instance each time; avoid aliasing potential! | ||
| get bestExample(): { text: string; p: number; } { | ||
| return { text: '', p: 1 }; | ||
| } | ||
|
|
||
| increaseMaxEditDistance(): void { | ||
| this.rootNode.calculation = this.rootNode.calculation.increaseMaxDistance(); | ||
| } | ||
|
|
||
| /** | ||
| * Retrieves the lowest-cost / lowest-distance edge from the selection queue, | ||
| * checks its validity as a correction to the input text, and reports on what | ||
| * sort of result the edge's destination node represents. | ||
| * @returns | ||
| */ | ||
| public handleNextNode(): PathResult { | ||
| if(this.hasBeenProcessed) { | ||
| return null; | ||
| } | ||
|
|
||
| this.hasBeenProcessed = true; | ||
|
|
||
| return { | ||
| type: 'complete', | ||
| cost: 0, | ||
| finalNode: this.rootNode, | ||
| spaceId: this.spaceId | ||
| }; | ||
| } | ||
|
|
||
| public get currentCost(): number { | ||
| return this.hasBeenProcessed ? Number.POSITIVE_INFINITY : 0; | ||
| } | ||
|
|
||
| get previousResults(): SearchResult[] { | ||
| if(!this.hasBeenProcessed) { | ||
| return []; | ||
| } else { | ||
| return [this.rootResult]; | ||
| } | ||
| } | ||
| } | ||
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.
missing prolog comment