Compare commits

..

14 Commits

Author SHA1 Message Date
13fcdf05fb grand dico 2025-05-08 01:47:58 +02:00
ca39709fbd nombre de mots 2025-05-07 23:54:00 +02:00
ddebd453df interface trie avec des strings 2025-05-07 21:21:48 +02:00
d5a120cd9a omettre les mots plus grands que la grille 2025-05-07 19:38:04 +02:00
ab9e1f08ef Tries everywhere! 2025-05-07 18:34:08 +02:00
b1f3e8b85f omission des lettres isolées 2025-05-07 00:16:55 +02:00
c20d2324e9 FAST & FURIOUS 2025-05-06 17:44:16 +02:00
65bb8be2e3 Revert "Merge branch 'tmp3'"
This reverts commit ed1ad0566f, reversing
changes made to 6ee909914d.
2025-05-06 17:29:51 +02:00
ed1ad0566f Merge branch 'tmp3' 2025-05-06 17:27:47 +02:00
47be3d2e51 FAST & FURIOUS 2025-05-06 16:56:59 +02:00
6ee909914d WIP 2025-05-06 15:55:58 +02:00
4c014bc789 variable inutilisée 2025-05-06 08:47:10 +02:00
b063d39689 Merge branch 'tmp2' 2025-05-06 02:43:01 +02:00
d150e76a9e fix id vide 2025-05-06 02:38:18 +02:00
6 changed files with 973839 additions and 3210 deletions

2
.gitignore vendored
View File

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

View File

@ -2,16 +2,33 @@
include_once "dico.php"; include_once "dico.php";
class Grille implements ArrayAccess { $randmax = mt_getrandmax() + 1;
function gaussienne($moyenne = 0, $ecartType = 1.0): float {
global $randmax;
$u = 0;
$v = 0;
$u = (mt_rand() + 1) / $randmax;
$v = (mt_rand() + 1) / $randmax;
$z = sqrt(-2.0 * log($u)) * cos(2.0 * M_PI * $v);
return $z * $ecartType + $moyenne;
}
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 = [];
public $colonnes = []; public $colonnes = [];
public $valide = false; public $valide = false;
private $id;
public function __construct($hauteur, $largeur) public function __construct($hauteur, $largeur)
{ {
@ -26,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)
@ -45,33 +62,51 @@ class Grille implements ArrayAccess {
return $colonne; return $colonne;
} }
public function gen_grilles($i = 0, $lettres_suivantes_ligne = NULL) public function gen_grilles($i = 0, $lettres_ligne = NULL)
{ {
[$x, $y] = $this->positions[$i]; [$x, $y] = $this->positions[$i];
// Recherche de la prochaine lettre possible sur la case courante
// en ligne
if ($x == 0) { if ($x == 0) {
$lettres_suivantes_ligne = $this->lettres_suivantes[$this->largeur]; $lettres_ligne = $this->dico[$this->largeur];
} }
$lettres_suivantes_colonne = $this->lettres_suivantes[$this->hauteur]; // en colonne
for ($y2 = 0; $y2 < $y; $y2++) $lettres_colonne = $this->dico[$this->hauteur];
$lettres_suivantes_colonne = $lettres_suivantes_colonne->noeud[$this->grille[$y2][$x]]; for ($y2 = 0; $y2 < $y; $y2++) {
$lettres_communes = array_intersect( $lettres_colonne = $lettres_colonne->branches[$this->grille[$y2][$x]];
array_keys($lettres_suivantes_ligne->noeud), }
array_keys($lettres_suivantes_colonne->noeud) $lettres_communes = array_intersect_key(
$lettres_ligne->branches,
$lettres_colonne->branches
); );
usort($lettres_communes, function ($a, $b) { foreach ($lettres_communes as $lettre => $_) {
return mt_rand(-1, 1); $lettres_communes[$lettre] = count($lettres_ligne->branches[$lettre]) * count($lettres_colonne->branches[$lettre]) * gaussienne(1, 5);
}
uksort($lettres_communes, function($a, $b) use ($lettres_communes) {
return $lettres_communes[$b] <=> $lettres_communes[$a];
}); });
$lettres_communes = array_slice($lettres_communes, 0, 3);
foreach ($lettres_communes as $lettre) { foreach ($lettres_communes as $lettre => $_) {
$this->grille[$y][$x] = $lettre; $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] == " ")
) {
continue;
}
// Omission des doublons
$mots = []; $mots = [];
if ($x == $this->largeur - 1) $mots = explode(" ", $this->get_ligne($y, $this->largeur)); if ($x == $this->largeur - 1) $mots = explode(" ", $this->get_ligne($y, $this->largeur));
else if ($lettre == " ") $mots = explode(" ", $this->get_ligne($y, $x)); else if ($lettre == " ") $mots = explode(" ", $this->get_ligne($y, $x));
else $mots = []; else $mots = [];
$this->lignes[$y] = array_filter($mots, function($mot) { $this->lignes[$y] = array_filter($mots, function ($mot) {
return strlen($mot) >= 2; return strlen($mot) >= 2;
}); });
if (count($this->lignes[$y])) { if (count($this->lignes[$y])) {
@ -92,24 +127,23 @@ class Grille implements ArrayAccess {
} }
if ($i < $this->nb_positions - 1) { if ($i < $this->nb_positions - 1) {
yield from $this->gen_grilles($i + 1, $lettres_suivantes_ligne->noeud[$lettre]); yield from $this->gen_grilles($i + 1, $lettres_ligne->branches[$lettre]);
} else { } else {
yield $this; yield $this;
} }
} }
} }
public function genere() { public function genere($id)
session_regenerate_id(); {
$id = session_id();
mt_srand(crc32($id)); mt_srand(crc32($id));
$grilles = $this->gen_grilles(); $grilles = $this->gen_grilles();
$grilles->current(); $grilles->current();
if ($grilles->valid()) { if ($grilles->valid()) {
$this->save(); $this->save($id);
return $id; return true;
} else { } else {
return false; return false;
} }
@ -123,7 +157,9 @@ class Grille implements ArrayAccess {
return hash('sha256', $string); return hash('sha256', $string);
} }
public function save() { public function save($id)
{
session_id($id);
session_start(["use_cookies" => false]); session_start(["use_cookies" => false]);
$_SESSION["$this->largeur,$this->hauteur"] = implode( $_SESSION["$this->largeur,$this->hauteur"] = implode(
@ -137,7 +173,8 @@ class Grille implements ArrayAccess {
); );
} }
public function load($id) { public function load($id)
{
session_id($id); session_id($id);
session_start(["use_cookies" => false]); session_start(["use_cookies" => false]);
@ -146,21 +183,21 @@ class Grille implements ArrayAccess {
} }
foreach (str_split($_SESSION["$this->largeur,$this->hauteur"], $this->largeur) as $y => $ligne) { foreach (str_split($_SESSION["$this->largeur,$this->hauteur"], $this->largeur) as $y => $ligne) {
foreach(str_split($ligne) as $x => $lettre) { foreach (str_split($ligne) as $x => $lettre) {
$this->grille[$y][$x] = $lettre; $this->grille[$y][$x] = $lettre;
} }
} }
for ($y = 0; $y < $this->hauteur; $y++) { for ($y = 0; $y < $this->hauteur; $y++) {
$mots = explode(" ", $this->get_ligne($y, $this->largeur)); $mots = explode(" ", $this->get_ligne($y, $this->largeur));
$this->lignes[$y] = array_filter($mots, function($mot) { $this->lignes[$y] = array_filter($mots, function ($mot) {
return strlen($mot) >= 2; return strlen($mot) >= 2;
}); });
} }
for ($x = 0; $x < $this->largeur; $x++) { for ($x = 0; $x < $this->largeur; $x++) {
$mots = explode(" ", $this->get_colonne($x, $this->hauteur)); $mots = explode(" ", $this->get_colonne($x, $this->hauteur));
$this->colonnes[$x] = array_filter($mots, function($mot) { $this->colonnes[$x] = array_filter($mots, function ($mot) {
return strlen($mot) >= 2; return strlen($mot) >= 2;
}); });
} }
@ -169,19 +206,23 @@ class Grille implements ArrayAccess {
} }
public function offsetExists(mixed $offset): bool { public function offsetExists(mixed $offset): bool
{
return isset($this->grille[$offset]); return isset($this->grille[$offset]);
} }
public function offsetGet(mixed $offset): mixed { public function offsetGet(mixed $offset): mixed
{
return $this->grille[$offset]; return $this->grille[$offset];
} }
public function offsetSet(mixed $offset, mixed $value): void { public function offsetSet(mixed $offset, mixed $value): void
{
$this->grille[$offset] = $value; $this->grille[$offset] = $value;
} }
public function offsetUnset(mixed $offset): void { public function offsetUnset(mixed $offset): void
{
unset($this->grille[$offset]); unset($this->grille[$offset]);
} }
} }

123
Trie.php
View File

@ -1,70 +1,95 @@
<?php <?php
class Trie implements ArrayAccess, IteratorAggregate //, Countable //, Iterator class Trie implements ArrayAccess, IteratorAggregate, Countable {
{ public array $branches = [];
public array $noeud = []; private $nb_branches = 0;
public function offsetSet($cles, $valeur): void { public function arraySet($cles, $valeur) {
if (!count($cles)) { $this->nb_branches++;
throw new \OutOfBoundsException("Liste de clés vide."); $cle = $cles[0];
} $cles = array_slice($cles, 1);
$cle = array_shift($cles); if ($cles == []) {
if (!isset($this->noeud[$cle])) $this->noeud[$cle] = new Trie(); $this->branches[$cle] = $valeur;
if (count($cles)) {
$this->noeud[$cle]->offsetSet($cles, $valeur);
} else { } else {
$this->noeud[$cle] = $valeur; if (!isset($this->branches[$cle])) $this->branches[$cle] = new Trie();
$this->branches[$cle]->arraySet($cles, $valeur);
}
}
public function arrayExists($cles) {
$cle = $cles[0];
$cles = array_slice($cles, 1);
if ($cles == []) {
return isset($this->branches[$cle]);
} else {
return isset($this->branches[$cle]) && $this->branches[$cle]->arrayExists($cles);
}
}
public function &arrayGet($cles) {
$cle = $cles[0];
$cles = array_slice($cles, 1);
if ($cles == []) {
return $this->branches[$cle];
} else {
return $this->branches[$cle]->arrayGet($cles);
}
}
public function arrayUnset($cles) {
$cle = $cles[0];
$cles = array_slice($cles, 1);
if ($cles == []) {
unset($this->branches[$cle]);
$this->nb_branches--;
} else {
$this->branches[$cle]->arrayUnset($cles);
$this->nb_branches--;
if (count($this->branches[$cle]) == 0) {
unset($this->branches[$cle]);
}
}
}
public function arrayIterator() {
foreach ($this->branches as $cle => $branche) {
if ($branche instanceof Trie) {
foreach($branche->arrayIterator() as $sous_cles => $feuille) {
yield array_merge([$cle], $sous_cles) => $feuille;
}
} else {
yield [$cle] => $branche;
}
} }
} }
// ArrayAccess // ArrayAccess
public function offsetExists($cles): bool { public function offsetSet($string, $valeur): void {
if (!count($cles)) { $this->arraySet(str_split($string), $valeur);
return false;
}
$cle = array_shift($cles);
if (count($cles)) {
return $this->noeud[$cle]->offsetExists($cles);
} else {
return isset($this->noeud[$cles[0]]);
}
} }
public function offsetGet($cles): mixed { public function offsetExists($string): bool {
if (!count($cles)) { return $this->arrayExists(str_split($string));
throw new \OutOfBoundsException("Liste de clés vide.");
}
$cle = array_shift($cles);
if (!isset($this->noeud[$cle])) $this->noeud[$cle] = new Trie();
if (count($cles)) {
return $this->noeud[$cle]->offsetGet($cles);
} else {
return $this->noeud[$cle];
}
} }
public function offsetUnset($cles): void { public function &offsetGet($string): mixed {
if ($this->offsetExists($cles)) { return $this->arrayGet(str_split($string));
$cle = array_shift($cles);
if (count($cles)) {
$this->noeud[$cle]->offsetUnset($cles);
} else {
unset($this->noeud[$cle]);
}
} }
public function offsetUnset($string): void {
$this->arrayUnset(str_split($string));
} }
// IteratorAggregate // IteratorAggregate
public function getIterator(): Generator { public function getIterator(): Traversable {
foreach ($this->noeud as $cle => $branche) { foreach($this->arrayIterator() as $array => $valeur) {
if ($branche instanceof Trie) { yield implode("", $array) => $valeur;
foreach($branche as $sous_cles => $feuille) {
yield [$cle, ...$sous_cles] => $feuille;
}
} else {
yield $cle => $branche;
} }
} }
// Countable
public function count(): int {
return $this->nb_branches;
} }
} }

976654
dico.csv

File diff suppressed because one or more lines are too long

View File

@ -6,48 +6,61 @@ const MIN_PREMIER_MOT = 1;
const MIN_MOTS_SUIVANTS = 1; const MIN_MOTS_SUIVANTS = 1;
$dico = [[""]]; $nb_mots = 0;
if (($lecteur = fopen("dico.csv", "r")) !== FALSE) {
$header = fgetcsv($lecteur, 0, "\t"); function dico($longueur_max) {
while (($ligne = fgetcsv($lecteur, 0, "\t")) !== FALSE) { global $nb_mots;
if ($ligne[0] == NULL || substr($ligne[0], 0, 1) == "#") continue;
switch(count($ligne)) { $transliterator = Transliterator::createFromRules(':: Any-Latin; :: Latin-ASCII; :: NFD; :: [:Nonspacing Mark:] Remove; :: Upper(); :: NFC;', Transliterator::FORWARD);
case 1:
[$mot] = $ligne; $dico = [[""]];
$definition = ""; for ($longueur = 0; $longueur <= $longueur_max; $longueur++) {
break; $dico[] = new Trie();
case 2:
[$mot, $definition] = $ligne;
break;
case 3:
[$mot, $definition, $auteur] = $ligne;
$definition .= " <small><em>$auteur</em></small>";
break;
} }
$mot = strtoupper($mot); if (($lecteur = fopen("dico.csv", "r")) !== FALSE) {
$entete = 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;
$mot = $ligne[0];
$definitions = array_slice($ligne, 1);
$mot = str_replace("-", " ", $mot);
$mot = $transliterator->transliterate($mot);
if (strpos($mot, " ") !== false) {
$mots = explode(" ", $mot);
$nb_mots = count($mots);
$mot = implode("", $mots);
foreach($definitions as $i => $definition) {
$definitions[$i] .= " ($nb_mots mots)";
}
}
$longueur = strlen($mot); $longueur = strlen($mot);
if (!isset($dico[$longueur])) $dico[$longueur] = []; $dico[$longueur][$mot] = $definitions;
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) { function mots_espaces($longueur_max) {
global $dico; global $nb_mots;
$_tries = [[]]; $dico = dico($longueur_max);
for ($longueur = 1; $longueur <= $longueur_max; $longueur++) { for ($longueur = 1; $longueur <= $longueur_max; $longueur++) {
$_tries[$longueur] = new Trie();
foreach ($dico[$longueur] as $mot => $definition) {
$_tries[$longueur][str_split($mot)] = [];
}
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]->arrayIterator() as $premier_mot => $definition) {
$_tries[$longueur][str_split($premier_mot . " ")] = $mots_suivants; $premier_mot[] = " ";
$dico[$longueur]->arraySet($premier_mot, $mots_suivants);
} }
} }
} }
return $_tries; return $dico;
} }

View File

@ -1,12 +1,11 @@
<?php <?php
include_once "dico.php";
include_once "Grille.php"; include_once "Grille.php";
const HAUTEUR_DEFAUT = 6; const HAUTEUR_DEFAUT = 8;
const HAUTEUR_MIN = 2; const HAUTEUR_MIN = 2;
const HAUTEUR_MAX = 10; const HAUTEUR_MAX = 10;
const LARGEUR_DEFAUT = 6; const LARGEUR_DEFAUT = 8;
const LARGEUR_MIN = 2; const LARGEUR_MIN = 2;
const LARGEUR_MAX = 10; const LARGEUR_MAX = 10;
@ -31,8 +30,8 @@ $grille = new Grille($hauteur, $largeur);
if (!isset($_GET["grille"]) || $_GET["grille"] == "") { if (!isset($_GET["grille"]) || $_GET["grille"] == "") {
do { do {
$id = $grille->genere(); $id = uniqid();
} while (!$id); } while (!$grille->genere($id));
$_GET["grille"] = $id; $_GET["grille"] = $id;
header("Location: " . dirname($_SERVER['DOCUMENT_URI']) . "?" . http_build_query($_GET)); header("Location: " . dirname($_SERVER['DOCUMENT_URI']) . "?" . http_build_query($_GET));
@ -41,16 +40,15 @@ if (!isset($_GET["grille"]) || $_GET["grille"] == "") {
$id = htmlspecialchars($_GET["grille"]); $id = htmlspecialchars($_GET["grille"]);
$grille_valide = $grille->load($id) || $grille->genere(); $grille_valide = $grille->load($id) || $grille->genere($id);
mt_srand(crc32($id));
if ($grille_valide) { if ($grille_valide) {
mt_srand(crc32($id));
$definitions_horizontales = []; $definitions_horizontales = [];
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)][$mot]; $definitions = $grille->dico[strlen($mot)][$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)];
} }
@ -60,7 +58,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)][$mot]; $definitions = $grille->dico[strlen($mot)][$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)];
} }