Phonetic spelling
From QB64 Wiki
The Soundex Function scores word spelling based on the possible phonetic sounds when words are spoken. It can be used in a program where a user entry may not be spelled correctly and the choices of words to compare the spelling with are limited.
DECLARE FUNCTION Soundex$ (word AS STRING) DECLARE SUB Align (Tclr, Trow, Txt$) DIM SHARED Qt AS STRING * 1 Qt = CHR$(34) Align 11, 10, "Try misspelled words like LIKWOOD, SKOOL, ENUFF, FONE" DO: good$ = "False" COLOR 14: LOCATE 15, 4: INPUT "Misspell a word(Enter quits): ", wrd$ IF LEN(wrd$) = 0 THEN EXIT DO Align 0, 19, SPACE$(5) Align 0, 20, SPACE$(20) LOCATE 17, 4: PRINT SPACE$(50) misspell$ = Soundex$(wrd$) COLOR 13: LOCATE 17, 4: INPUT "Spell word correctly: ", word$ spell$ = Soundex$(word$) 'can use a file or DATA to return the proper words IF LEN(spell$) AND misspell$ = spell$ THEN good$ = "Match" Align 14, 19, good$ Align 13, 20, Qt + misspell$ + Qt + SPACE$(5) + Qt + spell$ + Qt 'Align 11, 21, wrd$ + SPACE$(5) + word$ 'displays words entered _DELAY 4 LOCATE 15, 4: PRINT SPACE$(50) LOOP SYSTEM FUNCTION Soundex$ (s AS STRING) DIM c AS INTEGER, p AS INTEGER, w AS INTEGER, CodeTab AS STRING * 26 DIM wd AS STRING, ss AS STRING, PrevCode AS STRING, Code AS STRING 'CodeTab = " 123 12 22455 12623 1 2 2" 'original Soundex constant 'alphabet: abcdefghijklmnopqrstuvwxyz CodeTab = " 123 11 22455 12633 1 2 3" 'adjusted constant for f, ph, gh IF LEN(s) = 0 THEN EXIT FUNCTION 'avoid empty string ASC error wd = UCASE$(s): p = 1 'change to all uppercase letters FOR w = 1 TO LEN(wd) 'scan words for all letters c = ASC(MID$(wd, w, 1)) IF c < 65 OR c > 90 THEN EXIT FUNCTION NEXT DO WHILE LEN(ss) < 5 AND p <= LEN(wd) c = ASC(MID$(wd, p, 1)) Code = MID$(CodeTab, c - 64, 1) IF Code$ <> SPACE$(1) AND Code <> PrevCode THEN ss = ss + Code PrevCode = Code: p = p + 1 LOOP IF LEN(ss) < 5 THEN ss = ss + STRING$(5 - LEN(ss), "0") Soundex$ = ss END FUNCTION SUB Align (Tclr, Trow, Txt$) Tcol = 41 - (LEN(Txt$) \ 2) COLOR Tclr: LOCATE Trow, Tcol: PRINT Txt$; END SUB
- Explanation: Each letter is rated compared to other letters for the kinds of phonetic sounds they produce: f, ph and gh = 1