optimize data retrieve by customfields und enricher
This commit is contained in:
128
src/Knowledge/Retrieval/QueryEnricher.php
Normal file
128
src/Knowledge/Retrieval/QueryEnricher.php
Normal file
@@ -0,0 +1,128 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Knowledge\Retrieval;
|
||||
|
||||
final class QueryEnricher
|
||||
{
|
||||
public function enrichPrompt(string $query): string
|
||||
{
|
||||
// Return early if the input is empty or contains only whitespace.
|
||||
if (trim($query) === '') {
|
||||
return '';
|
||||
}
|
||||
|
||||
// Keep the original query untouched for the final output.
|
||||
$originalQuery = $query;
|
||||
|
||||
// Normalize the query for case-insensitive matching.
|
||||
$normalizedQuery = $this->normalize($query);
|
||||
|
||||
// Expect an associative array like:
|
||||
// [
|
||||
// 'hose' => 'jeans',
|
||||
// 'jacke' => 'mantel',
|
||||
// ]
|
||||
$mapping = $this->enrichQueryList();
|
||||
|
||||
// Build a bidirectional lookup table:
|
||||
// key -> value
|
||||
// value -> key
|
||||
$lookup = $this->buildBidirectionalLookup($mapping);
|
||||
|
||||
// Split the query into searchable words/tokens.
|
||||
$tokens = $this->tokenize($normalizedQuery);
|
||||
|
||||
$matches = [];
|
||||
|
||||
foreach ($tokens as $token) {
|
||||
// If the token exists in the lookup table, add the mapped counterpart.
|
||||
if (isset($lookup[$token])) {
|
||||
$matches[] = $lookup[$token];
|
||||
}
|
||||
}
|
||||
|
||||
// Remove duplicates while preserving order.
|
||||
$matches = array_values(array_unique($matches));
|
||||
|
||||
// If nothing was found, return the original query unchanged.
|
||||
if ($matches === []) {
|
||||
return $originalQuery;
|
||||
}
|
||||
|
||||
// Append the matched counterpart terms to the original prompt.
|
||||
return $originalQuery . " | Pseudonyme: " . implode(', ', $matches);
|
||||
}
|
||||
|
||||
/**
|
||||
* Normalize a string for case-insensitive comparison.
|
||||
*/
|
||||
private function normalize(string $value): string
|
||||
{
|
||||
return mb_strtolower(trim($value), 'UTF-8');
|
||||
}
|
||||
|
||||
/**
|
||||
* Tokenize the query into words.
|
||||
* Splits on everything that is not a letter or number.
|
||||
*/
|
||||
private function tokenize(string $value): array
|
||||
{
|
||||
return preg_split('/[^\p{L}\p{N}]+/u', $value, -1, PREG_SPLIT_NO_EMPTY) ?: [];
|
||||
}
|
||||
|
||||
/**
|
||||
* Build a lookup table that works in both directions.
|
||||
*
|
||||
* Example:
|
||||
* [
|
||||
* 'hose' => 'jeans',
|
||||
* 'jacke' => 'mantel',
|
||||
* ]
|
||||
*
|
||||
* becomes:
|
||||
* [
|
||||
* 'hose' => 'jeans',
|
||||
* 'jeans' => 'hose',
|
||||
* 'jacke' => 'mantel',
|
||||
* 'mantel' => 'jacke',
|
||||
* ]
|
||||
*/
|
||||
private function buildBidirectionalLookup(array $mapping): array
|
||||
{
|
||||
$lookup = [];
|
||||
|
||||
foreach ($mapping as $key => $value) {
|
||||
$key = trim((string)$key);
|
||||
$value = trim((string)$value);
|
||||
|
||||
// Skip incomplete pairs.
|
||||
if ($key === '' || $value === '') {
|
||||
continue;
|
||||
}
|
||||
|
||||
$normalizedKey = $this->normalize($key);
|
||||
$normalizedValue = $this->normalize($value);
|
||||
|
||||
// If the key is found in the query, return the value.
|
||||
$lookup[$normalizedKey] = $value;
|
||||
|
||||
// If the value is found in the query, return the key.
|
||||
$lookup[$normalizedValue] = $key;
|
||||
}
|
||||
|
||||
return $lookup;
|
||||
}
|
||||
|
||||
public function enrichQueryList(): array
|
||||
{
|
||||
return [
|
||||
'Wasserhärte' => "Resthärte",
|
||||
'Gerät' => 'Modell',
|
||||
'Indikator' => 'Chemie',
|
||||
'Wasserhärte-Grenzwert'=>'Resthärte',
|
||||
'Resthärte-Grenzwert'=>'Wasserhärte'
|
||||
];
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user