rm CachedRetriever.php
add second shopsearch
This commit is contained in:
@@ -26,35 +26,43 @@ final readonly class CommerceQueryParser
|
||||
string $historyContext = ''
|
||||
): CommerceSearchQuery {
|
||||
$normalizedPrompt = $this->normalize($originalPrompt);
|
||||
$isDirectProductQuery = $this->isDirectProductQuery($normalizedPrompt);
|
||||
|
||||
[$priceMin, $priceMax] = $this->extractPriceRange($normalizedPrompt);
|
||||
$sizes = $this->extractSizes($normalizedPrompt);
|
||||
$brand = $this->extractBrand($normalizedPrompt);
|
||||
|
||||
$searchText = $this->buildSearchText(
|
||||
$normalizedPrompt,
|
||||
$sizes,
|
||||
$brand,
|
||||
$priceMin,
|
||||
$priceMax
|
||||
prompt: $normalizedPrompt,
|
||||
sizes: $sizes,
|
||||
brand: $brand,
|
||||
priceMin: $priceMin,
|
||||
priceMax: $priceMax,
|
||||
preserveDirectProductQuery: $isDirectProductQuery
|
||||
);
|
||||
|
||||
if ($historyContext !== '' && $this->shouldUseHistoryContext($normalizedPrompt)) {
|
||||
if (
|
||||
!$isDirectProductQuery
|
||||
&& $historyContext !== ''
|
||||
&& $this->shouldUseHistoryContext($normalizedPrompt)
|
||||
) {
|
||||
$latestHistoryQuestion = $this->extractLatestQuestionFromHistory($historyContext);
|
||||
|
||||
if ($latestHistoryQuestion !== '') {
|
||||
$normalizedHistoryPrompt = $this->normalize($latestHistoryQuestion);
|
||||
$isDirectHistoryProductQuery = $this->isDirectProductQuery($normalizedHistoryPrompt);
|
||||
|
||||
[$historyPriceMin, $historyPriceMax] = $this->extractPriceRange($normalizedHistoryPrompt);
|
||||
$historySizes = $this->extractSizes($normalizedHistoryPrompt);
|
||||
$historyBrand = $this->extractBrand($normalizedHistoryPrompt);
|
||||
|
||||
$historySearchText = $this->buildSearchText(
|
||||
$normalizedHistoryPrompt,
|
||||
$historySizes,
|
||||
$historyBrand,
|
||||
$historyPriceMin,
|
||||
$historyPriceMax
|
||||
prompt: $normalizedHistoryPrompt,
|
||||
sizes: $historySizes,
|
||||
brand: $historyBrand,
|
||||
priceMin: $historyPriceMin,
|
||||
priceMax: $historyPriceMax,
|
||||
preserveDirectProductQuery: $isDirectHistoryProductQuery
|
||||
);
|
||||
|
||||
$searchText = $this->mergeSearchTexts($historySearchText, $searchText);
|
||||
@@ -98,7 +106,7 @@ final readonly class CommerceQueryParser
|
||||
*/
|
||||
private function extractPriceRange(string $prompt): array
|
||||
{
|
||||
$priceMin = 0;
|
||||
$priceMin = null;
|
||||
$priceMax = null;
|
||||
|
||||
if (preg_match('/\bzwischen\s+(\d+(?:[.,]\d+)?)\s+und\s+(\d+(?:[.,]\d+)?)\s+euro\b/u', $prompt, $m) === 1) {
|
||||
@@ -165,19 +173,34 @@ final readonly class CommerceQueryParser
|
||||
array $sizes,
|
||||
?string $brand,
|
||||
?float $priceMin,
|
||||
?float $priceMax
|
||||
?float $priceMax,
|
||||
bool $preserveDirectProductQuery = false
|
||||
): string {
|
||||
if ($preserveDirectProductQuery) {
|
||||
return $this->buildDirectProductSearchText($prompt);
|
||||
}
|
||||
|
||||
$text = ' ' . $prompt . ' ';
|
||||
|
||||
foreach ($this->config->getPhrasesToRemove() as $phrase) {
|
||||
$text = str_replace($phrase, ' ', $text);
|
||||
$normalizedPhrase = $this->normalize((string) $phrase);
|
||||
if ($normalizedPhrase === '') {
|
||||
continue;
|
||||
}
|
||||
|
||||
$text = str_replace(' ' . $normalizedPhrase . ' ', ' ', $text);
|
||||
}
|
||||
|
||||
foreach ($sizes as $size) {
|
||||
$text = preg_replace('/\b' . preg_quote($size, '/') . '\b/u', ' ', $text) ?? $text;
|
||||
$normalizedSize = $this->normalize((string) $size);
|
||||
if ($normalizedSize === '') {
|
||||
continue;
|
||||
}
|
||||
|
||||
$text = preg_replace('/\b' . preg_quote($normalizedSize, '/') . '\b/u', ' ', $text) ?? $text;
|
||||
}
|
||||
|
||||
if ($brand !== null && $brand !== '') {
|
||||
if ($brand !== null && $brand !== '' && !$this->isBrandPartOfModelPhrase($prompt, $brand)) {
|
||||
$text = preg_replace('/\b' . preg_quote($brand, '/') . '\b/u', ' ', $text) ?? $text;
|
||||
}
|
||||
|
||||
@@ -200,6 +223,22 @@ final readonly class CommerceQueryParser
|
||||
return trim(implode(' ', $tokens));
|
||||
}
|
||||
|
||||
private function buildDirectProductSearchText(string $prompt): string
|
||||
{
|
||||
$text = $prompt;
|
||||
$text = preg_replace('/\s+/u', ' ', $text) ?? $text;
|
||||
$text = trim($text, " \t\n\r\0\x0B-.,");
|
||||
|
||||
$tokens = array_filter(
|
||||
explode(' ', $text),
|
||||
static fn(string $token): bool => mb_strlen($token) > 0
|
||||
);
|
||||
|
||||
$tokens = array_values(array_unique($tokens));
|
||||
|
||||
return trim(implode(' ', $tokens));
|
||||
}
|
||||
|
||||
private function shouldUseHistoryContext(string $prompt): bool
|
||||
{
|
||||
return preg_match(
|
||||
@@ -263,6 +302,57 @@ final readonly class CommerceQueryParser
|
||||
));
|
||||
}
|
||||
|
||||
private function isDirectProductQuery(string $prompt): bool
|
||||
{
|
||||
if ($prompt === '') {
|
||||
return false;
|
||||
}
|
||||
|
||||
if ($this->containsModelLikePhrase($prompt)) {
|
||||
return true;
|
||||
}
|
||||
|
||||
if ($this->containsAccessoryLikePhrase($prompt)) {
|
||||
return true;
|
||||
}
|
||||
|
||||
$tokens = preg_split('/\s+/u', $prompt, -1, PREG_SPLIT_NO_EMPTY) ?: [];
|
||||
|
||||
if (count($tokens) <= 4 && preg_match('/\d/u', $prompt) === 1) {
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
private function containsModelLikePhrase(string $text): bool
|
||||
{
|
||||
return preg_match(
|
||||
'/\b[a-zäöüß][a-zäöüß®\-]*(?:\s+[a-zäöüß][a-zäöüß®\-]*){0,2}\s+\d{2,5}[a-z0-9\-]*\b/u',
|
||||
$text
|
||||
) === 1;
|
||||
}
|
||||
|
||||
private function containsAccessoryLikePhrase(string $text): bool
|
||||
{
|
||||
return preg_match(
|
||||
'/\b(?:indikator|indicator|reagenz|reagent|kit|set)\s+\d{1,5}[a-z0-9\-]*\b/u',
|
||||
$text
|
||||
) === 1;
|
||||
}
|
||||
|
||||
private function isBrandPartOfModelPhrase(string $prompt, string $brand): bool
|
||||
{
|
||||
if ($brand === '') {
|
||||
return false;
|
||||
}
|
||||
|
||||
return preg_match(
|
||||
'/\b' . preg_quote($brand, '/') . '\s+\d{2,5}[a-z0-9\-]*\b/u',
|
||||
$prompt
|
||||
) === 1;
|
||||
}
|
||||
|
||||
private function toFloat(string $value): ?float
|
||||
{
|
||||
$value = str_replace(',', '.', trim($value));
|
||||
|
||||
Reference in New Issue
Block a user