<?php
function vetaNaSlova(string $v): array {
    return preg_split('/\s+/', trim(strtolower($v)));
}
$vety = file("trainset.txt", FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
$model = [];
// Pre každý riadok trénujeme bigram
foreach ($vety as $veta) {
    $slova = vetaNaSlova($veta);
    for ($i = 0; $i < count($slova) - 1; $i++) {
        $w1 = $slova[$i];
        $w2 = $slova[$i+1];

        if (!isset($model[$w1])) {
            $model[$w1] = [];
        }

        if (!isset($model[$w1][$w2])) {
            $model[$w1][$w2] = 0;
        }

        $model[$w1][$w2]++;
    }
}
file_put_contents("model.json", json_encode($model, JSON_PRETTY_PRINT));
echo "Model uložený do model.json\n";
?>
