1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207
| // soundex2 francisé par Frédéric BROUARD
function soundex2(sIn : string) : sound;
type
TabloVoyell = array[1..4] of char;
TabloCombi1 = array[1..11,1..2] of string;
TabloCombi2 = array[1..5,1..2] of string;
Const
Voyelle : TabloVoyell =
('E',
'I',
'O',
'U');
Combin1 : TabloCombi1 =
(('GUI','KI'),
('GUE','KE'),
('GA','KA'),
('GO','KO'),
('GU','K'),
('CA','KA'),
('CO','KO'),
('CU','KU'),
('Q','K'),
('CC','K'),
('CK','K'));
Combin2 : TabloCombi2 =
(('ASA','AZA'),
('KN','NN'),
('PF','FF'),
('PH','FF'),
('SCH','SSS'));
var
i : integer; // indice de boucle
lSin : integer; // longueur de la chaîne d'entrée
prfx : string; // préfixe
sIn2 : string; // sIn moins la première lettre
let : string; // lettre
begin
// cas trivial : la chaîne est vide
if sIn = ''
then
begin
result := ' ';
exit;
end;
// on prepare la chaine : étapes 1, 2 et 3
sIn := prepare(sIn);
lSin := length(sIn);
// traitement du second effet de bord : chaîne de longeur 1
if lSin = 1
then
begin
result := sIn+' ';
exit;
end;
// étapes 1, 2, 3 et 4: élimine les blancs, met en majuscule,
// convertit les accents et le c cédille
sIn := prepare(sIn);
// étape 5 : on remplace les consonnances primaires
for i := 1 to 4
do
sIn := SearchReplace(sIn,Combin1[i,1],Combin1[i,2]);
// étape 6 : on remplace les voyelles sauf le Y et sauf la première par A
lSin := length(sIn);
sIn2 := copy(sIn,2,lSin-1);
for i := 1 to 4
do
sIn2 := SearchReplace(sIn2,Voyelle[i],'A');
sIn := sIn[1]+sIn2;
// étape 7 : on remplace les préfixes
lSin := length(sIn);
if lSin>=2
then
begin
prfx := copy(sIn,1,2);
if (prfx = 'KN')
then
prfx := 'NN';
if (prfx = 'PH') or (prfx = 'PF')
then
prfx := 'FF';
if lSin = 2
then
sIn := prfx
else
sIn := prfx+copy(sIn,3,lSin-2);
end;
if lSin>=3
then
begin
prfx := copy(sIn,1,3);
if (prfx = 'MAC')
then
prfx := 'MCC';
if (prfx = 'SCH')
then
prfx := 'SSS';
if (prfx = 'ASA')
then
prfx := 'AZA';
if lSin = 3
then
sIn := prfx
else
sIn := prfx+copy(sIn,4,lSin-3);
end;
// étape 8 : on conserve la première lettre et on fait
// les remplacements complémentaires
sIn2 := copy(Sin,2,lSin-1);
for i := 1 to 5
do
sIn2 := SearchReplace(sIn2,Combin2[i,1],Combin2[i,2]);
sIn := sIn[1]+sIn2;
// étape 9 : suppression des H sauf CH ou SH
lSin := length(sIn);
sIn2 := '';
for i := 1 to lSin
do
// pas de H on conserve la lettre
if (sIn[i] <> 'H')
then
begin
sIn2 := SIn2+sIn[i];
continue;
end
else
// le H est précédé d'un S ou d'un C on le conserve
if (i>1) and ((sIn[i-1] = 'C') or (sIn[i-1] = 'S'))
then
sIn2 := Sin2+sIn[i];
sIn := Sin2;
lSin := length(sIn);
// étape 10 : suppression des Y sauf précédés d'un A
lSin := length(sIn);
sIn2 := '';
for i := 1 to lSin
do
// pas de Y on conserve la lettre
if (sIn[i] <> 'Y')
then
begin
sIn2 := SIn2+sIn[i];
continue;
end
else
// le Y est précédé d'un A on le conserve
if (sIn[i-1] = 'A')
then
sIn2 := Sin2+sIn[i];
sIn := Sin2;
lSin := length(sIn);
// étape 11 : on supprime les terminaisons A, T, D, S
let := copy(sIn,lSin,1);
if (let = 'A') or (let = 'D') or(let = 'S') or (let = 'T')
then
sIn := copy(sIn,1,lSin-1);
// étape 12 : suppression de tous les A sauf en tête
lSin := length(sIn);
sIn2 := copy(sIn,1,1);
for i := 2 to lSin
do
// pas de A on conserve la lettre
if (sIn[i] <> 'A')
then
begin
sIn2 := sIn2+sIn[i];
continue;
end;
sIn := Sin2;
lSin := length(sIn);
// étape 13 : on supprime les lettres répétitives
let := copy(sIn,1,1);
sIn2 := let;
for i := 2 to lSin
do
begin
if sIn[i] = let
then
continue;
let := sIn[i];
Sin2 := Sin2 + sIn[i];
end;
sIn := sIn2;
// étape 14 : on ne retient que 4 caractères ou on complète avec des blancs
while length(sIn) < 4
do
Sin := Sin+' ';
if length(sIn) > 4
then
sIn := copy(sIn,1,4);
result := sIn;
end; |
Partager