Did you mean: SpellCorrection?

  • This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn more.
Nov 13, 2008
17
0
#1
I am pathetic at pyting on the computer and I keep making pselling mitsakes.
So I wrote a batch file to correct myself. Just for the heck of it.
That is, if you type "dri" instead of "dir", it prompts you - Did you mean "dir"?
The "did you mean" concept is stolen from google. hope they dont sue me. :-)

1. It works only for internal commands and aliases. Does not work for external applications.
2. I have aliased unknown_cmd=ucmd.bat
3. It corrects 'csl' to 'cls', but it does not correct 'fit' to 'dir' (That is, it tries jumbling up the characters, does not try adjacent keys on keyboard)
4. my ucmd.bat is below

PHP:
@echo off
break on
on break quit
set a=%1
set p=
set n=%@eval[%@len[%a%]-1]
for /l %x in (0,1,%n%) do set p=%p%%x
set i=1
do while %i% lt %@eval[%n+1] 
	call :decr1 %i p
	set pi=%@instr[%i%,1,%p%]
	set j=%@eval[(%i %% 2) * %pi]
    call :swap1 %i %j a
	iff isinternal %a .or. isalias %a then
		set resp=n
		set yrow=%_row
		set ycol=%@eval[%@len[Did you mean: "%a %2$" ?]]
		scrput +0 +%ycol bri %_fg on %_bg Y
		inkey /k"yn[esc][enter]" /w10 Did you mean: "%a %2$" ? %%resp
		if %resp==@28 set resp=y
		if %resp==%@char[27] set resp=n
		: Display the response on screen (in case of ESC or enter)
		scrput %yrow %ycol %_fg on %_bg %resp 
		iff %resp==y then
		  %a %2$
		  quit
		endiff
	endiff
	set i=1
	set pi=%@instr[%i%,1,%p%]
	do while %pi eq 0
	  call :setval %i p
	  set i=%@inc[%i]
	  set pi=%@instr[%i%,1,%p%]
	enddo
enddo
echo Unknown command "%$"
exit /b

:swap1
set ta=
set s1=%@instr[%2,1,%[%3]] 
set s2=%@instr[%1,1,%[%3]] 
for /l %x in (0,1,%n%) do (
 set swc=%@instr[%x,1,%[%3]]
 iff %x eq %1 then
  set swc=%s1
 endiff
 iff %2 eq %x then 
  set swc=%s2
 endiff
 set ta=%ta%%swc%
)
set %3=%ta
exit /b


:decr1
set ta=
set i1=%@instr[%1,1,%[%2]]
for /l %x in (0,1,%n%) do (
 set swc=%@instr[%x,1,%[%2]]
 iff %x eq %1 then
  set swc=%@dec[%i1]
 endiff
 set ta=%ta%%swc%
)
set %2=%ta
exit /b

:setval
set ta=
for /l %x in (0,1,%n%) do (
 set swc=%@instr[%x,1,%[%2]]
 iff %x eq %1 then
  set swc=%1
 endiff
 set ta=%ta%%swc%
)
set %2=%ta
exit /b
Example:

02:54:51 C:\...\bat>dle DayanandLoka.ram.ram.delete
Did you mean: "del DayanandLoka.ram.ram.delete" ?n

02:59:19 C:\...\bat>hdc mine
Did you mean: "chd mine" ?n
Did you mean: "cdh mine" ?n

02:59:40 C:\...\bat>

In the second case, both chd and cdh are aliases.
[Edit: added to display "Unknown command" if nothing worked out]