TS2068 Subroutine Adds Intelligence to Programs

Authors

Publication

Pub Details

Date

Pages

BASIC Terms

See all articles from Quarters Spring 1985

Computer users are painfully aware of the need to input information perfectly if a computer program is to understand them.

Here’s a short subroutine that adds intelligence to your programs to make them user friendly. It will compare a user’s input to a list of words that a computer is expecting. If an exact match is not found it calculates the best match based on the similarity of the letters in the words. This allows the program to accept user inputs with typos rather than make the user enter the info again.

 1000 RESTORE 1100: REM WORD LIST POINTER
1005 LET R=0: LET C$=""
1010 READ B$: IF B$="*" THEN RETURN
1015 IF B$=A$ THEN LET C$=B*: RETURN
1020 LET T=0
1025 FOR I=1 TO LEN A$
1030 FOR J=1 TO LEN B$
1035 IF ABS(J-I)>3 THEN GOTO 1045
1040 IF A$(I)=B$(J) THEN LET T=T+2 (3-(ABS(J-I)))
1045 NEXT J
1050 NEXT I
1055 IF T>R THEN R=T: LET C$=B$
1060 GOTO 1010

Best matches are found by calculating a score for letter-to-letter similarity. If two matching letters are in the same position, 8 points are added to the total score. If they are off by one position, 4 points are added. Two positions off adds 2 points and three positions off adds 1 point. If the matching letters are more than three positions apart nothing is added. Once all letters are checked, the word is ranked and the word with the highest score is returned.

A sample program and word list is shown below. Note that the input to the routine is C$, and the last list entry is a “*”. Run the program and input one of the choices on the list with a typo in it. The routine will probably print the word that meant to type.

   10 INPUT "ENTER A WORD: "; A$
20 GOSUB 1000
30 PRINT "THE CLOSEST MATCH IS ";C$
40 STOP
1100 DATA "INTERROGATE", "INTEGRATE", "INSPECT", "INNOVATE"
1110 DATA "IMPORT", "ILLUMINATE" , "INTERNATIONAL", "*"

Products

 

Media

 

Image Gallery

Source Code

Note: Type-in program listings on this website use ZMAKEBAS notation for graphics characters.

Scroll to Top