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__/
test*.*
*.py
*.py
dico2.csv
wiktionaryXfr2010.7z

View File

@ -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;
}

View File

@ -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 .= " <small><em>$auteur</em></small>";
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 .= " <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));
$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;
}

View File

@ -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)];
}