for a real "good" correction, we need to know the set of words which are "reasonable". In a program editor, this depends on the word before the cursor. This may be either a class or variable name, or the name of the function to be called (some syntax awareness required here). In a general text editor, this would be a list of words from a dictionary.
In the following code, I assume that the list of "good" words is found in "candidates", in Smalltalk this would be one of:
Smalltalk allClasses collect:[:cls | cls name]
for class names, or
Smalltalk allClasses collectAll:[:cls | cls methodDictionary keys] as:Set
for the names of all methods.
Of course, in a real world corrector, this would take into account the current scope to restrict the set of possible candidates.
The code then selects the best candidate by its editing distance,
which is the distance between two strings based on the weighted number of insert, delete and replace operations required to change one string onto another. All Smalltalks provide a "spellAgainst:" method in their String class, which is based on levenshtein or a similar algorithm.
"the following is of course very Smalltalk dialect specific,
as the GUI frameworks are slightly different.
It assumes there is an editor object, which represents the code edit
window."
candidates := <one of the above>.
wordToFix := editor wordBeforeCursor.
best := candidates detectMax:[:each | each spellAgainst: wordToFix].
editor selectWordBeforeCursor.
editor replaceSelectionBy:best.
PS: one of the coolest features of Smalltalk is that the IDE is part of your program and vice versa (they are the same), so you can actually add the above code fragment to your editor's keyPress method and have it work immediately!
2Is it OK if we read correctly spelled words from a file? And are we reading from stdin? – golfer9338 – 2014-07-09T21:19:16.607
@golfer9338 Yes and yes. – DatEpicCoderGuyWhoPrograms – 2014-07-09T21:38:57.483
3Yes, there are some things which you need to clarify. What do you mean by "has to be able to make changes to any combination of characters in any string"? Which errors does it need to correct? What about overcorrection? What about other languages? – Peter Taylor – 2014-07-09T22:02:32.400
@PeterTaylor It needs to be able to fix basic spelling errors. Such as the swapping of I and E. Like misspelling their. It doesn't matter what language you make it in. In the case of overcorrecting, that's okay if it makes a few mistakes, even advanced autocorrect like the one on my iphone makes mistakes. – DatEpicCoderGuyWhoPrograms – 2014-07-09T22:07:00.827
1You need a set of production rules like "If you see this, it should be replaced with this." Making it a popularity contest is not an excuse for completely leaving out the details of the task you have asked us to do. Btw, golfer9338 added a neat idea: read correctly spelled words from a file. His edit was not perfect, but I approved it anyway because I think that his idea could salvage this challenge. – Rainbolt – 2014-07-11T02:01:34.497
It's still completely opaque what you mean by "has to be able to make changes to any combination of characters in any string". – Peter Taylor – 2014-07-11T10:29:47.803