synthèse vocale

This commit is contained in:
2026-01-17 23:49:13 +01:00
parent a1606819a1
commit ee3c443464
5 changed files with 262 additions and 124 deletions

View File

@@ -3,18 +3,59 @@ 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
$reponse = `python markov.py`;
$suivants = [];
$total = [];
// désactiver le buffering PHP
@ini_set('output_buffering', 'off');
@ini_set('zlib.output_compression', 'off');
while (ob_get_level()) ob_end_flush();
flush();
// 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");
}
// envoyer chaque lettre avec un délai
$len = strlen($reponse);
for ($i = 0; $i < $len; $i++) {
echo $reponse[$i];
flush(); // forcer l'envoi immédiat
usleep(40000); // 40ms = 40000 microsecondes
}
$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";