Compare commits
6 Commits
c20d2324e9
...
grand-dico
Author | SHA1 | Date | |
---|---|---|---|
13fcdf05fb | |||
ca39709fbd | |||
ddebd453df | |||
d5a120cd9a | |||
ab9e1f08ef | |||
b1f3e8b85f |
4
.gitignore
vendored
4
.gitignore
vendored
@ -1,3 +1,5 @@
|
|||||||
__pycache__/
|
__pycache__/
|
||||||
test*.*
|
test*.*
|
||||||
*.py
|
*.py
|
||||||
|
dico2.csv
|
||||||
|
wiktionaryXfr2010.7z
|
||||||
|
33
Grille.php
33
Grille.php
@ -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)
|
||||||
@ -66,20 +66,23 @@ class Grille implements ArrayAccess
|
|||||||
{
|
{
|
||||||
[$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_ligne = $this->lettres_suivantes[$this->largeur];
|
$lettres_ligne = $this->dico[$this->largeur];
|
||||||
}
|
}
|
||||||
|
|
||||||
$lettres_colonne = $this->lettres_suivantes[$this->hauteur];
|
// en colonne
|
||||||
|
$lettres_colonne = $this->dico[$this->hauteur];
|
||||||
for ($y2 = 0; $y2 < $y; $y2++) {
|
for ($y2 = 0; $y2 < $y; $y2++) {
|
||||||
$lettres_colonne = $lettres_colonne->noeud[$this->grille[$y2][$x]];
|
$lettres_colonne = $lettres_colonne->branches[$this->grille[$y2][$x]];
|
||||||
}
|
}
|
||||||
$lettres_communes = array_intersect_key(
|
$lettres_communes = array_intersect_key(
|
||||||
$lettres_ligne->noeud,
|
$lettres_ligne->branches,
|
||||||
$lettres_colonne->noeud
|
$lettres_colonne->branches
|
||||||
);
|
);
|
||||||
foreach ($lettres_communes as $lettre => $_) {
|
foreach ($lettres_communes as $lettre => $_) {
|
||||||
$lettres_communes[$lettre] = count($lettres_ligne->noeud[$lettre]) * count($lettres_colonne->noeud[$lettre]) * gaussienne(1, 5);
|
$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) {
|
uksort($lettres_communes, function($a, $b) use ($lettres_communes) {
|
||||||
return $lettres_communes[$b] <=> $lettres_communes[$a];
|
return $lettres_communes[$b] <=> $lettres_communes[$a];
|
||||||
@ -89,6 +92,16 @@ class Grille implements ArrayAccess
|
|||||||
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));
|
||||||
@ -114,7 +127,7 @@ class Grille implements ArrayAccess
|
|||||||
}
|
}
|
||||||
|
|
||||||
if ($i < $this->nb_positions - 1) {
|
if ($i < $this->nb_positions - 1) {
|
||||||
yield from $this->gen_grilles($i + 1, $lettres_ligne->noeud[$lettre]);
|
yield from $this->gen_grilles($i + 1, $lettres_ligne->branches[$lettre]);
|
||||||
} else {
|
} else {
|
||||||
yield $this;
|
yield $this;
|
||||||
}
|
}
|
||||||
@ -158,7 +171,6 @@ class Grille implements ArrayAccess
|
|||||||
$this->grille
|
$this->grille
|
||||||
)
|
)
|
||||||
);
|
);
|
||||||
var_dump($_SESSION);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public function load($id)
|
public function load($id)
|
||||||
@ -167,7 +179,6 @@ class Grille implements ArrayAccess
|
|||||||
session_start(["use_cookies" => false]);
|
session_start(["use_cookies" => false]);
|
||||||
|
|
||||||
if (!isset($_SESSION["$this->largeur,$this->hauteur"])) {
|
if (!isset($_SESSION["$this->largeur,$this->hauteur"])) {
|
||||||
var_dump($_SESSION);
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
121
Trie.php
121
Trie.php
@ -2,74 +2,89 @@
|
|||||||
|
|
||||||
|
|
||||||
class Trie implements ArrayAccess, IteratorAggregate, Countable {
|
class Trie implements ArrayAccess, IteratorAggregate, Countable {
|
||||||
public array $noeud = [];
|
public array $branches = [];
|
||||||
private $nb_branches = 0;
|
private $nb_branches = 0;
|
||||||
|
|
||||||
public function offsetSet($cles, $valeur): void {
|
public function arraySet($cles, $valeur) {
|
||||||
if (!count($cles)) {
|
|
||||||
throw new \OutOfBoundsException("Liste de clés vide.");
|
|
||||||
}
|
|
||||||
$cle = array_shift($cles);
|
|
||||||
if (!isset($this->noeud[$cle])) $this->noeud[$cle] = new Trie();
|
|
||||||
$this->nb_branches++;
|
$this->nb_branches++;
|
||||||
if (count($cles)) {
|
$cle = $cles[0];
|
||||||
$this->noeud[$cle]->offsetSet($cles, $valeur);
|
$cles = array_slice($cles, 1);
|
||||||
|
if ($cles == []) {
|
||||||
|
$this->branches[$cle] = $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);
|
}
|
||||||
$this->nb_branches--;
|
|
||||||
if (count($cles)) {
|
public function offsetUnset($string): void {
|
||||||
$this->noeud[$cle]->offsetUnset($cles);
|
$this->arrayUnset(str_split($string));
|
||||||
if (count($this->noeud[$cle]) == 0) {
|
|
||||||
unset($this->noeud[$cle]);
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
unset($this->noeud[$cle]);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// 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;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
85
dico.php
85
dico.php
@ -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);
|
|
||||||
$longueur = strlen($mot);
|
|
||||||
if (!isset($dico[$longueur])) $dico[$longueur] = [];
|
|
||||||
if (!isset($dico[$longueur][$mot])) $dico[$longueur][$mot] = [];
|
|
||||||
if (strlen($definition)) $dico[$longueur][$mot][] = $definition;
|
|
||||||
}
|
}
|
||||||
fclose($lecteur);
|
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);
|
||||||
|
$dico[$longueur][$mot] = $definitions;
|
||||||
|
}
|
||||||
|
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;
|
||||||
}
|
}
|
||||||
|
@ -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;
|
||||||
|
|
||||||
@ -49,7 +48,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)][$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)];
|
||||||
}
|
}
|
||||||
@ -59,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)];
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user