Ooops
November 28, 2020, 10:38:43 AM

Author Topic: [bb] SoundEx Search by Dr. Wildrick [ 1+ years ago ]  (Read 371 times)

Offline BlitzBot

  • Jr. Member
  • **
  • Posts: 1
[bb] SoundEx Search by Dr. Wildrick [ 1+ years ago ]
« on: June 29, 2017, 12:28:39 AM »
Title : SoundEx Search
Author : Dr. Wildrick
Posted : 1+ years ago

Description : This function generates SoundEx codes. Soundex is way of comparing what words sound like. So if two words have the same Soundex code they sound similar. This can be used for spell checking (to suggest entries in the dictionary that might be what they were trying to spell), database searches, Artificial intelligence the list goes on. Basically if you have a word and you want to find any word or entry that sounds similar to it... this function is for you. Its use is simple you call the function with the word as a parameter. You get back a 4 byte string. A letter followed by 3 numbers. The letter helps you narrow down your search to just words that start with that letter, This makes Dictionary comparison simple and easy to code.
Constructive criticism or comments are welcome, flames will be laughed at and ignored.
Enjoy!


Code :
Code: BlitzBasic
  1. Function SoundEx$(sWord$)
  2. ;****************************************************************
  3. ; SoundEx code Generator - Cherry Cola Film Studios LLC
  4. ;****************************************************************
  5.  
  6. ; Set the input lookup table and the return codes
  7. ; note the return code is the position of the letter in the
  8. ; first string +1 This weeds out non-letters. Any return code of
  9. ; 0 will return a null string
  10. ; Note A,E,I,O,U,Y,H, and W return a Null string
  11.  
  12. ; to use this function is simple:
  13. ; B$ = SoundEx$(A$)
  14. ; Where A$ is the word you want the SoundEx code for
  15. ; and B$ is the returned SoundEx code as a 4 byte string
  16. ; For example the word "Gothic" returns a code of "G320"
  17.  
  18.  
  19. l1$= "BFPVCGJKQSXZDTLMNR"
  20. l2$="0111122222222334556"
  21.  
  22.  
  23.     ; Get the First letter
  24.     Num$ = Upper$(Mid$(sWord$, 1, 1))
  25.     sLastCode$ = num$
  26.                 sLastCode$=Mid$(l2$,(Instr(l1$,sLastCode$)+1),1)
  27.                 If  sLastCode$= "0" Then sLastCode$ = ""
  28.                
  29.    lWordLength = Len(sWord$)
  30.        
  31.     ;Create the code starting at the second letter.
  32.     For I = 2 To lWordLength
  33.         sChar$ = Upper$(Mid$(sWord$, I, 1))
  34.  
  35.             sChar$=Mid$(l2$,(Instr(l1$,sChar$)+1),1)
  36.                 If sChar$= "0" Then sChar$ = ""
  37.        
  38.         ; If two letters that are the same are Next To Each other
  39.         ; only count one of them
  40.         If Len(sChar$) > 0 And sLastCode$ <> sChar$ Then
  41.             Num$ = Num$ + sChar$
  42.         End If
  43.         sLastCode$ = sChar$
  44.     Next
  45.     ; Make sure code isn't longer Then 4 letters
  46.     SEx$ = Mid$(Num$, 1, 4)
  47.     ;Make sure the code is at least 4 characters long
  48.     If Len(Num$) < 4 Then
  49.         SEx$ = SEx$  + String$("0",4 - Len(Num$))
  50.     End If
  51. Return sex$


Comments : none...

 

SimplePortal 2.3.6 © 2008-2014, SimplePortal