Beranda > Delphi > Determine if 2 strings sound alike

Determine if 2 strings sound alike

Soundex function--determines whether
two words sound alike. Written after
reading an article in PC Magazine
about the Soundex algorithm.
Pass the function a string. It
returns a Soundex value string. This
value can be saved in a database or
compared to another Soundex value. If two words have
the same Soundex value, then they sound alike (more
or less).
Note that the Soundex algorithm ignores the first
letter of a word. Thus, "won" and "one" will have
different Soundex values, but "Won" and "Wunn" will
have the same values.

Soundex is especially useful in databases when one
does not know how to spell a last name.

Function Soundex(OriginalWord: string): string;
  Tempstring1, Tempstring2: string;
  Count: integer;
  Tempstring1 := '';
  Tempstring2 := '';
  {Make original word uppercase}
  OriginalWord := Uppercase(OriginalWord); 
  Appendstr(Tempstring1, OriginalWord[1]);
  {Use the first letter of the word}
  for Count := 2 to length(OriginalWord) do
    {Assign a numeric value to each letter, 
     except the first}

    case OriginalWord[Count] of
      'B','F','P','V': Appendstr(Tempstring1, '1');
      'Q','S','X','Z': Appendstr(Tempstring1, '2');
      'D','T'        : Appendstr(Tempstring1, '3');
      'L'            : Appendstr(Tempstring1, '4');
      'M','N'        : Appendstr(Tempstring1, '5');
      'R'            : Appendstr(Tempstring1, '6');
    {All other letters, punctuation and 
     numbers are ignored}
  Appendstr(Tempstring2, OriginalWord[1]);
  {Go through the result removing any consecutive 
   duplicate numeric values.}

  for Count:=2 to length(Tempstring1) do
    if Tempstring1[Count-1]<>Tempstring1[Count] then
  Soundex:=Tempstring2; {This is the soundex value}

SoundAlike--pass two strings to this function.
It returns True if they sound alike, False if they 
don't. Simply calls the Soundex function.

Function SoundAlike(Word1, Word2: string): boolean;
  if (Word1 = '') and (Word2 = '') then
    result := True
    if (Word1 = '') or (Word2 = '') then 
     result := False
      if (Soundex(Word1) = Soundex(Word2)) then 
        result := True
        result := False;
{This code came from Lloyd's help file!}
  1. Belum ada komentar.
  1. No trackbacks yet.

Tinggalkan Balasan

Isikan data di bawah atau klik salah satu ikon untuk log in:


You are commenting using your account. Logout /  Ubah )

Foto Google+

You are commenting using your Google+ account. Logout /  Ubah )

Gambar Twitter

You are commenting using your Twitter account. Logout /  Ubah )

Foto Facebook

You are commenting using your Facebook account. Logout /  Ubah )


Connecting to %s

%d blogger menyukai ini: