Accueil > > > SOUNDEX 2 FRANCAIS
SOUNDEX 2 FRANCAIS
Information sur la source
Description
Bon ben ceci est une première version de soundex2, un soundex francisé. Il est basé sur un algo décrit ici : http://sqlpro.developpez.com/cours/soundex/ par Frédéric BROUARD. Il n'est pas encore parfait (j'ai dû manquer 2-3 choses), mais il tourne déjà pas mal. Il faudra aussi l'optimiser car il risque d'être gourmand, à la longue...! Soundex est une méthode utilisé pour comparer phonétiquement 2 chaînes. Il existe en natif dans PHP les fonction soundex, ou metaphone, mais elles sont anglicisées. Cette version-ci est francisé, donc prend en compte les spécificités de la langue française. PRECISIONS SUR L'UTILITE : Ces algo, soundex, soundex2, phonex, metaphone, assigne un code à une chaîne donnée. Ce code est calculé en fonction de la phonétique, donc de la prononciation de cette chaîne. En l'occurence, les 2 algo présents par défaut dans php, soundex () et metaphone () ne prennent en compte que la prononciation anglaise. celui-ci, basé sur soundex2 (un algo plus performant que soundex), est francisé, donc prend en compte la prononciation française. Evidemment, 2 chaînes différentes peuvent avoir le même code. Par exemple, ici, 'gros' aura le même code soundex2 que 'grau'. Ce qui veut dire, dans le cadre d'une recherche sur une base de données contenant des noms, par exemple, on peut effecyuer une recherche phonétique aussi! Bref, le mec a parlé avec un cilent par téléphone, mais il ne s'est pas comment s'écrit exactement son nom de famille...gros, graus, grau, graux, greaux...? etc... Il tapoe par exemple gros, et effectue une recherche soundex. Cette recherche lui ressortira tous les noms dont le code soundex est le même que 'gros'. Donc si le client s'appelait 'Graux', il le trouvera. Couplé à l'algo de levenshtein (fonction interne php) en plus, on peut avoir une recherche phonétique par pertinence...les codes identiques en premiers, puis ceux un peu différents, etc...jusqu'à un degré de différence voulu. Le mieux, dans le cadre d'une bdd, est évidemment de stocker le code soundex2 dans la base, histoire de ne pas le recalculer à chaque recherche (pour info, sur ma bécane, 10000 tours de boucle sur le tableau que j'ai mis en exemple dans index.php, donc 10000 * 11 chaînes dont on doit calculer le code soundex2, ça me prend une 20aine de secondes. Et encore, je n'ai pas optimisé l'algo).
Source
- <?php
- /**
- * CLASS soundex2
- * soundex2 French version
- * based on the algorithm described here : http://sqlpro.developpez.com/cours/soundex/ by Frédéric BROUARD
- *
- * author Johan Barbier <barbier_johan@hotmail.com>
- */
- class soundex2 {
-
- /**
- * public sString
- * main string we work on
- */
- public $sString = '';
-
- /**
- * vowels replacement array
- */
- private $aReplaceVoy1 = array (
- 'E' => 'A',
- 'I' => 'A',
- 'O' => 'A',
- 'U' => 'A'
- );
-
- /**
- * consonnants replacement array
- */
- private $aReplaceGrp1 = array (
- 'GUI' => 'KI',
- 'GUE' => 'KE',
- 'GA' => 'KA',
- 'GO' => 'KO',
- 'GU' => 'K',
- 'CA' => 'KA',
- 'CO' => 'KO',
- 'CU' => 'KU',
- 'Q' => 'K',
- 'CC' => 'K',
- 'CK' => 'K'
- );
-
- /**
- * other replacement array
- */
- private $aReplaceGrp2 = array (
- 'ASA' => 'AZA',
- 'KN' => 'NN',
- 'PF' => 'FF',
- 'PH' => 'FF',
- 'SCH' => 'SSS'
- );
-
- /**
- * endings replacement array
- */
- private $aEnd = array (
- 'A',
- 'T',
- 'D',
- 'S'
- );
-
- /**
- * public function build
- * core function of the class, go through the whole process
- * @Param string sString : the string we want to check
- */
- public function build ($sString) {
- /**
- * let's check it's a real string...
- */
- if (is_string ($sString)) {
- $this -> sString = $sString;
- }
- /**
- * remove starting and ending spaces
- */
- $this -> sString = trim ($this -> sString);
- /**
- * remove special french characters
- */
- $this -> trimAccent ();
- /**
- * string to upper case
- */
- $this -> sString = strtoupper ($this -> sString );
- /**
- * let's remove every space in the string
- */
- $this -> sString = str_replace (' ', '', $this -> sString);
- /**
- * let's remove every '-' in the string
- */
- $this -> sString = str_replace ('-', '', $this -> sString);
- /**
- * let's process through the first replacement array
- */
- $this -> arrReplace ($this -> aReplaceGrp1);
- /**
- * let's process through th vowels replacement
- */
- $sChar = substr ($this -> sString, 0, 1);
- $this -> sString = substr ($this -> sString, 1, strlen ($this -> sString) - 1);
- $this -> arrReplace ($this -> aReplaceVoy1);
- $this -> sString = $sChar.$this -> sString;
- /**
- * let's process through the second replacement array
- */
- $this -> arrReplace ($this -> aReplaceGrp2, true);
- /**
- * let's remove every 'H' but those prededed by a 'C' or an 'S'
- */
- $this -> sString = preg_replace ('/(?<![CS])H/', '', $this -> sString);
- /**
- * let's remove every 'Y' but those preceded by an 'A'
- */
- $this -> sString = preg_replace ('/(?<!A)Y/', '', $this -> sString);
- /**
- * remove endings in aEnd
- */
- $length = strlen ($this -> sString) - 1;
- if (in_array ($this -> sString{$length}, $this -> aEnd)) {
- $this -> sString = substr ($this -> sString, 0, $length);
- }
- /**
- * let's remove every 'A', but the one at the beginning of the string, if any.
- */
- $sChar = '';
- if ($this -> sString{0} === 'A') {
- $sChar = 'A';
- }
- $this -> sString = str_replace ('A', '', $this -> sString);
- $this -> sString = $sChar.$this -> sString;
- /**
- * let's have only 1 occurence of each letter
- */
- $this -> sString = preg_replace( '`(.)\1`', '$1', $this -> sString );
- /**
- * let's have the final code : a 4 letters string
- */
- $this -> getFinal ();
- }
-
- /**
- * private function getFinal
- * gets the first 4 letters, pads the string with white space if the string length < 4
- */
- private function getFinal () {
- if (strlen ($this -> sString) < 4) {
- $this -> sString = str_pad ($this -> sString, 4, ' ', STR_PAD_RIGHT);
- } else {
- $this -> sString = substr ($this -> sString, 0, 4);
- }
- }
-
- /**
- * private function trimAccent
- * remove every special French letters
- */
- private function trimAccent () {
- $this -> sString = htmlentities(strtolower($this -> sString ));
- $this -> sString = preg_replace("/&(.)(acute|cedil|circ|ring|tilde|uml|grave);/", "$1", $this -> sString );
- $this -> sString = preg_replace("/([^a-z0-9]+)/", "-", html_entity_decode($this -> sString ));
- $this -> sString = trim($this -> sString , "-");
- }
-
- /**
- * private function arrReplace
- * replacement method, given an array
- * @Param array tab : the replacement array to be used
- * @Param bool pref : if false, just replace keys by values; if true, do the same but only with prefix
- */
- private function arrReplace (array $tab, $pref = false) {
- $fromRep = array_keys ($tab);
- $toRep = array_values ($tab);
- if (false === $pref) {
- $this -> sString = str_replace ($fromRep, $toRep, $this -> sString);
- } else {
- foreach ($fromRep as $clef => $val) {
- $length = strlen ($val);
- if (substr ($this -> sString, 0, $length) === $val) {
- $this -> sString = substr_replace ($this -> sString, $toRep[$clef], 0, $length);
- }
- }
- }
- }
-
- }
- ?>
<?php
/**
* CLASS soundex2
* soundex2 French version
* based on the algorithm described here : http://sqlpro.developpez.com/cours/soundex/ by Frédéric BROUARD
*
* author Johan Barbier <barbier_johan@hotmail.com>
*/
class soundex2 {
/**
* public sString
* main string we work on
*/
public $sString = '';
/**
* vowels replacement array
*/
private $aReplaceVoy1 = array (
'E' => 'A',
'I' => 'A',
'O' => 'A',
'U' => 'A'
);
/**
* consonnants replacement array
*/
private $aReplaceGrp1 = array (
'GUI' => 'KI',
'GUE' => 'KE',
'GA' => 'KA',
'GO' => 'KO',
'GU' => 'K',
'CA' => 'KA',
'CO' => 'KO',
'CU' => 'KU',
'Q' => 'K',
'CC' => 'K',
'CK' => 'K'
);
/**
* other replacement array
*/
private $aReplaceGrp2 = array (
'ASA' => 'AZA',
'KN' => 'NN',
'PF' => 'FF',
'PH' => 'FF',
'SCH' => 'SSS'
);
/**
* endings replacement array
*/
private $aEnd = array (
'A',
'T',
'D',
'S'
);
/**
* public function build
* core function of the class, go through the whole process
* @Param string sString : the string we want to check
*/
public function build ($sString) {
/**
* let's check it's a real string...
*/
if (is_string ($sString)) {
$this -> sString = $sString;
}
/**
* remove starting and ending spaces
*/
$this -> sString = trim ($this -> sString);
/**
* remove special french characters
*/
$this -> trimAccent ();
/**
* string to upper case
*/
$this -> sString = strtoupper ($this -> sString );
/**
* let's remove every space in the string
*/
$this -> sString = str_replace (' ', '', $this -> sString);
/**
* let's remove every '-' in the string
*/
$this -> sString = str_replace ('-', '', $this -> sString);
/**
* let's process through the first replacement array
*/
$this -> arrReplace ($this -> aReplaceGrp1);
/**
* let's process through th vowels replacement
*/
$sChar = substr ($this -> sString, 0, 1);
$this -> sString = substr ($this -> sString, 1, strlen ($this -> sString) - 1);
$this -> arrReplace ($this -> aReplaceVoy1);
$this -> sString = $sChar.$this -> sString;
/**
* let's process through the second replacement array
*/
$this -> arrReplace ($this -> aReplaceGrp2, true);
/**
* let's remove every 'H' but those prededed by a 'C' or an 'S'
*/
$this -> sString = preg_replace ('/(?<![CS])H/', '', $this -> sString);
/**
* let's remove every 'Y' but those preceded by an 'A'
*/
$this -> sString = preg_replace ('/(?<!A)Y/', '', $this -> sString);
/**
* remove endings in aEnd
*/
$length = strlen ($this -> sString) - 1;
if (in_array ($this -> sString{$length}, $this -> aEnd)) {
$this -> sString = substr ($this -> sString, 0, $length);
}
/**
* let's remove every 'A', but the one at the beginning of the string, if any.
*/
$sChar = '';
if ($this -> sString{0} === 'A') {
$sChar = 'A';
}
$this -> sString = str_replace ('A', '', $this -> sString);
$this -> sString = $sChar.$this -> sString;
/**
* let's have only 1 occurence of each letter
*/
$this -> sString = preg_replace( '`(.)\1`', '$1', $this -> sString );
/**
* let's have the final code : a 4 letters string
*/
$this -> getFinal ();
}
/**
* private function getFinal
* gets the first 4 letters, pads the string with white space if the string length < 4
*/
private function getFinal () {
if (strlen ($this -> sString) < 4) {
$this -> sString = str_pad ($this -> sString, 4, ' ', STR_PAD_RIGHT);
} else {
$this -> sString = substr ($this -> sString, 0, 4);
}
}
/**
* private function trimAccent
* remove every special French letters
*/
private function trimAccent () {
$this -> sString = htmlentities(strtolower($this -> sString ));
$this -> sString = preg_replace("/&(.)(acute|cedil|circ|ring|tilde|uml|grave);/", "$1", $this -> sString );
$this -> sString = preg_replace("/([^a-z0-9]+)/", "-", html_entity_decode($this -> sString ));
$this -> sString = trim($this -> sString , "-");
}
/**
* private function arrReplace
* replacement method, given an array
* @Param array tab : the replacement array to be used
* @Param bool pref : if false, just replace keys by values; if true, do the same but only with prefix
*/
private function arrReplace (array $tab, $pref = false) {
$fromRep = array_keys ($tab);
$toRep = array_values ($tab);
if (false === $pref) {
$this -> sString = str_replace ($fromRep, $toRep, $this -> sString);
} else {
foreach ($fromRep as $clef => $val) {
$length = strlen ($val);
if (substr ($this -> sString, 0, $length) === $val) {
$this -> sString = substr_replace ($this -> sString, $toRep[$clef], 0, $length);
}
}
}
}
}
?>
Historique
- 14 mars 2006 16:43:40 :
- Ajout des commentaires dans la classe
- 14 mars 2006 17:29:04 :
- ajout de la documentation générée par mon outil ClassDoc, dans docs/francais/index.html
- 15 mars 2006 12:54:24 :
- ajout de quelques précisions dans la description
- 15 mars 2006 13:33:15 :
- Quelques optimisations
- 15 mars 2006 13:36:00 :
- Modif du titre ;-)
- 15 mars 2006 13:38:21 :
- Me suis trompé de zip ;-)
- 16 mars 2006 09:32:51 :
- Correction d'un bug lors du dédoublonnage
- 16 mars 2006 10:34:54 :
- ajout du calcul de la distance de Levensthein avec affichage par pertinence
- 17 mars 2006 12:48:34 :
- Ajout version PHP4
Sources du même auteur
Sources de la même categorie
RÉCUPÉRER LES MINIATURES D'UNE VIDÉO YOUTUBERÉCUPÉRER LES MINIATURES D'UNE VIDÉO YOUTUBE Le code est simple, il permet depuis une url youtube de récupérer son identifiant et de se connecter au serveur de miniatures pour en récupérer les im...
par tefa24600
CONVERTISSEUR DE NOMBRES EN TEXTECONVERTISSEUR DE NOMBRES EN TEXTEQu'est-ce ? Un convertisseur de nombre en texte.
Ses particularités?
- pas de limitation sur la taille du nombre (traitement en string, et non en ...
par macruz
CODAGE TEXTE >HTML, ISO, SPECIALCHARS, URL ET DECODAGECODAGE TEXTE >HTML, ISO, SPECIALCHARS, URL ET DECODAGECe script php permet, comme son nom l'indique de coder un texte pour remplacer les caractères spéciaux, ou apprendre à comprendre les htmlentities, sp...
par Salva9473
Commentaires et avis
Discussions en rapport avec ce code source dans le forum
Comparaison de date [ par benett ]
J'essaie de créer un site CDThèque et j'ai un gros problème : J'explique :Comment puis-je faire à partir de deux dates (La date de visite du site et l
Comparaison de date [ par benett ]
Bonjour à tous,Comment peut-on comparer 2 dates et extraire la différences en jours.Ces 2 dates sont encodées via un formulaire au format aaaa/aa/aa.C
problème de comparaison de variables string [ par julp ]
je cherche comment savoir si deux variables (en fait ce sont des chaînes) sont égales. Pour l'instant j'ai essayé ceci :if (!($a==$b)):instruc;endif;m
comparaison de chaines [ par darkhorkeu ]
Quelqu'un sait-il si la comparaison de chaine: "str1" == "str2" revient au meme que:!strcmp("str1","str2") merci d'avance
Comparaison chaines de caracteres [ par jdaviaud ]
salut a tous, voila mon gros probleme actuel :je récupère la valeur de la variable d'environnement HTTP_HOST et je veux savoir si c'est le Domaine A o
Comparaison Binaire [ par 6Po ]
Bonjour,J'aimerais effectué une comparaison binaire. 6 = 110 2 = 010 Donc normal 6 & 2 devrait faire 010 (soit 2)... si j'effectue le test suivant if
pb comparaison string [ par fmazoue ]
ca doit etre tout con mais la je vois pas l'erreur je doit etre bigleu !!!voila le bout de code : echo "comparaison entre ".$pwd." et ".$info[$i]["ntp
comparaison d'enregistrements dans 2 tables [ par michelvernet2 ]
bonjour,j'ai une table ETUDIANT composée des variables $A, $B, $C . cette table contient 30 lignes.j'ai une table REPONSES composée de svariables $RA,
une petite aide ! [ par tibo830 ]
salut je voudrai juste que par défaut, la case "France soit cochée" merci :o)<INPUT TYPE="RADIO" NAME="type" VALUE="fr"> France <INPUT TYPE="
soundex [ par psg120 ]
cherche un generateur pour soundex
|
Derniers Blogs
IMAGINE CUP 2012, MAKE A SIGN EN FINALEIMAGINE CUP 2012, MAKE A SIGN EN FINALE par junarnoalg
Voilà qui est fait, la nouvelle est officielle ! L'équipe belge "Make a Sign" va au pays des kangourous défendre son projet dans la catégorie Software Design. http://www.imaginecup.com/CompetitionsContent/Competition/WorldwideFinalists.aspx V...
Cliquez pour lire la suite de l'article par junarnoalg KINECT 1.5 IS OUT !KINECT 1.5 IS OUT ! par Vko
La version 1.5 du Kinect For Microsoft vient tout juste de sortir ! Plein de nouveautés: Tracking de squelette en Near Mode Détection en position assise Détection faciale avec un SDK dédié Documentation et des guideline (enfin) Un out...
Cliquez pour lire la suite de l'article par Vko LES ACTUALITéS DE LA SEMAINE SUR C2I.FR (14 MAI - 20 MAI) LES ACTUALITéS DE LA SEMAINE SUR C2I.FR (14 MAI - 20 MAI) par richardc
Mise à jour des Web API du 14 Mai
Réservez dès maintenant votre journée du 20 juin pour le Windows Azure Dev Camp 2012 à Paris
Mise à jour de Team Foundation Service
MechCommander 2 sur Windows 8
Entity Framework 5 Release Candidate e...
Cliquez pour lire la suite de l'article par richardc REACTIVE EXTENSIONS : CONSOMMER DES SERVICES AVEC RX PARTIE 3, LES PIèGES à éVITERREACTIVE EXTENSIONS : CONSOMMER DES SERVICES AVEC RX PARTIE 3, LES PIèGES à éVITER par Groc
Une mauvaise utilisation de rx lors de l'écriture d'une couche d'accès à des services peut conduire à des cas embarassants avec des erreurs mal gérées, des appels qui ne partent lorsqu'ils le devraient, et même des résultats incorrects . le tout nuis...
Cliquez pour lire la suite de l'article par Groc SHAREPOINT BLOG SITE, PROBLèME D'ARCHIVESSHAREPOINT BLOG SITE, PROBLèME D'ARCHIVES par junarnoalg
Dernièrement, nous avons migré le site
myTIC
vers un nouveau serveur SharePoint 2010. Dans les contenus que nous vouloins récupérer, nous avions un certain nombre de blogs.
Nous avons utilisé les commandes Power...
Cliquez pour lire la suite de l'article par junarnoalg
Logiciels
sDEVIS-FACTURES vlPRO (8.1.0.3)SDEVIS-FACTURES VLPRO (8.1.0.3)sDEVIS-FACTURES vlPRO a été mis au point pour les particuliers, créateurs, entrepreneurs, artisa... Cliquez pour télécharger sDEVIS-FACTURES vlPRO 974 Application Server (12.2.4.6)974 APPLICATION SERVER (12.2.4.6)Développez de puissantes applications dans un environnement de 'cloud computing', clusterisé, séc... Cliquez pour télécharger 974 Application Server vPicture (1.4.2.1)VPICTURE (1.4.2.1)Avec vPicture, hébergez vos images facilement et rapidement.
vPicture est un utilitaire simple, ... Cliquez pour télécharger vPicture Easy-Planning (2.2.1.6)EASY-PLANNING (2.2.1.6)Easy-Planning permet de créer des plannings sous la représentation de diagrammes et est adapté au... Cliquez pour télécharger Easy-Planning COM-BACKUP (2.0)COM-BACKUP (2.0)
COM-BACKUP est un logiciel de sauvegarde qui permet de planifier les sauvegardes de vos dossiers ...
Cliquez pour télécharger COM-BACKUP
|