This commit is contained in:
team 1
2026-05-05 08:16:45 +02:00
parent b259b6cd2d
commit de12386a98
11 changed files with 523 additions and 6 deletions

View File

@@ -208,6 +208,16 @@ final readonly class SearchRepairService
);
}
if (
$requestedAccessoryCodes === []
&& $this->isDirectProductAttributeLookup($prompt . ' ' . $primaryQuery)
) {
return $this->normalizeRepairQueries(
$this->buildDirectProductAttributeRepairQueries($prompt, $primaryQuery),
$primaryQuery
);
}
$topPrimaryName = $primaryShopResults[0]->name ?? '';
$topPrimaryProductNumber = $primaryShopResults[0]->productNumber ?? null;
$topPrimaryPhrase = trim($topPrimaryName . ' ' . ($topPrimaryProductNumber ?? ''));
@@ -259,6 +269,125 @@ final readonly class SearchRepairService
return $this->normalizeRepairQueries($queries, $primaryQuery);
}
/**
* @return string[]
*/
private function buildDirectProductAttributeRepairQueries(string $prompt, string $primaryQuery): array
{
$queries = [];
foreach ([$primaryQuery, $prompt] as $source) {
$query = $this->cleanupDirectProductAttributeRepairQuery($source);
if ($query !== '') {
$queries[] = $query;
}
}
return array_values(array_unique($queries));
}
private function cleanupDirectProductAttributeRepairQuery(string $source): string
{
$source = trim($source);
if ($source === '') {
return '';
}
$constraintTokens = $this->extractDirectProductAttributeConstraintTokens($source);
if ($constraintTokens === []) {
return '';
}
$removeTokens = array_fill_keys($constraintTokens, true);
foreach ($this->config->getDirectProductAttributeLookupStopTerms() as $term) {
foreach ($this->tokenize($term) as $token) {
$removeTokens[$token] = true;
}
}
$kept = [];
foreach ($this->tokenize($source) as $token) {
if (isset($removeTokens[$token]) || isset($kept[$token])) {
continue;
}
$kept[$token] = $token;
}
if (count($kept) < $this->config->getDirectProductAttributeLookupMinTokens()) {
return '';
}
$query = implode(' ', array_values($kept));
return $this->containsDirectProductTypeTerm($query) ? $query : '';
}
/**
* @return string[]
*/
private function extractDirectProductAttributeConstraintTokens(string $text): array
{
$tokens = [];
foreach ($this->config->getDirectProductAttributeLookupComparativeConstraintPatterns() as $pattern) {
if (@preg_match_all($pattern, $text, $matches, PREG_SET_ORDER) === false) {
continue;
}
foreach ($matches as $match) {
$value = $match['value'] ?? ($match[1] ?? '');
if (!is_scalar($value)) {
continue;
}
foreach ($this->tokenize((string) $value) as $token) {
$tokens[$token] = $token;
}
}
}
return array_values($tokens);
}
private function isDirectProductAttributeLookup(string $text): bool
{
return $this->config->isDirectProductAttributeLookupRepairEnabled()
&& $this->containsDirectProductTypeTerm($text)
&& $this->extractDirectProductAttributeConstraintTokens($text) !== [];
}
private function containsDirectProductTypeTerm(string $text): bool
{
$tokens = array_fill_keys($this->tokenize($text), true);
if ($tokens === []) {
return false;
}
foreach ($this->config->getDirectProductAttributeLookupProductTypeTerms() as $term) {
$termTokens = $this->tokenize($term);
if ($termTokens === []) {
continue;
}
$matches = true;
foreach ($termTokens as $termToken) {
if (!isset($tokens[$termToken])) {
$matches = false;
break;
}
}
if ($matches) {
return true;
}
}
return false;
}
/**
* @param string[] $queries
* @return string[]