Versioned Text Correction Previews
Problem statement
A text editor receives correction suggestions from a spelling and grammar model while a user types. Process the supplied ordered events and return a preview for every SUGGEST event.
For this exercise, assume the initial document has revision 0. An ["EDIT", newText] event replaces the entire current document and increases its revision by one, even if its text is unchanged. A ["SUGGEST", revision, start, end, replacement] event is the supplied model output for one half-open character range [start,end).
If the suggestion revision equals the current revision, return ["CURRENT", correctedText], replacing that range with replacement. A preview does not modify the current document or its revision. Otherwise return ["STALE", ""] and ignore the range. This also rejects a revision from the future. Preserve suggestion output order; EDIT events produce no output.
Function
previewVersionedTextCorrections(initialText: String, events: String[][]) → String[][]Examples
Example 1
initialText = "teh cat"events = [["SUGGEST","0","0","3","the"],["EDIT","teh cats"],["SUGGEST","0","0","3","the"],["SUGGEST","1","0","3","the"]]return = [["CURRENT","the cat"],["STALE",""],["CURRENT","the cats"]]The edit makes the delayed revision-zero prediction stale.
Example 2
initialText = "a b"events = [["SUGGEST","0","1","2",""],["SUGGEST","0","3","3","!"]]return = [["CURRENT","ab"],["CURRENT","a b!"]]Both previews use the unchanged document, because suggestions do not commit.
Example 3
initialText = ""events = [["EDIT","hi"],["SUGGEST","1","0","2","hello"]]return = [["CURRENT","hello"]]The first edit creates revision one.
Constraints
0 <= events.length <= 5000; every event has one of the exact row shapes above.- All text contains ASCII characters and each document or replacement has at most
2000characters. - Revision, start and end are nonnegative decimal strings. Revision is at most
5000, and0 <= start <= end <= 2000. - For a current suggestion,
end <= currentText.length. Stale suggestion ranges need not fit the current text. - The total number of characters in all current previews is at most
100000. Empty ranges insert text; empty replacements delete text.