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 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572
| /*
* Ce fichier contient le code qui ajoute les deux fonctions suivantes
* au serveur MySQL:
*
* PHONEX2S("une chaine") -> retourne un chaine correspondant à la valeur phonétique du paramètre
* PHONEX2R("une chaine") -> retourne un réel
*
* L'utilisation d'une ou l'autre des fonctions dépend de votre compromis
* entre lisbilité et performance/taille des tables.
*
* Intégration dans MySQL:
*
* mysql> CREATE FUNCTION phonex2s RETURNS STRING SONAME "phonex2.so";
* mysql> CREATE FUNCTION phonex2r RETURNS STRING SONAME "phonex2.so";
*
* Suppression de MySQL:
*
* mysql> DROP FUNCTION phonex2s;
* mysql> DROP FUNCTION phonex2r;
*/
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <math.h>
#include <mysql.h>
#ifdef WITH_MAIN
static int debug = 0;
#else
static int debug = 0;
#endif
/* taille max en car. d'une conversion de chaine en son phonex: */
#define PHONEX_MAXSZ 512
/* différents drapeaux qui influent sur l'algorithme: */
#define FLAG_NORMAL 0x0000
#define FLAG_EFINAL 0x0001
#define FLAG_EACCENT 0x0002
/* liste des caractères autorisés dans la conversion en phonex: */
static unsigned char *finals = "12345efghiklnorstuwxyz";
static void phonex2(unsigned char *s, unsigned int flag);
/*
* Interface avec MySQL:
*/
my_bool phonex2s_init(UDF_INIT *initid, UDF_ARGS *args, char *message)
{
initid->maybe_null = 0;
if (args->arg_count < 1 || args->arg_count > 3) {
strcpy(message,"PHONEX2S syntax is PHONEX2S(string[,flag[,maxsize]]) with maxsize <= 512");
return 1;
}
if (args->arg_type[0] != STRING_RESULT) {
strcpy(message,"PHONEX2S needs a STRING Parameter");
return 1;
}
if (args->arg_count >= 2 &&args->arg_type[1] != INT_RESULT) {
strcpy(message,"Second Parameter of PHONEX2S must be an INT (the flag)");
return 1;
}
if (args->arg_count >= 3) {
if (args->arg_type[2] != INT_RESULT) {
strcpy(message,"Third Parameter of PHONEX2S must be an INT (the maximum size)");
return 1;
}
if (*((long long *)args->args[2]) > 512) {
strcpy(message,"Third Parameter of PHONEX2S must be <= 512");
return 1;
}
}
return 0;
}
void phonex2s_deinit(UDF_INIT *initid)
{
}
my_bool phonex2r_init(UDF_INIT *initid, UDF_ARGS *args, char *message)
{
initid->maybe_null = 0;
if (args->arg_count < 1 || args->arg_count > 2) {
strcpy(message,"PHONEX2R syntax is PHONEX2R(string[,flag])");
return 1;
}
if (args->arg_type[0] != STRING_RESULT) {
strcpy(message,"PHONEX2R needs a STRING Parameter");
return 1;
}
if (args->arg_count >= 2 && args->arg_type[1] != INT_RESULT) {
strcpy(message,"Second Parameter of PHONEX2R must be an INT (the flag)");
return 1;
}
return 0;
}
void phonex2r_deinit(UDF_INIT *initid)
{
}
/*
* Implémentation:
*/
char *phonex2s( UDF_INIT *initid,
UDF_ARGS *args,
char *result,
unsigned long *length,
char *is_null,
char *error)
{
unsigned int flag = 0;
unsigned int size = 0;
float res;
unsigned int len = args->lengths[0];
unsigned char *copy = (unsigned char *)malloc(len+1);
memcpy(copy,args->args[0],len);
copy[len] = '\0';
if (args->arg_count > 1) {
flag = *((long long *)args->args[1]);
}
if (args->arg_count > 2) {
size = *((long long *)args->args[2]);
}
phonex2(copy,flag);
len = strlen(copy);
if (size) *length = (size < len) ? size : len;
else *length = len;
memcpy(result,copy,*length);
*is_null = *error = 0;
free(copy);
return result;
}
double phonex2r(UDF_INIT *initid,
UDF_ARGS *args,
char *is_null,
char *error)
{
unsigned int flag = 0;
float phonex = 0;
int indice = 0;
int base = strlen(finals);
unsigned int len = args->lengths[0];
unsigned char *p,*copy = (unsigned char *)malloc(len+1);
memcpy(copy,args->args[0],len);
copy[len] = '\0';
if (args->arg_count > 1) {
flag = *((long long *)args->args[1]);
}
phonex2(copy,flag);
*is_null = *error = 0;
/* chiffrement numerique en ignorant les car. non autorises: */
p = copy;
indice = 0;
while (*p) {
unsigned char *rank = strchr(finals,*p);
/* normalement tous les car. sont corrects */
phonex += (rank-finals) * powf(indice,base);
indice++; p++;
}
free(copy);
return phonex;
}
/*
* Note: l'écriture 'p+2' est sure car si 'p+2' pointe au-delà du dernier
* caractère de la chaine, on ne recopiera jamais après le '\0' final
*
* Les opérations doivent être faites dans le bon ordre (par exemple:
* ph->f avant traitement du 'h' !)
*
* Le contenu de la chaine passée en paramètre est modifié. C'est la
* responsabilité de la fonction appelante de passer une copie de
* l'original si nécessaire. Le contenu de la chaine retournée a une
* longueur de 512 caractères maximum.
*/
static void phonex2(unsigned char *copy, unsigned int flag)
{
unsigned char *p,achar;
unsigned char *voyelles1 = "aeiouy";
unsigned char *voyelles2 = "aeiouy1234";
/* Cas trivial de la chaine vide: */
if (!*copy) return;
/*
* R1
* - remplace les accents et autres caracteres "bizarres".
* - le Y/y devient i
* - la cédille devient 'ss'
* - les autres cars. sont mis en minuscules
*/
p = copy;
while (*p) {
if ((*p >= 0xE0 && *p <= 0xE6) || (*p >= 0xC0 && *p <= 0xC6)) { *p++ = 'a'; continue; }
if ((*p >= 0xE8 && *p <= 0xEB) || (*p >= 0xC8 && *p <= 0xCB)) { *p++ = 'e'; continue; }
if ((*p >= 0xD2 && *p <= 0xD6) || (*p >= 0xF2 && *p <= 0xF6)) { *p++ = 'o'; continue; }
if ((*p >= 0xCC && *p <= 0xCF) || (*p >= 0xEC && *p <= 0xEF)) { *p++ = 'i'; continue; }
if ((*p >= 0xF9 && *p <= 0xFC) || (*p >= 0xD9 && *p <= 0xDC)) { *p++ = 'u'; continue; }
if (*p == 'Y' || *p == 'y') { *p++ = 'i'; continue; }
if (*p == 0xC7 || *p == 0xE7) {
*p++ = 's';
memmove(p,p+1,strlen(p));
*p++ = 's';
continue;
}
*p++ = tolower(*p);
}
if (debug) printf("S1(accents): %s\n",copy);
/*
* R2
* Remplace 'ph' par 'f': */
p = copy;
while (*p) {
if (*p == 'p' && *(p+1) == 'h') {
*p = 'f';
memmove(p+1,p+2,strlen(p+1));
}
*p++;
}
if (debug) printf("S2(ph->f): %s\n",copy);
/*
* R3
* Supprime les 'h' muets, c'est à dire, les 'h' qui ne sont pas précédés
* ni d'un 'c', ni d'un 's':
*/
p = copy;
while (*p) {
if (*p == 'h') {
if (p == copy) {
/* 'h' initial: */
memmove(copy,p+1,strlen(copy));
continue;
}
if (*(p-1) != 'c' && *(p-1) != 's') {
memmove(p,p+1,strlen(p));
continue;
}
}
p++;
}
if (debug) printf("S3(h): %s\n",copy);
/*
* R4
* Remplacement de 'g' par 'k' devant 'an/am/ain/aim': */
p = copy;
while (*p) {
if (*p != 'g' || *(p+1) != 'a') { p++; continue; }
if (*(p+2) == 'n' || *(p+2) == 'm' ) {
*p++ = 'k';
continue;
}
if (*(p+2) == 'i') {
if (*(p+3) == 'n' || *(p+3) == 'm' ) {
*p++ = 'k';
continue;
}
}
p++;
}
if (debug) printf("S4(g->k): %s\n",copy);
/* R5 - remplacement de aina,eina,aima,eima par yna
* - remplacement de aine,eine,aime,eime par yne
* - remplacement de aini,eini,aimi,eimi par yni
* - remplacement de aino,eino,aimo,eimo par yno
* - remplacement de ainu,einu,aimu,eimu par ynu
*/
p = copy;
while (*p) {
unsigned char v;
if (*p != 'a' && *p != 'e') { p++; continue; }
if (*(p+1) != 'i') { p++; continue; }
if (*(p+2) != 'n' && *(p+2) != 'm') { p++; continue; }
v = *(p+3);
if (!*(p+3) || !strchr(voyelles1,v)) { p++; continue; }
*p = 'y';
*(p+1) = 'n';
*(p+2) = v;
memmove(p+3,p+4,strlen(p+3));
p++;
}
if (debug) printf("S5(aine): %s\n",copy);
/* R6 - remplacement de eau par o
* - remplacement de oua par 2
* - remplacement de ein par 4
* - remplacement de ain par 4
*/
p = copy;
while (*p) {
if (*p == 'e' && *(p+1) == 'a' && *(p+2) == 'u') {
*p++ = 'o'; memmove(p,p+2,strlen(p+1)); continue;
}
if (*p == 'o' && *(p+1) == 'u' && *(p+2) == 'a') {
*p++ = '2'; memmove(p,p+2,strlen(p+1)); continue;
}
if (*p == 'e' && *(p+1) == 'i' && *(p+2) == 'n') {
*p++ = '4'; memmove(p,p+2,strlen(p+1)); continue;
}
if (*p == 'a' && *(p+1) == 'i' && *(p+2) == 'n') {
*p++ = '4'; memmove(p,p+2,strlen(p+1)); continue;
}
p++;
}
if (debug) printf("S6(eau): %s\n",copy);
/* R7 - remplacement de ai par y
* - remplacement de ei par y
* - remplacement de ee par y
* - remplacement de er par yr
* - remplacement de ess par yss
* - remplacement de et par yt
* - remplacement de ez par yz
*/
p = copy;
while (*p) {
if (*p == 'a' && *(p+1) == 'i') {
*p++ = 'y'; memmove(p,p+1,strlen(p)); continue;
}
if (*p == 'e' && *(p+1) == 'i') {
*p++ = 'y'; memmove(p,p+1,strlen(p)); continue;
}
if (*p == 'e' && *(p+1) == 'e') {
*p++ = 'y'; memmove(p,p+1,strlen(p)); continue;
}
if (*p == 'e' && (*(p+1) == 'r' || *(p+1) == 't' || *(p+1) == 'z')) {
*p++ = 'y'; continue;
}
if (*p == 'e' && *(p+1) == 's' && *(p+2) == 's') {
*p++ = 'y'; continue;
}
p++;
}
if (debug) printf("S7(er->yr): %s\n",copy);
/* R8 suppression des lettres doubles: */
p = copy;
while (*p) {
if (*p == *(p+1)) { memmove(p,p+1,strlen(p)); p--; }
p++;
}
if (debug) printf("S15(nn): %s\n",copy);
/* R9 - remplacement de an par 1
* - remplacement de am par 1
* - remplacement de en par 1
* - remplacement de em par 1
* - remplacement de in par 4
* à condition que ces modèles ne soient ni suivis d'une voyelle
* ni d'un son 1 à 4
*/
p = copy;
while (*p) {
if ((*p == 'a' || *p == 'e') && (*(p+1) == 'n' || *(p+1) == 'm')) {
if (!*(p+2) || !strchr(voyelles2,*(p+2))) {
*p++ = '1'; memmove(p,p+1,strlen(p)); continue;
}
}
if (*p == 'i' && *(p+1) == 'n') {
if (!*(p+2) || !strchr(voyelles2,*(p+2))) {
*p++ = '4'; memmove(p,p+1,strlen(p)); continue;
}
}
p++;
}
if (debug) printf("S8(an->1): %s\n",copy);
/* R10 remplacement du 'z' par 's' s'il est précédé (ou en tête de mot) et suivi d'une voyelle
* ou d'un son 1 à 4:
*/
p = copy;
if (*p == 'z' && *(p+1) && strchr(voyelles2,*(p+1))) *p = 's';
while (*p) {
if (*p == 'z' && *(p+1)) {
unsigned char v1 = *(p-1);
unsigned char v2 = *(p+1);
if (strchr(voyelles2,v1) && v2 && strchr(voyelles2,v2)) {
*p = 's';
}
}
p++;
}
if (debug) printf("S9(z->s): %s\n",copy);
/* R11 - remplacement de oe par e
* - remplacement de eu par e
* - remplacement de au par o
* - remplacement de oi par 2
* - remplacement de ou par 3
*/
p = copy;
while (*p) {
if (*p == 'o' && *(p+1) == 'e') { *p++ = 'e'; memmove(p,p+1,strlen(p)); continue; }
if (*p == 'e' && *(p+1) == 'u') { *p++ = 'e'; memmove(p,p+1,strlen(p)); continue; }
if (*p == 'a' && *(p+1) == 'u') { *p++ = 'o'; memmove(p,p+1,strlen(p)); continue; }
if (*p == 'o' && *(p+1) == 'i') { *p++ = '2'; memmove(p,p+1,strlen(p)); continue; }
if (*p == 'o' && *(p+1) == 'u') { *p++ = '3'; memmove(p,p+1,strlen(p)); continue; }
p++;
}
if (debug) printf("S10(oe/oi): %s\n",copy);
/* R12 - remplacement de ch par 5
* - remplacement de sch par 5
* - remplacement de sh par 5
* - remplacement de ss par s
* - remplacement de sc par s si suivi d'un "e" ou d'un "i"
*/
p = copy;
while (*p) {
if (*p == 'c' && *(p+1) == 'h') { *p++ = '5'; memmove(p,p+1,strlen(p)); continue; }
if (*p == 's' && *(p+1) == 'h') { *p++ = '5'; memmove(p,p+1,strlen(p)); continue; }
if (*p == 's' && *(p+1) == 's') { p++; memmove(p,p+1,strlen(p)); continue; }
if (*p == 's' && *(p+1) == 'c' && *(p+2) == 'h') { *p++ = '5'; memmove(p,p+2,strlen(p+1)); continue; }
if (*p == 's' && *(p+1) == 'c' && (*(p+2) == 'e' || *(p+2) == 'i') ) { p++; memmove(p,p+1,strlen(p)); continue; }
p++;
}
if (debug) printf("S11(ch->5): %s\n",copy);
/* R13 remplacement du 'c' par un 's' s'il est suivi d'un e/i: */
p = copy;
while (*p) {
if (*p == 'c' && (*(p+1) == 'e' || *(p+1) == 'i')) {
*p = 's';
}
p++;
}
if (debug) printf("S12(c->s): %s\n",copy);
/* R14 - remplacement de c par k
* - remplacement de q par k
* - remplacement de qu par k
* - remplacement de gu par k
* - remplacement de ga par ka
* - remplacement de go par ko
*/
p = copy;
while (*p) {
if (*p == 'c' || *p == 'q') { *p++ = 'k'; continue; }
if ((*p == 'q' || *p == 'g') && *(p+1) == 'u') { *p++ = 'k'; memmove(p,p+1,strlen(p)); continue; }
if (*p == 'g' && (*(p+1) == 'a' || *(p+1) == 'o')) { *p++ = 'k'; continue; }
p++;
}
if (debug) printf("S13(c->k): %s\n",copy);
/* R15 - remplacement de a par o
* - remplacement de d et p par t
* - remplacement de j par g
* - remplacement de b et v par f
* - remplacement de m par n
*/
p = copy;
while (*p) {
if (*p == 'a') { *p++ = 'o'; continue; }
if (*p == 'd' || *p == 'p') { *p++ = 't'; continue; }
if (*p == 'j') { *p++ = 'g'; continue; }
if (*p == 'b' || *p == 'v') { *p++ = 'f'; continue; }
if (*p == 'm') { *p++ = 'n'; continue; }
p++;
}
if (debug) printf("S14(d->t): %s\n",copy);
/* R16 Option: convertit les 'y' en 'e': */
if (!(flag & FLAG_EACCENT)) {
p = copy;
while (*p) {
if (*p == 'y') *p = 'e';
p++;
}
if (debug) printf("S17(y->e): %s\n",copy);
}
/* R17 - suppression des finales t,x,s,z
* - suppression du e final après une consonne
* (fait en deux fois pour eliminer les finales -ez par exemple)
*/
if (strlen(copy) > 1) {
achar = copy[strlen(copy)-1];
if (achar == 'x' || achar == 't' || achar == 's' || achar == 'z') {
copy[strlen(copy)-1] = '\0';
}
}
if (!(flag & FLAG_EFINAL)) {
if (strlen(copy) > 1) {
achar = copy[strlen(copy)-1];
if (achar == 'e' && !strchr(voyelles1,copy[strlen(copy)-2])) {
copy[strlen(copy)-1] = '\0';
}
}
if (debug) printf("S16(txsz): %s\n",copy);
}
/* R18 suppression des lettres doubles (2eme fois): */
p = copy;
while (*p) {
if (*p == *(p+1)) { memmove(p,p+1,strlen(p)); p--; }
p++;
}
if (debug) printf("S15(nn): %s\n",copy);
/* R19 Ne retourne que les caractères autorisés avec un
* maximum de 512 car. dans la chaine:
*/
p = copy;
while (*p && (p-copy) < PHONEX_MAXSZ) {
if (!strchr(finals,*p)) {
memmove(p,p+1,strlen(p));
continue;
}
p++;
}
*p = '\0';
}
#ifdef WITH_MAIN
void Check(char *s1, char *s2)
{
char buff1[64],buff2[64];
printf("Compare %s and %s: ",s1,s2);
strcpy(buff1,s1); strcpy(buff2,s2);
phonex2(buff1,0); phonex2(buff2,0);
printf("%s\n",(strcmp(buff1,buff2)) ? "no match" : "match");
}
int main(int argc, char *argv[])
{
char buff[64];
int flag;
if (argc > 1 && argv[1][1] == 'd') debug = 1;
/* tests automatiques: */
Check("phaure","fort");
Check("elephant","ellefan");
Check("arrivez","arivee");
printf("flag: ");
scanf("%d",&flag);
/* tests interactifs: */
for (;;) {
printf("Chaine: ");
scanf("%s",buff);
phonex2(buff,flag);
printf("phonex=%s\n",buff);
}
return 0;
}
#endif
/* EOF */ |
Partager