Skip to content

Commit bfad3ef

Browse files
committed
fix(web): prevent insert-delete edit sequences from aliasing as substitution edits
Build-bot: skip build:web Test-bot: skip
1 parent 4f98dbf commit bfad3ef

File tree

1 file changed

+12
-1
lines changed

1 file changed

+12
-1
lines changed

web/src/engine/predictive-text/worker-thread/src/main/correction/distance-modeler.ts

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,14 @@ export class SearchNode {
135135
*/
136136
private _inputCost?: number;
137137

138+
/**
139+
* Counts the number of delete edits modeled directly after insert edits in
140+
* this SearchNode's path. Such a sequence shows up within the calculation
141+
* matrix as a substitution rather than two separate edits; we use this count
142+
* to adjust accordingly.
143+
*/
144+
private readonly deleteAfterInsertEditPairs: number;
145+
138146
/**
139147
* A unique identifier corresponding to the earliest SearchPath containing
140148
* the correction-search graph edge represented by this instance.
@@ -164,6 +172,8 @@ export class SearchNode {
164172
this.matchedTraversals = priorNode.matchedTraversals.slice();
165173

166174
this.lastEdgeType = param2 as PathEdge;
175+
const isInsertAfterDelete = priorNode.lastEdgeType == PathEdge.INSERTION && this.lastEdgeType == PathEdge.DELETION;
176+
this.deleteAfterInsertEditPairs = priorNode.deleteAfterInsertEditPairs + (isInsertAfterDelete ? 1 : 0);
167177

168178
// Do NOT copy over _inputCost; this is a helper-constructor for methods
169179
// building new nodes... which will have a different cost.
@@ -180,6 +190,7 @@ export class SearchNode {
180190
this.toKey = toKey || (x => x);
181191
this.spaceId = spaceId;
182192
this.lastEdgeType = PathEdge.ROOT;
193+
this.deleteAfterInsertEditPairs = 0;
183194
}
184195
}
185196

@@ -189,7 +200,7 @@ export class SearchNode {
189200
* by the current node.
190201
*/
191202
get editCount(): number {
192-
return this.calculation.getHeuristicFinalCost();
203+
return this.calculation.getHeuristicFinalCost() + this.deleteAfterInsertEditPairs;
193204
}
194205

195206
/**

0 commit comments

Comments
 (0)