Accueil > > > PHONEX
PHONEX
Information sur la source
Description
Tout comme soundex2, ceci est une adaptation php de l'algo phonex qui a été créé, et décrit ici : http://sqlpro.developpez.com/cours/soundex/ par Frédéric BROUARD. 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 phonex (un algo plus performant que soundex ou soundex2), est francisé. Evidemment, 2 chaînes différentes peuvent avoir le même code. Par exemple, ici, 'gros' aura le même code phonex 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 tape par exemple gros, et effectue une recherche phonex. Cette recherche lui ressortira tous les noms dont le code phonex 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 phonex dans la base, histoire de ne pas le recalculer à chaque recherche.
Source
- <?php
- /**
- * CLASS phonex
- * phonex, phonetics search algo
- * 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 phonex {
-
- /**
- * The public string we will work on
- */
- public $sString = '';
-
- /**
- * private replacement array
- */
- private $aReplaceGrp1 = array (
- 'gan' => 'kan',
- 'gam' => 'kam',
- 'gain' => 'kain',
- 'gaim' => 'kaim'
- );
- /**
- * private replacement array
- */
- private $aReplaceGrp2 = array (
- '/(ain)([aeiou])/' => 'yn$2',
- '/(ein)([aeiou])/'=> 'yn$2',
- '/(aim)([aeiou])/' => 'yn$2',
- '/(eim)([aeiou])/'=> 'yn$2',
- );
- /**
- * private replacement array
- */
- private $aReplaceGrp3 = array (
- 'eau' => 'o',
- 'oua' => '2',
- 'ein' => '4',
- 'ain' => '4',
- 'eim' => '4',
- 'aim' => '4'
- );
- /**
- * private replacement array
- */
- private $aReplaceGrp4 = array (
- 'é' => 'y',
- 'è' => 'y',
- 'ê' => 'y',
- 'ai' => 'y',
- 'ei' => 'y',
- 'er' => 'yr',
- 'ess' => 'yss',
- 'et' => 'yt'
- );
- /**
- * private replacement array
- */
- private $aReplaceGrp5 = array (
- '/(an)($|[^aeiou1234])/' => '1$2',
- '/(am)($|[^aeiou1234])/' => '1$2',
- '/(en)($|[^aeiou1234])/' => '1$2',
- '/(em)($|[^aeiou1234])/' => '1$2',
- '/(in)($|[^aeiou1234])/' => '4$2'
- );
- /**
- * private replacement array
- */
- private $aReplaceGrp6 = array (
- '/([aeiou1234])(s)([aeiou1234])/' => '$1z$3'
- );
- /**
- * private replacement array
- */
- private $aReplaceGrp7 = array (
- 'oe' => 'e',
- 'eu' => 'e',
- 'au' => 'o',
- 'oi' => '2',
- 'oy' => '2',
- 'ou' => '3'
- );
- /**
- * private replacement array
- */
- private $aReplaceGrp8 = array (
- 'ch' => '5',
- 'sch' => '5',
- 'sh' => '5',
- 'ss' => 's',
- 'sc' => 's'
- );
- /**
- * private replacement array
- */
- private $aReplaceGrp9 = array (
- '/(c)([ei])/' => 's$2'
- );
- /**
- * private replacement array
- */
- private $aReplaceGrp10 = array (
- 'c' => 'k',
- 'q' => 'k',
- 'qu' => 'k',
- 'gu' => 'k',
- 'ga' => 'ka',
- 'go' => 'ko',
- 'gy' => 'ky'
- );
- /**
- * private replacement array
- */
- private $aReplaceGrp11 = array (
- 'a' => 'o',
- 'd' => 't',
- 'p' => 't',
- 'j' => 'g',
- 'b' => 'f',
- 'v' => 'f',
- 'm' => 'n'
- );
- /**
- * private replacement array
- */
- private static $aReplaceGrp12 = array (
- '1',
- '2',
- '3',
- '4',
- '5',
- 'e',
- 'f',
- 'g',
- 'h',
- 'i',
- 'k',
- 'l',
- 'n',
- 'o',
- 'r',
- 's',
- 't',
- 'u',
- 'w',
- 'x',
- 'y',
- 'z'
- );
- /**
- * private replacement array
- */
- private $aEnd = array (
- 't',
- 'x'
- );
-
- /**
- * public function build ()
- * main method, building the phonex code of a given string
- * @Param string sString : the string!
- */
- public function build ($sString) {
- if (is_string ($sString)) {
- $this -> sString = $sString;
- }
- $this -> sString = strtolower ($this -> sString);
- $this -> sString = str_replace ('y', 'i', $this -> sString);
- $this -> sString = preg_replace ('/(?<![csp])h/', '', $this -> sString);
- $this -> sString = str_replace ('ph', 'f', $this -> sString);
- $this -> aReplace ($this -> aReplaceGrp1);
- $this -> aReplace ($this -> aReplaceGrp2, true);
- $this -> aReplace ($this -> aReplaceGrp3);
- $this -> aReplace ($this -> aReplaceGrp4);
- $this -> aReplace ($this -> aReplaceGrp5, true);
- $this -> aReplace ($this -> aReplaceGrp6, true);
- $this -> aReplace ($this -> aReplaceGrp7);
- $this -> aReplace ($this -> aReplaceGrp8);
- $this -> aReplace ($this -> aReplaceGrp9, true);
- $this -> aReplace ($this -> aReplaceGrp10);
- $this -> aReplace ($this -> aReplaceGrp11);
- $this -> sString = preg_replace( '`(.)\1`', '$1', $this -> sString );
- $this -> trimLast ();
- $this -> getNum ();
- }
-
- /**
- * private function aReplace ()
- * method used to replace letters, given an array
- * @Param array aTab : the replacement array to be used
- * @Param bool bPreg : is the array an array of regular expressions patterns : true => yes`| false => no
- */
- private function aReplace (array $aTab, $bPreg = false) {
- if (false === $bPreg) {
- $this -> sString = str_replace (array_keys ($aTab), array_values ($aTab), $this -> sString);
- } else {
- $this -> sString = preg_replace (array_keys ($aTab), array_values ($aTab), $this -> sString);
- }
- }
-
- /**
- * private function trimLast ()
- * method to trim the bad endings
- */
- private function trimLast () {
- $length = strlen ($this -> sString) - 1;
- if (in_array ($this -> sString{$length}, $this -> aEnd)) {
- $this -> sString = substr ($this -> sString, 0, $length);
- }
- }
-
- /**
- * private static function mapNum ()
- * callback method to create the phonex numeric code, base 22
- * @Param int val : current value
- * @Param int clef : current key
- * @Returns int num : the calculated base 22 value
- */
- private static function mapNum ($val, $clef) {
- $num = array_search ($val, self::$aReplaceGrp12);
- $num *= pow (22, - ($clef + 1));
- return $num;
- }
-
- /**
- * private function getNum ()
- * method to get a numeric array from the main string
- * we call the callback function mapNum and we add every values of the obtained array to get the final phonex code
- */
- private function getNum () {
- $aString = str_split ($this -> sString);
- $aNum = array_map (array ('self', 'mapNum'), array_values ($aString), array_keys ($aString));
- $this -> sString = (string) array_sum ($aNum);
- }
- }
- ?>
<?php
/**
* CLASS phonex
* phonex, phonetics search algo
* 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 phonex {
/**
* The public string we will work on
*/
public $sString = '';
/**
* private replacement array
*/
private $aReplaceGrp1 = array (
'gan' => 'kan',
'gam' => 'kam',
'gain' => 'kain',
'gaim' => 'kaim'
);
/**
* private replacement array
*/
private $aReplaceGrp2 = array (
'/(ain)([aeiou])/' => 'yn$2',
'/(ein)([aeiou])/'=> 'yn$2',
'/(aim)([aeiou])/' => 'yn$2',
'/(eim)([aeiou])/'=> 'yn$2',
);
/**
* private replacement array
*/
private $aReplaceGrp3 = array (
'eau' => 'o',
'oua' => '2',
'ein' => '4',
'ain' => '4',
'eim' => '4',
'aim' => '4'
);
/**
* private replacement array
*/
private $aReplaceGrp4 = array (
'é' => 'y',
'è' => 'y',
'ê' => 'y',
'ai' => 'y',
'ei' => 'y',
'er' => 'yr',
'ess' => 'yss',
'et' => 'yt'
);
/**
* private replacement array
*/
private $aReplaceGrp5 = array (
'/(an)($|[^aeiou1234])/' => '1$2',
'/(am)($|[^aeiou1234])/' => '1$2',
'/(en)($|[^aeiou1234])/' => '1$2',
'/(em)($|[^aeiou1234])/' => '1$2',
'/(in)($|[^aeiou1234])/' => '4$2'
);
/**
* private replacement array
*/
private $aReplaceGrp6 = array (
'/([aeiou1234])(s)([aeiou1234])/' => '$1z$3'
);
/**
* private replacement array
*/
private $aReplaceGrp7 = array (
'oe' => 'e',
'eu' => 'e',
'au' => 'o',
'oi' => '2',
'oy' => '2',
'ou' => '3'
);
/**
* private replacement array
*/
private $aReplaceGrp8 = array (
'ch' => '5',
'sch' => '5',
'sh' => '5',
'ss' => 's',
'sc' => 's'
);
/**
* private replacement array
*/
private $aReplaceGrp9 = array (
'/(c)([ei])/' => 's$2'
);
/**
* private replacement array
*/
private $aReplaceGrp10 = array (
'c' => 'k',
'q' => 'k',
'qu' => 'k',
'gu' => 'k',
'ga' => 'ka',
'go' => 'ko',
'gy' => 'ky'
);
/**
* private replacement array
*/
private $aReplaceGrp11 = array (
'a' => 'o',
'd' => 't',
'p' => 't',
'j' => 'g',
'b' => 'f',
'v' => 'f',
'm' => 'n'
);
/**
* private replacement array
*/
private static $aReplaceGrp12 = array (
'1',
'2',
'3',
'4',
'5',
'e',
'f',
'g',
'h',
'i',
'k',
'l',
'n',
'o',
'r',
's',
't',
'u',
'w',
'x',
'y',
'z'
);
/**
* private replacement array
*/
private $aEnd = array (
't',
'x'
);
/**
* public function build ()
* main method, building the phonex code of a given string
* @Param string sString : the string!
*/
public function build ($sString) {
if (is_string ($sString)) {
$this -> sString = $sString;
}
$this -> sString = strtolower ($this -> sString);
$this -> sString = str_replace ('y', 'i', $this -> sString);
$this -> sString = preg_replace ('/(?<![csp])h/', '', $this -> sString);
$this -> sString = str_replace ('ph', 'f', $this -> sString);
$this -> aReplace ($this -> aReplaceGrp1);
$this -> aReplace ($this -> aReplaceGrp2, true);
$this -> aReplace ($this -> aReplaceGrp3);
$this -> aReplace ($this -> aReplaceGrp4);
$this -> aReplace ($this -> aReplaceGrp5, true);
$this -> aReplace ($this -> aReplaceGrp6, true);
$this -> aReplace ($this -> aReplaceGrp7);
$this -> aReplace ($this -> aReplaceGrp8);
$this -> aReplace ($this -> aReplaceGrp9, true);
$this -> aReplace ($this -> aReplaceGrp10);
$this -> aReplace ($this -> aReplaceGrp11);
$this -> sString = preg_replace( '`(.)\1`', '$1', $this -> sString );
$this -> trimLast ();
$this -> getNum ();
}
/**
* private function aReplace ()
* method used to replace letters, given an array
* @Param array aTab : the replacement array to be used
* @Param bool bPreg : is the array an array of regular expressions patterns : true => yes`| false => no
*/
private function aReplace (array $aTab, $bPreg = false) {
if (false === $bPreg) {
$this -> sString = str_replace (array_keys ($aTab), array_values ($aTab), $this -> sString);
} else {
$this -> sString = preg_replace (array_keys ($aTab), array_values ($aTab), $this -> sString);
}
}
/**
* private function trimLast ()
* method to trim the bad endings
*/
private function trimLast () {
$length = strlen ($this -> sString) - 1;
if (in_array ($this -> sString{$length}, $this -> aEnd)) {
$this -> sString = substr ($this -> sString, 0, $length);
}
}
/**
* private static function mapNum ()
* callback method to create the phonex numeric code, base 22
* @Param int val : current value
* @Param int clef : current key
* @Returns int num : the calculated base 22 value
*/
private static function mapNum ($val, $clef) {
$num = array_search ($val, self::$aReplaceGrp12);
$num *= pow (22, - ($clef + 1));
return $num;
}
/**
* private function getNum ()
* method to get a numeric array from the main string
* we call the callback function mapNum and we add every values of the obtained array to get the final phonex code
*/
private function getNum () {
$aString = str_split ($this -> sString);
$aNum = array_map (array ('self', 'mapNum'), array_values ($aString), array_keys ($aString));
$this -> sString = (string) array_sum ($aNum);
}
}
?>
Conclusion
Reste des imperfections et des étrangetés...si quelqu'un veut s'amuser à débugger, il le peut. Je fais de même de mon côté :-)
Je remercie Malik qui m'a aidé a débugger un peu l'algo :-) Quelques bugs subsistaient sur l'algo que j'ai suivi. Maintenant, il ne devrait plus y en avoir! Seul bémol : php n'utilise pas des nombres de taille suffisemment grande pour que le code phonex base22 soit aussi long que celui de l'algo original. Je travaille à contourner ce problème, mais j'ai quelques petits soucis avec bcmath :-) Ceci dit, ça tourne en l'état, et plutôt pas mal même :-)
Historique
- 16 mars 2006 09:33:29 :
- Correction d'un bug lors du dédoublonnage
- 16 mars 2006 10:03:07 :
- ajout des commentaires dans la classe
- 16 mars 2006 10:34:43 :
- ajout du calcul de la distance de Levensthein avec affichage par pertinence
- 17 mars 2006 12:47:47 :
- Ajout version PHP4
- 17 mars 2006 13:10:48 :
- Ajout des credits ;-)
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 chaines [ par darkhorkeu ]
Quelqu'un sait-il si la comparaison de chaine: "str1" == "str2" revient au meme que:!strcmp("str1","str2") merci d'avance
Code php pour comparaison de 2 chaines de caractère [ par camille911 ]
Bonjour à tous, Voilà, je suis en train de développer un petit site de comparateur de séquences (c'est de la biologie), et je bute sur beaucoup d'obst
comparaison de deux listes [ par eva4 ]
kikou tout le monde [^^happy3] heureuse de vous revoir ..... alors, je suis aujourd'hui confrontée à un petit pb. dans une bdd j'ai 2 champs. 1er cha
algo test sur une bdd... urgent svp!!! ='( [ par tagadax ]
Bonjour a tous, voila j'ai un petit souci d'algo... heu je n'y connais vraiment rien... J'aimerai faire un test sur une base de donnée pour par exempl
Algo "modified preorder tree traversal" [ par kohntark ]
Salut, Une fois n'est pas coutume je VEUX un code tout fait !!! (<== [^^mad5] règlement !!) [^^yeuxenlair] ma masse capillaire commence à souffrir d
Nombre de combinaisons possibles avec l'algo MCRYPT_RIJNDAEL_256 [ par roymatthieu ]
Bonjour... Question débile de fin d'après-midi... J'ai une fonction de chiffrement qui me permet de protéger certaines données sensibles... J'utili
php algo [ par tagadax ]
Bonjour a tous, Voila j'ai un petit souci avec l'algo, en fait j'ai besoin de faire un algo pour pouvoir réserver une salle de reunion, afin de verifi
Comparaison Entre Deux Bases de Donnés [ par Btakeshi ]
Bonsoir tout le monde Je souhaite faire une comparaison entre deux base de donnes Une avec oracle et l’autre avec mysql Ensuite afficher les r
comparaison [ par marwamaritta ]
bonsoir, je veux comparer entre six variables de type entier mais j'ai pas trouvé le code convenable en php, merci de m'aider
Comparaison URL PHP [ par vargas ]
Bonjour tout le monde J'ai une question toute bête, mais je ne trouve pas la solution. Voila j'ai besoin de comparer deux URL pour faire une conditio
|
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
|