Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import { ContextTransition } from './correction/context-transition.js';
import { ExecutionTimer } from './correction/execution-timer.js';
import { ModelCompositor } from './model-compositor.js';
import { EDIT_DISTANCE_COST_SCALE, getBestTokenMatches } from './correction/distance-modeler.js';
import { TokenResult } from './correction/tokenization-corrector.js';

import CasingForm = LexicalModelTypes.CasingForm;
import Context = LexicalModelTypes.Context;
Expand All @@ -25,7 +26,6 @@ import Reversion = LexicalModelTypes.Reversion;
import Suggestion = LexicalModelTypes.Suggestion;
import SuggestionTag = LexicalModelTypes.SuggestionTag;
import Transform = LexicalModelTypes.Transform;
import { TokenResult } from './correction/tokenization-corrector.js';

/*
* The functions in this file exist to provide unit-testable stateless components for the
Expand Down Expand Up @@ -470,6 +470,34 @@ export interface PredictionParameters {
applyInPost: (entry: CorrectionPredictionTuple) => void
}

export function buildCorrectionSequence(
transitionEffects: ReturnType<typeof determineSuggestionRange>,
context: Context,
match: Readonly<TokenResult>,
costFactor: number
) {
const { deleteLeft } = transitionEffects;

const rootContext = models.applyTransform({insert: '', deleteLeft}, context);

// Replace the existing context with the correction.
const correctionTransform: Transform = {
insert: match.matchString, // insert correction string
deleteLeft: 0,
}

const rootCost = match.totalCost;
const predictionRoot = {
sample: correctionTransform,
p: Math.exp(-rootCost * costFactor)
};

return {
rootContext,
tokenizedCorrection: [predictionRoot]
};
}

/**
* This function takes in metadata about generated corrections (for models that
* implement Traversals) and uses that to produce the corresponding parameters
Expand All @@ -491,31 +519,20 @@ export function determineTokenizedCorrectionSequence(
costFactor: number
): PredictionParameters {
const applicationTarget = transition.base.displayTokenization;
const { deleteLeft } = determineSuggestionRange(applicationTarget.tokens, tokenization.tokens, (a, b) => a.spaceId == b.spaceId);

const rootContext = models.applyTransform({insert: '', deleteLeft}, transition.base.context);
const transitionParams = determineSuggestionRange(applicationTarget.tokens, tokenization.tokens, (a, b) => a.spaceId == b.spaceId);

// Replace the existing context with the correction.
const correctionTransform: Transform = {
insert: match.matchString, // insert correction string
deleteLeft: 0,
}
const suggestionParams = buildCorrectionSequence(transitionParams, transition.base.context, match, costFactor);

// The correction should always be based on the most recent external
// transform/transcription ID.
if(transition.transitionId !== undefined) {
correctionTransform.id = transition.transitionId;
suggestionParams.tokenizedCorrection.forEach((t) => t.sample.id = transition.transitionId);
}

const rootCost = match.totalCost;
const predictionRoot = {
sample: correctionTransform,
p: Math.exp(-rootCost * costFactor)
};
const { deleteLeft } = transitionParams;

return {
rootContext,
tokenizedCorrection: [predictionRoot],
...suggestionParams,
applyInPost: (entry: CorrectionPredictionTuple) => {
entry.preservationTransform = tokenization.taillessTrueKeystroke;
// // Will need an extra lookup layer if the suggestion is generated from within a cluster.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -286,7 +286,7 @@ describe('determineTokenizedCorrectionSequence', () => {
]);
});

it(`properly analyzes conplex transition - multi-token replacement`, () => {
it(`properly analyzes complex transition - multi-token replacement`, () => {
const context: Context = {
left: 'the quick brown f',
right: '',
Expand Down
Loading