ajustement de mots_espaces

This commit is contained in:
2025-04-25 02:11:21 +02:00
parent 64bc91d4a0
commit 4b69fa4803
5 changed files with 50 additions and 37 deletions

View File

@ -1,5 +1,10 @@
<?php
const MIN_LETTRES = 0;
const MAX_MOTS = 100000;
$dico = [];
if (($handle = fopen("dico.csv", "r")) !== FALSE) {
$header = fgetcsv($handle, 0, "\t");
@ -13,7 +18,7 @@ if (($handle = fopen("dico.csv", "r")) !== FALSE) {
fclose($handle);
}
$mots_de_n_lettres = [[]];
$mots_de_n_lettres = [];
foreach ($dico as $mot => $definition) {
$n = strlen($mot);
if (!isset($mots_de_n_lettres[$n])) {
@ -22,39 +27,41 @@ foreach ($dico as $mot => $definition) {
$mots_de_n_lettres[$n][] = $mot;
}
function fisherYatesShuffle(&$items, $seed)
function fisherYatesShuffle(&$items)
{
mt_srand($seed);
for ($i = count($items) - 1; $i > 0; $i--)
{
$j = @mt_rand(0, $i);
for ($i = count($items) - 1; $i > 0; $i--) {
$j = mt_rand(0, $i);
$tmp = $items[$i];
$items[$i] = $items[$j];
$items[$j] = $tmp;
}
mt_srand();
}
function mots_espaces($max, $min=0, $seed=0) {
function mots_espaces($longueur) {
global $mots_de_n_lettres;
global $dico;
if ($seed) {
fisherYatesShuffle($mots_de_n_lettres[$max], $seed);
} else {
shuffle($mots_de_n_lettres[$max]);
}
foreach($mots_de_n_lettres[$max] as $mot) {
$nb_mots = 0;
fisherYatesShuffle($mots_de_n_lettres[$longueur]);
foreach($mots_de_n_lettres[$longueur] as $mot) {
yield $mot;
$nb_mots++;
if ($nb_mots > MAX_MOTS) {
return;
}
}
for ($i = ceil($max / 2); $max - $i -1 >= $min; $i++) {
for ($i = 2; $longueur - $i - 1 >= MIN_LETTRES; $i++) {
foreach ($mots_de_n_lettres[$i] as $mot1) {
foreach (mots_espaces($max - $i -1, $min) as $mot2) {
foreach (mots_espaces($longueur - $i - 1) as $mot2) {
if ($mot1 != $mot2) {
$dico["$mot1 $mot2"] = $dico[$mot1] && $dico[$mot2] ? "{$dico[$mot1]}<br/>{$dico[$mot2]}." : $dico[$mot1] . $dico[$mot2];
yield "$mot1 $mot2";
$dico["$mot2 $mot1"] = $dico[$mot2] && $dico[$mot1] ? "{$dico[$mot2]}<br/>{$dico[$mot1]}." : $dico[$mot2] . $dico[$mot1];
yield "$mot2 $mot1";
$nb_mots += 2;
if ($nb_mots > MAX_MOTS) {
break;
}
}
}
}