(* Gestalt string comparison function Web site: http://rumkin.com/reference/algorithms/fuzzy_strings/ License: http://rumkin.com/license/ *) function TfMain.Fuzzy_Simil(padrao, texto: string; tpadrao,ttexto:integer):variant; var max, l, pos1, pos2, sum, end1, end2, cont1, cont2 : integer; p, q : string; begin max := 1; end1 := ttexto + 1; end2 := tpadrao + 1; pos1:=1; pos2:=1; p := texto; cont1:=1; while cont1 < end1 do begin q:=padrao; cont2:=1; while cont2 < end2 do begin l:=1; while (((cont1 + l) - 1) < end1) and (((cont2 + l)-1) < end2) and (p[(cont1+l)-1] = q[(cont2+l)-1]) do inc(l); if (l > max) then begin max := l; pos1 := (length(texto) - (length(texto)-(cont1-1)))+1; pos2 := (length(padrao) - (length(padrao)-(cont2-1)))+1; end; inc(cont2); end; inc(cont1); end; if (max = 1) then Fuzzy_Simil:=0 else begin sum := max; if (pos1>1) and (pos2>1) then sum := sum + Fuzzy_Simil(padrao, texto, pos2-1, pos1-1); if (((pos1 + max - 1) < tpadrao) and ((pos2 + max - 1) < ttexto)) then begin sum := sum + Fuzzy_Simil(copy(padrao,pos2 + max - 1,length(padrao)), copy(texto,pos1 + max - 1,length(texto)), tpadrao - pos2 - max , ttexto - pos1 - max); end; Fuzzy_Simil:=sum-1; end; end; function TfMain.Similar(padrao, texto:string):variant; var resultado:integer; total:variant; begin resultado:=Fuzzy_Simil(padrao, texto, length(padrao), length(texto)); total:=(100*(resultado * 2 / ( length(padrao) + length(texto) ))); Similar:=total; end;