cii-text-analysis/save/Lib/StringClassifier.php.save

132 lines
3.8 KiB
Text
Raw Normal View History

2026-02-10 16:19:59 +01:00
<?php
declare(strict_types=1);
namespace Classifier\Lib;
use Symfony\Component\Console\Style\SymfonyStyle;
use Symfony\Component\Console\Input\ArgvInput;
use Symfony\Component\Console\Output\ConsoleOutput;
class StringClassifier
{
const OUTPUT_DIR = __DIR__ . '/../output';
const GOOD_FILE = self::OUTPUT_DIR . '/positive.php';
const BAD_FILE = self::OUTPUT_DIR . '/negative.php';
const IGNORE_FILE = self::OUTPUT_DIR . '/ignored.php'; // Nouveau fichier
/** @var string[] */
private $strings = [];
/** @var SymfonyStyle */
private $io;
/** @var array{good: string[], bad: string[], ignored: string[]} */
private $results = ['good' => [], 'bad' => [], 'ignored' => []];
public function __construct(array $strings)
{
$this->io = new SymfonyStyle(new ArgvInput(), new ConsoleOutput());
$this->loadExistingData();
$cleaned = array_unique(array_map(function (string $value): string {
return preg_replace('/\s+/', ' ', trim(mb_strtolower($value)));
}, $strings));
$alreadyDone = array_merge(
$this->results['good'],
$this->results['bad'],
$this->results['ignored']
);
$this->strings = array_diff($cleaned, $alreadyDone);
if (!is_dir(self::OUTPUT_DIR)) {
mkdir(self::OUTPUT_DIR, 0777, true);
}
}
private function loadExistingData(): void
{
$files = [
'good' => self::GOOD_FILE,
'bad' => self::BAD_FILE,
'ignored' => self::IGNORE_FILE
];
foreach ($files as $key => $path) {
if (file_exists($path)) {
$this->results[$key] = include $path;
}
}
}
public function runQuestions(): void
{
$count = count($this->strings);
if ($count === 0) {
$this->io->success("Tous les messages fournis sont déjà labellisés ou ignorés !");
return;
}
$this->io->title('Labellisation des données (Mode Résilient)');
$this->io->note([
"Appuyez sur 'i' pour écarter un message définitivement.",
"Appuyez sur 'q' pour sauvegarder et quitter."
]);
$this->io->progressStart($count);
foreach ($this->strings as $string) {
$this->io->newLine(2);
$this->io->text("<info>Message :</info> $string");
$answer = $this->io->choice(
'Action pour ce message ?',
[
'y' => 'Oui (Alerte)',
'n' => 'Non (Autre)',
'i' => 'Ignorer (Passer)',
'q' => 'Quitter et Sauvegarder'
],
'n'
);
if ($answer === 'q') {
$this->io->note("Interruption demandée...");
break;
}
if ($answer === 'y') {
$this->results['good'][] = $string;
} elseif ($answer === 'i') {
$this->results['ignored'][] = $string;
} else {
$this->results['bad'][] = $string;
}
$this->io->progressAdvance();
}
$this->io->progressFinish();
$this->saveResults();
$this->io->success("Sauvegarde effectuée. Ignorés : " . count($this->results['ignored']));
}
private function saveResults(): void
{
$this->exportToFile(self::GOOD_FILE, $this->results['good']);
$this->exportToFile(self::BAD_FILE, $this->results['bad']);
$this->exportToFile(self::IGNORE_FILE, $this->results['ignored']);
}
private function exportToFile(string $path, array $data): void
{
sort($data);
$content = "<?php\n\nreturn " . var_export($data, true) . ";\n";
file_put_contents($path, $content);
}
}