par
supercanard » 07 avr. 2009, 14:49
Merci pour ces explications
Je vais regarder ton code dès que j'aurais un peu de temps.
Entre temps j'ai modifié le miens et j'ai essayé de faire une version avec des regex qui marche plus ou moins bien.
On voit sur ce lien deux test, le deuxième étant la version regex. Il y a des mots qui passent un peu à la trappe :
http://supercanard.phpnet.org/test/dete ... v/test.php
De plus lorque j'ai des mots composés ex base de données, il y a conflit avec base par exemple
<?php
/*
regex : \.
casse : ignoré
*/
class detectionMot{
public $inputString; // Chaine d'entrée
private $dictionaire; // contient les mots du fichiers texte chargé
private $motDetecte; // contient les mots présent à la fois dans $arrayOfInputString et $dictionaire
private $arrayOfInputString; // contient les mots de la chaine $inputString
public function __construct($file){
$this->inputString = null;
$this->dictionaire = $this->load($file);
$this->motDetecte = array();
$this->arrayOfInputString = array();
}
public function genererLien($url, $inputString){ // param : url des liens ( glossaire.php?mot= ), chaine à tester
$this->inputString = $inputString;
$this->detecter();
foreach($this->arrayOfInputString as $k=>$mot){
if(array_key_exists($k, $this->motDetecte)){
$finalString.= '<a href="'.$url.$this->simplifierMot($this->motDetecte[$k]).'">'.$mot.'</a>';
}
else{
$finalString.= $mot;
}
$finalString.= ' ';
}
return $finalString;
}
public function detecterSpam($tauxMax, $inputString){ // param : seuil max avant considération de la chaine comme spam, chaine à tester
$this->inputString = $inputString;
$this->detecter();
if($this->calculerTauxPresence() > $tauxMax){
return true;
}
else{
return false;
}
}
private function load($file){
$temp = file_get_contents($file);
return(explode("\n", $temp));
}
private function calculerTauxPresence(){
// Calcul le pourcentage de mots trouvés dans $arrayOfInputString étant présents dans $dictionaire
return round(count($this->motDetecte)*100/count($this->arrayOfInputString));
}
private function detecter(){
// Construit $arrayOfInputString, cherche ses valeurs présentes dans $dictionaire, construit $motDetecte
$this->arrayOfInputString = explode(' ', $this->inputString);
foreach($this->arrayOfInputString as $k=>$mot){
foreach($this->dictionaire as $motGlosaire){
if(strtolower($mot) == strtolower($motGlosaire)){ // cas simple
$this->motDetecte[$k] = $mot;
}
elseif(preg_match('#'.strtolower($motGlosaire).'(,|\.|;|:|!){1}$#', strtolower($mot), $matches)){ // cas : mot,
$this->motDetecte[$k] = str_replace($matches[1], null, $mot);
}
elseif(preg_match('#((.*){1}(\'))'.strtolower($motGlosaire).'#', strtolower($mot), $matches)){ // cas : l'mot
$this->motDetecte[$k] = str_replace($matches[1], null, $mot);
}
}
}
}
public function test($url, $inputString){
foreach($this->dictionaire as $motGlosaire){
$this->inputString = $inputString;
//$output = $this->inputString;
preg_match_all('#( ){1}('.$motGlosaire.')(,|\.|;|:|!| ){1}#ui', $this->inputString, $matches);
//print_r($matches);
foreach($matches[0] as $motTrouve){
//echo $motTrouve;
$pattern[] = '#'.$motTrouve.'#';
$replace[] = '<a href="'.$url.$this->simplifierMot($motGlosaire).'">'.$motTrouve.'</a>';
//$output = preg_replace('#'.$motTrouve.'#', 'TOTO', $output);
//echo $output;
}
}
return preg_replace($pattern, $replace, $this->inputString);
}
private function simplifierMot($string)
{
$replace = array(
' '=>'-',
'ŕ'=>'a',
'á'=>'a',
'â'=>'a',
'ă'=>'a',
'ä'=>'a',
'ĺ'=>'a',
'ň'=>'o',
'ó'=>'o',
'ô'=>'o',
'ő'=>'o',
'ö'=>'o',
'č'=>'e',
'é'=>'e',
'ę'=>'e',
'ë'=>'e',
'ě'=>'i',
'í'=>'i',
'î'=>'i',
'ď'=>'i',
'ů'=>'u',
'ú'=>'u',
'ű'=>'u',
'ü'=>'u',
'˙'=>'y',
'ń'=>'n',
'ç'=>'c',
'ř'=>'0'
);
return strtr(strtolower($string), $replace);
}
}
?>
Merci pour ces explications :D
Je vais regarder ton code dès que j'aurais un peu de temps.
Entre temps j'ai modifié le miens et j'ai essayé de faire une version avec des regex qui marche plus ou moins bien.
On voit sur ce lien deux test, le deuxième étant la version regex. Il y a des mots qui passent un peu à la trappe : http://supercanard.phpnet.org/test/detectionMot/dev/test.php
De plus lorque j'ai des mots composés ex base de données, il y a conflit avec base par exemple
[php]
<?php
/*
regex : \.
casse : ignoré
*/
class detectionMot{
public $inputString; // Chaine d'entrée
private $dictionaire; // contient les mots du fichiers texte chargé
private $motDetecte; // contient les mots présent à la fois dans $arrayOfInputString et $dictionaire
private $arrayOfInputString; // contient les mots de la chaine $inputString
public function __construct($file){
$this->inputString = null;
$this->dictionaire = $this->load($file);
$this->motDetecte = array();
$this->arrayOfInputString = array();
}
public function genererLien($url, $inputString){ // param : url des liens ( glossaire.php?mot= ), chaine à tester
$this->inputString = $inputString;
$this->detecter();
foreach($this->arrayOfInputString as $k=>$mot){
if(array_key_exists($k, $this->motDetecte)){
$finalString.= '<a href="'.$url.$this->simplifierMot($this->motDetecte[$k]).'">'.$mot.'</a>';
}
else{
$finalString.= $mot;
}
$finalString.= ' ';
}
return $finalString;
}
public function detecterSpam($tauxMax, $inputString){ // param : seuil max avant considération de la chaine comme spam, chaine à tester
$this->inputString = $inputString;
$this->detecter();
if($this->calculerTauxPresence() > $tauxMax){
return true;
}
else{
return false;
}
}
private function load($file){
$temp = file_get_contents($file);
return(explode("\n", $temp));
}
private function calculerTauxPresence(){
// Calcul le pourcentage de mots trouvés dans $arrayOfInputString étant présents dans $dictionaire
return round(count($this->motDetecte)*100/count($this->arrayOfInputString));
}
private function detecter(){
// Construit $arrayOfInputString, cherche ses valeurs présentes dans $dictionaire, construit $motDetecte
$this->arrayOfInputString = explode(' ', $this->inputString);
foreach($this->arrayOfInputString as $k=>$mot){
foreach($this->dictionaire as $motGlosaire){
if(strtolower($mot) == strtolower($motGlosaire)){ // cas simple
$this->motDetecte[$k] = $mot;
}
elseif(preg_match('#'.strtolower($motGlosaire).'(,|\.|;|:|!){1}$#', strtolower($mot), $matches)){ // cas : mot,
$this->motDetecte[$k] = str_replace($matches[1], null, $mot);
}
elseif(preg_match('#((.*){1}(\'))'.strtolower($motGlosaire).'#', strtolower($mot), $matches)){ // cas : l'mot
$this->motDetecte[$k] = str_replace($matches[1], null, $mot);
}
}
}
}
public function test($url, $inputString){
foreach($this->dictionaire as $motGlosaire){
$this->inputString = $inputString;
//$output = $this->inputString;
preg_match_all('#( ){1}('.$motGlosaire.')(,|\.|;|:|!| ){1}#ui', $this->inputString, $matches);
//print_r($matches);
foreach($matches[0] as $motTrouve){
//echo $motTrouve;
$pattern[] = '#'.$motTrouve.'#';
$replace[] = '<a href="'.$url.$this->simplifierMot($motGlosaire).'">'.$motTrouve.'</a>';
//$output = preg_replace('#'.$motTrouve.'#', 'TOTO', $output);
//echo $output;
}
}
return preg_replace($pattern, $replace, $this->inputString);
}
private function simplifierMot($string)
{
$replace = array(
' '=>'-',
'ŕ'=>'a',
'á'=>'a',
'â'=>'a',
'ă'=>'a',
'ä'=>'a',
'ĺ'=>'a',
'ň'=>'o',
'ó'=>'o',
'ô'=>'o',
'ő'=>'o',
'ö'=>'o',
'č'=>'e',
'é'=>'e',
'ę'=>'e',
'ë'=>'e',
'ě'=>'i',
'í'=>'i',
'î'=>'i',
'ď'=>'i',
'ů'=>'u',
'ú'=>'u',
'ű'=>'u',
'ü'=>'u',
'˙'=>'y',
'ń'=>'n',
'ç'=>'c',
'ř'=>'0'
);
return strtr(strtolower($string), $replace);
}
}
?>
[/php]