diff --git a/.gitignore b/.gitignore index 7fb79f0..1dc2ec5 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,5 @@ __pycache__/ test*.* -*.py \ No newline at end of file +*.py +dico2.csv +wiktionaryXfr2010.7z diff --git a/Grille.php b/Grille.php index f9ee652..6b0e7a7 100644 --- a/Grille.php +++ b/Grille.php @@ -22,7 +22,7 @@ class Grille implements ArrayAccess public $grille; public $hauteur; public $largeur; - private $lettres_suivantes; + public $dico; private $positions; private $nb_positions; public $lignes = []; @@ -43,7 +43,7 @@ class Grille implements ArrayAccess } $this->nb_positions = count($this->positions); - $this->lettres_suivantes = tries(max($hauteur, $largeur)); + $this->dico = mots_espaces(max($hauteur, $largeur)); } public function get_ligne($y, $largeur) @@ -69,11 +69,11 @@ class Grille implements ArrayAccess // Recherche de la prochaine lettre possible sur la case courante // en ligne if ($x == 0) { - $lettres_ligne = $this->lettres_suivantes[$this->largeur]; + $lettres_ligne = $this->dico[$this->largeur]; } // en colonne - $lettres_colonne = $this->lettres_suivantes[$this->hauteur]; + $lettres_colonne = $this->dico[$this->hauteur]; for ($y2 = 0; $y2 < $y; $y2++) { $lettres_colonne = $lettres_colonne->branches[$this->grille[$y2][$x]]; } @@ -93,11 +93,11 @@ class Grille implements ArrayAccess $this->grille[$y][$x] = $lettre; // Omission des lettres isolées - if ($lettre == " " && - ($y - 2 < 0 || $this->grille[$y - 2][$x] == " ") && - ($y - 1 < 0 || $x - 1 < 0 || $this->grille[$y - 1][$x - 1] == " ") && - ($y - 1 < 0 || $x + 1 >= $this->largeur || $this->grille[$y - 1][$x + 1] == " ") - ) { + if ($lettre == " " + && ($y - 2 < 0 || $this->grille[$y - 2][$x] == " ") + && ($y - 1 < 0 || $x - 1 < 0 || $this->grille[$y - 1][$x - 1] == " ") + && ($y - 1 < 0 || $x + 1 >= $this->largeur || $this->grille[$y - 1][$x + 1] == " ") + ) { continue; } diff --git a/dico.php b/dico.php index 449e40c..83a6a3e 100644 --- a/dico.php +++ b/dico.php @@ -6,46 +6,55 @@ const MIN_PREMIER_MOT = 1; const MIN_MOTS_SUIVANTS = 1; -$dico = [[""]]; -if (($lecteur = fopen("dico.csv", "r")) !== FALSE) { - $header = fgetcsv($lecteur, 0, "\t"); - while (($ligne = fgetcsv($lecteur, 0, "\t")) !== FALSE) { - if ($ligne[0] == NULL || substr($ligne[0], 0, 1) == "#") continue; - switch(count($ligne)) { - case 1: - [$mot] = $ligne; - $definition = ""; - break; - case 2: - [$mot, $definition] = $ligne; - break; - case 3: - [$mot, $definition, $auteur] = $ligne; - $definition .= " $auteur"; - break; +function dico($longueur_max) { + $dico = [[""]]; + if (($lecteur = fopen("dico.csv", "r")) !== FALSE) { + $header = fgetcsv($lecteur, 0, "\t"); + while (($ligne = fgetcsv($lecteur, 0, "\t")) !== FALSE) { + if ( + $ligne[0] == NULL + || substr($ligne[0], 0, 1) == "#" + || strlen($ligne[0]) > $longueur_max + ) continue; + + switch(count($ligne)) { + case 1: + [$mot] = $ligne; + $definition = ""; + break; + case 2: + [$mot, $definition] = $ligne; + break; + case 3: + [$mot, $definition, $auteur] = $ligne; + $definition .= " $auteur"; + break; + } + + $mot = str_split(strtoupper($mot)); + $longueur = count($mot); + if (!isset($dico[$longueur])) $dico[$longueur] = new Trie(); + if (!isset($dico[$longueur][$mot])) $dico[$longueur][$mot] = []; + if (strlen($definition)) $dico[$longueur][$mot][] = $definition; } - $mot = str_split(strtoupper($mot)); - $longueur = count($mot); - if (!isset($dico[$longueur])) $dico[$longueur] = new Trie(); - if (!isset($dico[$longueur][$mot])) $dico[$longueur][$mot] = []; - if (strlen($definition)) $dico[$longueur][$mot][] = $definition; + fclose($lecteur); } - fclose($lecteur); + + return $dico; } -function tries($longueur_max) { - global $dico; +function mots_espaces($longueur_max) { + $dico = dico($longueur_max); $_tries = [[]]; for ($longueur = 1; $longueur <= $longueur_max; $longueur++) { - $_tries[$longueur] = $dico[$longueur]; for ($position_espace = MIN_PREMIER_MOT; $position_espace + MIN_MOTS_SUIVANTS < $longueur; $position_espace++) { - $mots_suivants = $_tries[$longueur - $position_espace - 1]; + $mots_suivants = $dico[$longueur - $position_espace - 1]; foreach ($dico[$position_espace] as $premier_mot => $definition) { $premier_mot[] = " "; - $_tries[$longueur][$premier_mot] = $mots_suivants; + $dico[$longueur][$premier_mot] = $mots_suivants; } } } - return $_tries; + return $dico; } diff --git a/index.php b/index.php index 05d42fd..f8f6c09 100644 --- a/index.php +++ b/index.php @@ -49,7 +49,7 @@ if ($grille_valide) { for ($y = 0; $y < $hauteur; $y++) { $definitions_horizontales[$y] = []; foreach ($grille->lignes[$y] as $mot) { - $definitions = $dico[strlen($mot)][str_split($mot)]; + $definitions = $grille->dico[strlen($mot)][str_split($mot)]; if (count($definitions)) { $definitions_horizontales[$y][] = $definitions[mt_rand(0, count($definitions) - 1)]; } @@ -59,7 +59,7 @@ if ($grille_valide) { for ($x = 0 ; $x < $largeur; $x++) { $definitions_verticales[$x] = []; foreach ($grille->colonnes[$x] as $mot) { - $definitions = $dico[strlen($mot)][str_split($mot)]; + $definitions = $grille->dico[strlen($mot)][str_split($mot)]; if (count($definitions)) { $definitions_verticales[$x][] = $definitions[mt_rand(0, count($definitions) - 1)]; }