Files
ChatBeta/question.php
2026-01-17 23:49:13 +01:00

62 lines
1.9 KiB
PHP

<?php
header('Content-Type: text/plain; charset=utf-8');
header('Cache-Control: no-cache'); // pour éviter le buffering du navigateur
header('X-Accel-Buffering: no'); // si nginx, pour désactiver le buffering
$suivants = [];
$total = [];
// Lire le fichier fra_mixed_2009_100K-sentences.txt
$fichier = fopen("fra_mixed_2009_10K-sentences.txt", "r");
if ($fichier) {
while (($phrase = fgets($fichier)) !== false) {
$antepenultieme = "";
$penultieme = "";
$mots = explode(" ", trim($phrase));
foreach ($mots as $mot) {
$cle = $antepenultieme . " " . $penultieme;
if (!isset($suivants[$cle])) {
$suivants[$cle] = [];
}
if (!isset($suivants[$cle][$mot])) {
$suivants[$cle][$mot] = 0;
}
$suivants[$cle][$mot]++;
if (!isset($total[$cle])) {
$total[$cle] = 0;
}
$total[$cle]++;
$antepenultieme = $penultieme;
$penultieme = $mot;
}
}
fclose($fichier);
} else {
http_response_code(500);
die("Impossible d'ouvrir le fichier fra_mixed_2009_10K-sentences.txt\n");
}
$phrases = [];
for ($i = 0; $i < rand(1, 3); $i++) {
$antepenultieme = "";
$penultieme = "";
$phrase = [];
while (isset($suivants[$antepenultieme . " " . $penultieme]) && !empty($suivants[$antepenultieme . " " . $penultieme])) {
$cle = $antepenultieme . " " . $penultieme;
$choix = rand(1, $total[$cle]);
foreach ($suivants[$cle] as $mot => $occurences) {
if ($choix <= $occurences) {
$suivant = $mot;
break;
}
$choix -= $occurences;
}
$phrase[] = $suivant;
$antepenultieme = $penultieme;
$penultieme = $suivant;
}
$phrases[] = implode(" ", $phrase);
}
echo implode("\n", $phrases) . "\n";