QB / QB64 Discussion Forum     RULES     Other Subforums, Links and Downloads    Index of Threads

 

 Return to Index  

SPELL.BAS -- Version 5: USES THE THE DAMERAU-LEVENSHTEIN DISTANCE

September 10 2007 at 5:39 PM
qbguy  (no login)


Response to Spell Check Program

 

DECLARE FUNCTION DISTANCE% (S$, T$)
DECLARE FUNCTION MINIMUM% (A%, B%, C%)
DEFINT A-Z
OPEN "DICT.DAT" FOR INPUT AS #1
OPEN "RESULTS.TMP" FOR OUTPUT AS #2
INPUT SEARCH$
SEARCH$ = LTRIM$(RTRIM$(UCASE$(SEARCH$)))
WHILE NOT EOF(1)
INPUT #1, WORD$
WORD$ = UCASE$(WORD$)
IF DISTANCE(WORD$, SEARCH$) < 2 THEN PRINT #2, WORD$; " "
WEND
CLOSE #1
CLOSE #2
SHELL "FIND /I /C " + CHR$(34) + SEARCH$ + CHR$(32) + CHR$(34) + " RESULTS.TMP > FOUND.TMP"
OPEN "FOUND.TMP" FOR INPUT AS #1
INPUT #1, LINE$: INPUT #1, LINE$
IF INSTR(LINE$, " 0") THEN SHELL "MORE RESULTS.TMP": END ELSE PRINT "IN DICTIONARY"
CLOSE #1
SHELL "DEL *.TMP"
END

FUNCTION DISTANCE (S$, T$)
' RETURNS THE DAMERAU-LEVENSHTEIN DISTANCE BETWEEN TWO STRINGS
DIM D(0 TO LEN(S$), 0 TO LEN(T$))
FOR I = 0 TO LEN(S$)
D(I, 0) = I
NEXT
FOR J = 1 TO LEN(T$)
D(0, J) = J
NEXT
FOR I = 1 TO LEN(S$)
FOR J = 1 TO LEN(T$)
IF MID$(S$, I, 1) = MID$(T$, J, 1) THEN COST = 0 ELSE COST = 1
A = D(I - 1, J) + 1
IF J = LEN(T$) THEN B = 32767 ELSE B = D(I, J + 1) + 2
C = D(I - 1, J - 1) + COST
D(I, J) = MINIMUM(A, B, C)
IF I > 1 AND J > 1 THEN IF MID$(S$, I, 1) = MID$(T$, J - 1, 1) AND MID$(S$, I - 1, 1) = MID$(T$, J, 1) THEN D(I, J) = MINIMUM(D(I, J), D(I - 2, J - 2) + COST, 32767)
NEXT
NEXT
DISTANCE = D(LEN(S$), LEN(T$))
END FUNCTION

FUNCTION MINIMUM (A, B, C)
IF A <= B AND A <= C THEN MINIMUM = A
IF B <= A AND B <= C THEN MINIMUM = B
IF C <= A AND C <= B THEN MINIMUM = C
END FUNCTION

 
 Respond to this message   
 Copyright © 1999-2014 Network54. All rights reserved.   Terms of Use   Privacy Statement