omettre les mots plus grands que la grille

This commit is contained in:
Adrien MALINGREY 2025-05-07 19:38:04 +02:00
parent ab9e1f08ef
commit d5a120cd9a
4 changed files with 52 additions and 41 deletions

4
.gitignore vendored
View File

@ -1,3 +1,5 @@
__pycache__/ __pycache__/
test*.* test*.*
*.py *.py
dico2.csv
wiktionaryXfr2010.7z

View File

@ -22,7 +22,7 @@ class Grille implements ArrayAccess
public $grille; public $grille;
public $hauteur; public $hauteur;
public $largeur; public $largeur;
private $lettres_suivantes; public $dico;
private $positions; private $positions;
private $nb_positions; private $nb_positions;
public $lignes = []; public $lignes = [];
@ -43,7 +43,7 @@ class Grille implements ArrayAccess
} }
$this->nb_positions = count($this->positions); $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) public function get_ligne($y, $largeur)
@ -69,11 +69,11 @@ class Grille implements ArrayAccess
// Recherche de la prochaine lettre possible sur la case courante // Recherche de la prochaine lettre possible sur la case courante
// en ligne // en ligne
if ($x == 0) { if ($x == 0) {
$lettres_ligne = $this->lettres_suivantes[$this->largeur]; $lettres_ligne = $this->dico[$this->largeur];
} }
// en colonne // en colonne
$lettres_colonne = $this->lettres_suivantes[$this->hauteur]; $lettres_colonne = $this->dico[$this->hauteur];
for ($y2 = 0; $y2 < $y; $y2++) { for ($y2 = 0; $y2 < $y; $y2++) {
$lettres_colonne = $lettres_colonne->branches[$this->grille[$y2][$x]]; $lettres_colonne = $lettres_colonne->branches[$this->grille[$y2][$x]];
} }
@ -93,11 +93,11 @@ class Grille implements ArrayAccess
$this->grille[$y][$x] = $lettre; $this->grille[$y][$x] = $lettre;
// Omission des lettres isolées // Omission des lettres isolées
if ($lettre == " " && if ($lettre == " "
($y - 2 < 0 || $this->grille[$y - 2][$x] == " ") && && ($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 < 0 || $this->grille[$y - 1][$x - 1] == " ")
($y - 1 < 0 || $x + 1 >= $this->largeur || $this->grille[$y - 1][$x + 1] == " ") && ($y - 1 < 0 || $x + 1 >= $this->largeur || $this->grille[$y - 1][$x + 1] == " ")
) { ) {
continue; continue;
} }

View File

@ -6,46 +6,55 @@ const MIN_PREMIER_MOT = 1;
const MIN_MOTS_SUIVANTS = 1; const MIN_MOTS_SUIVANTS = 1;
$dico = [[""]]; function dico($longueur_max) {
if (($lecteur = fopen("dico.csv", "r")) !== FALSE) { $dico = [[""]];
$header = fgetcsv($lecteur, 0, "\t"); if (($lecteur = fopen("dico.csv", "r")) !== FALSE) {
while (($ligne = fgetcsv($lecteur, 0, "\t")) !== FALSE) { $header = fgetcsv($lecteur, 0, "\t");
if ($ligne[0] == NULL || substr($ligne[0], 0, 1) == "#") continue; while (($ligne = fgetcsv($lecteur, 0, "\t")) !== FALSE) {
switch(count($ligne)) { if (
case 1: $ligne[0] == NULL
[$mot] = $ligne; || substr($ligne[0], 0, 1) == "#"
$definition = ""; || strlen($ligne[0]) > $longueur_max
break; ) continue;
case 2:
[$mot, $definition] = $ligne; switch(count($ligne)) {
break; case 1:
case 3: [$mot] = $ligne;
[$mot, $definition, $auteur] = $ligne; $definition = "";
$definition .= " <small><em>$auteur</em></small>"; break;
break; case 2:
[$mot, $definition] = $ligne;
break;
case 3:
[$mot, $definition, $auteur] = $ligne;
$definition .= " <small><em>$auteur</em></small>";
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)); fclose($lecteur);
$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);
return $dico;
} }
function tries($longueur_max) { function mots_espaces($longueur_max) {
global $dico; $dico = dico($longueur_max);
$_tries = [[]]; $_tries = [[]];
for ($longueur = 1; $longueur <= $longueur_max; $longueur++) { 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++) { 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) { foreach ($dico[$position_espace] as $premier_mot => $definition) {
$premier_mot[] = " "; $premier_mot[] = " ";
$_tries[$longueur][$premier_mot] = $mots_suivants; $dico[$longueur][$premier_mot] = $mots_suivants;
} }
} }
} }
return $_tries; return $dico;
} }

View File

@ -49,7 +49,7 @@ if ($grille_valide) {
for ($y = 0; $y < $hauteur; $y++) { for ($y = 0; $y < $hauteur; $y++) {
$definitions_horizontales[$y] = []; $definitions_horizontales[$y] = [];
foreach ($grille->lignes[$y] as $mot) { foreach ($grille->lignes[$y] as $mot) {
$definitions = $dico[strlen($mot)][str_split($mot)]; $definitions = $grille->dico[strlen($mot)][str_split($mot)];
if (count($definitions)) { if (count($definitions)) {
$definitions_horizontales[$y][] = $definitions[mt_rand(0, count($definitions) - 1)]; $definitions_horizontales[$y][] = $definitions[mt_rand(0, count($definitions) - 1)];
} }
@ -59,7 +59,7 @@ if ($grille_valide) {
for ($x = 0 ; $x < $largeur; $x++) { for ($x = 0 ; $x < $largeur; $x++) {
$definitions_verticales[$x] = []; $definitions_verticales[$x] = [];
foreach ($grille->colonnes[$x] as $mot) { foreach ($grille->colonnes[$x] as $mot) {
$definitions = $dico[strlen($mot)][str_split($mot)]; $definitions = $grille->dico[strlen($mot)][str_split($mot)];
if (count($definitions)) { if (count($definitions)) {
$definitions_verticales[$x][] = $definitions[mt_rand(0, count($definitions) - 1)]; $definitions_verticales[$x][] = $definitions[mt_rand(0, count($definitions) - 1)];
} }