third step
This commit is contained in:
@@ -4,71 +4,72 @@ declare(strict_types=1);
|
||||
|
||||
namespace App\Config;
|
||||
|
||||
class IntentLightConfig
|
||||
/**
|
||||
* YAML-backed deterministic list-intent configuration.
|
||||
*
|
||||
* This class intentionally has no PHP fallback values. Missing or invalid
|
||||
* configuration must be fixed in config/retriex/intent.yaml.
|
||||
*/
|
||||
final class IntentLightConfig
|
||||
{
|
||||
public const LIST_THRESHOLD = 4;
|
||||
|
||||
private const QUANTITY_WORDS = [
|
||||
'alle',
|
||||
'sämtliche',
|
||||
'saemtliche',
|
||||
'mehrere',
|
||||
'verschiedene',
|
||||
'einige',
|
||||
'viele',
|
||||
'optionen',
|
||||
'möglichkeiten',
|
||||
'moeglichkeiten',
|
||||
'varianten',
|
||||
'arten',
|
||||
'modelle',
|
||||
'funktionen',
|
||||
'punkte',
|
||||
'schritte',
|
||||
'kategorien',
|
||||
'übersicht',
|
||||
'uebersicht',
|
||||
];
|
||||
|
||||
private const STRONG_PATTERNS = [
|
||||
'/\bliste(n)?\b/u',
|
||||
'/\bauflisten\b/u',
|
||||
'/\baufz(a|ä)hl(en)?\b/u',
|
||||
'/\bnenn(e)?\b/u',
|
||||
'/\bzeig(e)?\b/u',
|
||||
'/\bwelche\s+sind\b/u',
|
||||
'/\bwelche\s+gibt\s+es\b/u',
|
||||
'/\bwas\s+sind\b/u',
|
||||
'/\bwie\s+viele\b/u',
|
||||
'/\branking\b/u',
|
||||
'/\btop\s*\d+\b/u',
|
||||
];
|
||||
|
||||
/**
|
||||
* @param array<string, mixed> $config
|
||||
*/
|
||||
public function __construct(private readonly array $config = [])
|
||||
public function __construct(private readonly array $config)
|
||||
{
|
||||
}
|
||||
|
||||
public function getListThreshold(): int
|
||||
{
|
||||
return $this->requiredPositiveInt('list_threshold');
|
||||
}
|
||||
|
||||
/** @return string[] */
|
||||
public function getQuantityWords(): array
|
||||
{
|
||||
return $this->stringList('quantity_words', self::QUANTITY_WORDS);
|
||||
return $this->requiredStringList('quantity_words');
|
||||
}
|
||||
|
||||
/** @return string[] */
|
||||
public function getStrongPatterns(): array
|
||||
{
|
||||
return $this->stringList('strong_patterns', self::STRONG_PATTERNS);
|
||||
return $this->requiredStringList('strong_patterns');
|
||||
}
|
||||
|
||||
private function requiredPositiveInt(string $key): int
|
||||
{
|
||||
if (!array_key_exists($key, $this->config)) {
|
||||
throw new \InvalidArgumentException(sprintf('Missing required RetrieX light intent config key "%s".', $key));
|
||||
}
|
||||
|
||||
$value = $this->config[$key];
|
||||
|
||||
if (is_int($value)) {
|
||||
$intValue = $value;
|
||||
} elseif (is_string($value) && preg_match('/^-?\d+$/', trim($value)) === 1) {
|
||||
$intValue = (int) trim($value);
|
||||
} else {
|
||||
throw new \InvalidArgumentException(sprintf('RetrieX light intent config key "%s" must be an integer.', $key));
|
||||
}
|
||||
|
||||
if ($intValue <= 0) {
|
||||
throw new \InvalidArgumentException(sprintf('RetrieX light intent config key "%s" must be greater than 0.', $key));
|
||||
}
|
||||
|
||||
return $intValue;
|
||||
}
|
||||
|
||||
/** @return string[] */
|
||||
private function stringList(string $key, array $default): array
|
||||
private function requiredStringList(string $key): array
|
||||
{
|
||||
$value = $this->config[$key] ?? $default;
|
||||
if (!array_key_exists($key, $this->config)) {
|
||||
throw new \InvalidArgumentException(sprintf('Missing required RetrieX light intent config key "%s".', $key));
|
||||
}
|
||||
|
||||
$value = $this->config[$key];
|
||||
|
||||
if (!is_array($value)) {
|
||||
return $default;
|
||||
throw new \InvalidArgumentException(sprintf('RetrieX light intent config key "%s" must be a list.', $key));
|
||||
}
|
||||
|
||||
$out = [];
|
||||
@@ -78,13 +79,19 @@ class IntentLightConfig
|
||||
}
|
||||
|
||||
$item = trim((string) $item);
|
||||
if ($item === '' || in_array($item, $out, true)) {
|
||||
if ($item === '') {
|
||||
continue;
|
||||
}
|
||||
|
||||
$out[] = $item;
|
||||
if (!in_array($item, $out, true)) {
|
||||
$out[] = $item;
|
||||
}
|
||||
}
|
||||
|
||||
return $out !== [] ? $out : $default;
|
||||
if ($out === []) {
|
||||
throw new \InvalidArgumentException(sprintf('RetrieX light intent config key "%s" must not be empty.', $key));
|
||||
}
|
||||
|
||||
return $out;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user