Skip to content

Commit b467cc4

Browse files
committed
refactor(web): refactor local input cost heuristic
These changes were previously part of #14987 and have been extracted for ease of review. Build-bot: skip build:web Test-bot: skip
1 parent c6f6eb0 commit b467cc4

File tree

2 files changed

+18
-9
lines changed

2 files changed

+18
-9
lines changed

web/src/engine/predictive-text/worker-thread/src/main/correction/search-quotient-node.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,16 @@ export interface SearchQuotientNode {
6464
*/
6565
readonly currentCost: number;
6666

67+
/**
68+
* Provides a heuristic for the base cost at this path's depth if the best
69+
* individual input were taken here, regardless of whether or not that's
70+
* possible.
71+
*
72+
* This cost is based on the negative log-likelihood of the probability and
73+
* includes the cost from the lowest possible parent nodes visited.
74+
*/
75+
readonly lowestPossibleSingleCost: number;
76+
6777
/**
6878
* Returns the set of previously-processed results under this batcher's domain.
6979
*/

web/src/engine/predictive-text/worker-thread/src/main/correction/search-quotient-spur.ts

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -34,16 +34,16 @@ export class SearchQuotientSpur implements SearchQuotientNode {
3434
readonly spaceId: number;
3535

3636
/**
37-
* Marks all results that have already been returned since the last input was received.
38-
* Is cleared after .addInput() calls.
37+
* Marks all results that have already been returned from this instance of SearchPath.
38+
* Should be deleted and cleared if any paths consider this one as a parent.
3939
*/
4040
private returnedValues?: {[resultKey: string]: SearchNode} = {};
4141

4242
/**
43-
* Provides a heuristic for the base cost at each depth if the best
44-
* individual input were taken at that level.
43+
* Provides a heuristic for the base cost at this path's depth if the best
44+
* individual input were taken here, regardless of whether or not that's possible.
4545
*/
46-
private lowestCostAtDepth: number[];
46+
readonly lowestPossibleSingleCost: number;
4747

4848
/**
4949
* Constructs a fresh SearchSpace instance for used in predictive-text correction
@@ -61,7 +61,7 @@ export class SearchQuotientSpur implements SearchQuotientNode {
6161
const logTierCost = -Math.log(bestProbFromSet);
6262

6363
this.inputs = inputs;
64-
this.lowestCostAtDepth = parentNode.lowestCostAtDepth.concat(logTierCost);
64+
this.lowestPossibleSingleCost = parentNode.lowestPossibleSingleCost + logTierCost;
6565
this.parentPath = parentNode;
6666

6767
this.addEdgesForNodes(parentNode.previousResults.map(v => v.node));
@@ -71,7 +71,7 @@ export class SearchQuotientSpur implements SearchQuotientNode {
7171

7272
const model = arg1 as LexicalModel;
7373
this.selectionQueue.enqueue(new SearchNode(model.traverseFromRoot(), this.spaceId, t => model.toKey(t)));
74-
this.lowestCostAtDepth = [];
74+
this.lowestPossibleSingleCost = 0;
7575
}
7676

7777
/**
@@ -202,8 +202,7 @@ export class SearchQuotientSpur implements SearchQuotientNode {
202202
// ... or even just not the then-current layer of the keyboard.
203203
//
204204
// TODO: still consider the lowest-cost individual edges for THIS specific criterion.
205-
const tierMinCost = this.lowestCostAtDepth[currentNode.priorInput.length-1];
206-
if(currentNode.currentCost > tierMinCost + 2.5 * EDIT_DISTANCE_COST_SCALE) {
205+
if(currentNode.currentCost > this.lowestPossibleSingleCost + 2.5 * EDIT_DISTANCE_COST_SCALE) {
207206
return unmatchedResult;
208207
}
209208

0 commit comments

Comments
 (0)