<?php
use Phpml\Classification\SVC;
use Phpml\SupportVectorMachine\Kernel;
require 'vendor/autoload.php';
// Veta → pole slov
function vetaNaSlova(string $v): array {
    return preg_split('/\s+/', trim($v));
}
$vety = file("trainset.txt", FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
// Zisti najdlhšiu vetu
$maxIndex = 0;
foreach ($vety as $v) {
    $slova = vetaNaSlova($v);
    $maxIndex = max($maxIndex, count($slova));
}
if (!is_dir("models")) {
    mkdir("models");
}
echo "Trénujem modely...\n";
for ($index = 0; $index < $maxIndex; $index++) {
    $samples = [];
    $labels  = [];
    foreach ($vety as $v) {
        $slova = vetaNaSlova($v);
        if (isset($slova[$index])) {
            $samples[] = [$index];          // vstup
            $labels[]  = $slova[$index];    // label
        }
    }
    // Musíme mať aspoň 2 rôzne slová
    if (count(array_unique($labels)) < 2) {
        echo "Index $index: preskakujem (málo unikátnych slov)\n";
        continue;
    }
    $clf = new SVC(Kernel::LINEAR, 1.0);
    $clf->train($samples, $labels);
    file_put_contents("models/model_$index.model", serialize($clf));
    echo "Index $index: uložený model (".count($labels)." príkladov)\n";
}
echo "Hotovo!\n";
