emmanuel_grognet

solution en php de l'exercice http://sametmax.com/petit-exer

Dec 19th, 2013
145
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.57 KB | None | 0 0
  1. <?php
  2. // solution en php de l'exercice http://sametmax.com/petit-exercice-en-python/
  3. $texte = file_get_contents($argv[1]);
  4. $alphabet = array(
  5.             'Š'=>'S', 'š'=>'s', 'Ð'=>'Dj','Ž'=>'Z', 'ž'=>'z', 'À'=>'A', 'Á'=>'A', 'Â'=>'A', 'Ã'=>'A', 'Ä'=>'A',
  6.             'Å'=>'A', 'Æ'=>'A', 'Ç'=>'C', 'È'=>'E', 'É'=>'E', 'Ê'=>'E', 'Ë'=>'E', 'Ì'=>'I', 'Í'=>'I', 'Î'=>'I',
  7.             'Ï'=>'I', 'Ñ'=>'N', 'Ò'=>'O', 'Ó'=>'O', 'Ô'=>'O', 'Õ'=>'O', 'Ö'=>'O', 'Ø'=>'O', 'Ù'=>'U', 'Ú'=>'U',
  8.             'Û'=>'U', 'Ü'=>'U', 'Ý'=>'Y', 'Þ'=>'B', 'ß'=>'Ss','à'=>'a', 'á'=>'a', 'â'=>'a', 'ã'=>'a', 'ä'=>'a',
  9.             'å'=>'a', 'æ'=>'a', 'ç'=>'c', 'è'=>'e', 'é'=>'e', 'ê'=>'e', 'ë'=>'e', 'ì'=>'i', 'í'=>'i', 'î'=>'i',
  10.             'ï'=>'i', 'ð'=>'o', 'ñ'=>'n', 'ò'=>'o', 'ó'=>'o', 'ô'=>'o', 'õ'=>'o', 'ö'=>'o', 'ø'=>'o', 'ù'=>'u',
  11.             'ú'=>'u', 'û'=>'u', 'ý'=>'y', 'ý'=>'y', 'þ'=>'b', 'ÿ'=>'y', 'ƒ'=>'f', 'œ'=>'oe',
  12.         );
  13. $texte = strtr ($texte, $alphabet);
  14. $texte = strtolower($texte);
  15. $texte = preg_replace('#[^[:alnum:]]#u', ' ', $texte);
  16. $mots  = str_word_count($texte, 1);
  17. foreach ($mots as $position => $mot) $liste_non_triee[$mot][]=$position;
  18.  
  19. $index=0;
  20. foreach($liste_non_triee as $mot => $positions) $liste[$mot]=array($positions,$index++);
  21. uasort($liste,function($a, $b) {
  22.   if(count($a[0]) != count($b[0])) return (count($a[0]) < count($b[0])) ? -1 : 1;
  23.     return ($a[1] < $b[1]) ? -1 : 1;}
  24. );
  25.  
  26. foreach($liste as $mot => $positions_index) echo $mot.': '.implode(', ', $positions_index[0])."\n";
  27.    
  28. ?>
Add Comment
Please, Sign In to add comment