add new configs

This commit is contained in:
team 1
2026-04-15 08:46:26 +02:00
parent 8cac77ed31
commit 1815a42035
18 changed files with 508 additions and 309 deletions

View File

@@ -5,27 +5,22 @@ declare(strict_types=1);
namespace App\Commerce;
use App\Commerce\Dto\CommerceSearchQuery;
use App\Config\CommerceIntentConfig;
use App\Config\CommerceQueryParserConfig;
use App\Knowledge\Retrieval\QueryCleaner;
use App\Knowledge\Text\TextNormalizer;
final class CommerceQueryParser
final readonly class CommerceQueryParser
{
public function __construct(
private readonly TextNormalizer $textNormalizer,
private readonly QueryCleaner $queryCleaner,
private TextNormalizer $textNormalizer,
private QueryCleaner $queryCleaner,
private CommerceQueryParserConfig $config,
private CommerceIntentConfig $intentConfig,
)
{
}
/**
* @var string[]
*/
private array $knownBrands = [
'heyl',
'horiba'
];
public function parse(string $originalPrompt, string $intent): CommerceSearchQuery
{
$normalized = $this->normalize($originalPrompt);
@@ -103,7 +98,8 @@ final class CommerceQueryParser
{
$sizes = [];
if (preg_match_all('/\b(?:größe|groesse|grösse)\s*([a-z0-9.-]+)\b/u', $prompt, $matches) === false) {
$sizePattern = $this->intentConfig->getSizePattern();
if (preg_match_all('/\b(?:' . $sizePattern . ')\s*([a-z0-9.-]+)\b/u', $prompt, $matches) === false) {
return [];
}
@@ -111,7 +107,8 @@ final class CommerceQueryParser
$sizes[] = trim($size);
}
if (preg_match_all('/\b(xs|s|m|l|xl|xxl|xxxl)\b/u', $prompt, $tokenMatches) !== false) {
$sizeTokenPattern = $this->intentConfig->getSizeTokenPattern();
if (preg_match_all('/\b(' . $sizeTokenPattern . ')\b/u', $prompt, $tokenMatches) !== false) {
foreach ($tokenMatches[1] as $sizeToken) {
$sizes[] = trim($sizeToken);
}
@@ -122,16 +119,12 @@ final class CommerceQueryParser
private function extractBrand(string $prompt): ?string
{
foreach ($this->knownBrands as $brand) {
foreach ($this->config->getKnownBrands() as $brand) {
if (str_contains($prompt, $brand)) {
return $brand;
}
}
if (preg_match('/\bheyl\s+([a-z0-9][a-z0-9\s\-]+)/u', $prompt, $m) === 1) {
return trim($m[1]);
}
return null;
}
@@ -145,20 +138,7 @@ final class CommerceQueryParser
{
$text = ' ' . $prompt . ' ';
$phrasesToRemove = [
'ich suche',
'suche',
'habt ihr',
'gibt es',
'zeige mir',
'welches gerät',
'welche gerät',
'welches modell',
'welches ist besser',
'welches ist am besten',
'alternative',
'alternativen',
];
$phrasesToRemove = $this->config->getPhrasesToRemove();
foreach ($phrasesToRemove as $phrase) {
$text = str_replace($phrase, ' ', $text);
@@ -173,11 +153,9 @@ final class CommerceQueryParser
}
if ($priceMin !== null || $priceMax !== null) {
if ($priceMin !== null || $priceMax !== null) {
$text = preg_replace('/\bzwischen\s+\d+(?:[.,]\d+)?\s+und\s+\d+(?:[.,]\d+)?\s*euro\b/u', ' ', $text) ?? $text;
$text = preg_replace('/\b(?:unter|bis|max(?:imal)?|ab|mindestens|min)\s+\d+(?:[.,]\d+)?\s*euro\b/u', ' ', $text) ?? $text;
$text = preg_replace('/\beuro\b/u', ' ', $text) ?? $text;
}
$text = preg_replace('/\bzwischen\s+\d+(?:[.,]\d+)?\s+und\s+\d+(?:[.,]\d+)?\s*euro\b/u', ' ', $text) ?? $text;
$text = preg_replace('/\b(?:unter|bis|max(?:imal)?|ab|mindestens|min)\s+\d+(?:[.,]\d+)?\s*euro\b/u', ' ', $text) ?? $text;
$text = preg_replace('/\b'.$this->intentConfig->getPricePattern().'\b/u', ' ', $text) ?? $text;
}
$text = preg_replace('/\s+/u', ' ', $text) ?? $text;