77 lines
2.1 KiB
PHP
77 lines
2.1 KiB
PHP
|
|
<?php
|
||
|
|
|
||
|
|
declare(strict_types=1);
|
||
|
|
|
||
|
|
namespace Classifier\Lib;
|
||
|
|
|
||
|
|
use Classifier\Lib\ClassificationMethod;
|
||
|
|
use Classifier\Lib\Enum\MessageType;
|
||
|
|
|
||
|
|
class MessageClassificationResult
|
||
|
|
{
|
||
|
|
// FIXME : php8.1 readonly props
|
||
|
|
|
||
|
|
/** @var MessageType */
|
||
|
|
public $decision;
|
||
|
|
|
||
|
|
/** @var ClassificationMethod */
|
||
|
|
public $method;
|
||
|
|
|
||
|
|
/** @var float */
|
||
|
|
public $confidence;
|
||
|
|
|
||
|
|
/** @var array<string,float> */
|
||
|
|
private $details;
|
||
|
|
|
||
|
|
/** @var MessageType|null */
|
||
|
|
public $machineDecision;
|
||
|
|
|
||
|
|
public function __construct(
|
||
|
|
MessageType $decision,
|
||
|
|
ClassificationMethod $method,
|
||
|
|
float $confidence,
|
||
|
|
array $details = [],
|
||
|
|
?MessageType $machineDecision = null
|
||
|
|
) {
|
||
|
|
$this->decision = $decision;
|
||
|
|
$this->method = $method;
|
||
|
|
$this->confidence = $confidence;
|
||
|
|
$this->details = $details;
|
||
|
|
$this->machineDecision = $machineDecision;
|
||
|
|
|
||
|
|
// * Validation des détails
|
||
|
|
foreach ($details as $key => $value) {
|
||
|
|
if (empty($key) || !is_string($key)) {
|
||
|
|
throw new \InvalidArgumentException("Clé de détail invalide : " . var_export($key, true));
|
||
|
|
}
|
||
|
|
if (!is_float($value) && !is_int($value)) {
|
||
|
|
throw new \InvalidArgumentException("Valeur de détail pour '$key' invalide : " . var_export($value, true));
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
foreach ($details as $key => $value) {
|
||
|
|
if (empty($key) || !is_string($key)) {
|
||
|
|
throw new \InvalidArgumentException("Clé de détail invalide : " . var_export($key, true));
|
||
|
|
}
|
||
|
|
if (!is_float($value) && !is_int($value)) {
|
||
|
|
throw new \InvalidArgumentException("Valeur de détail pour '$key' invalide : " . var_export($value, true));
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
/** @return array{word:string,score:float}[] */
|
||
|
|
public function getDetails(): array
|
||
|
|
{
|
||
|
|
/** @var object{'word':string,'score':float}[] */
|
||
|
|
$output = [];
|
||
|
|
|
||
|
|
foreach ($this->details as $key => $value) {
|
||
|
|
$output[] = (object)[
|
||
|
|
'word' => $key,
|
||
|
|
'score' => $value
|
||
|
|
];
|
||
|
|
}
|
||
|
|
return $output;
|
||
|
|
}
|
||
|
|
}
|