Search: a single word keeps only its closest neighbours (Q9)
For a phrase of one word (stop words aside), the model gives two good records then a plateau of records close by spelling only: « datte » kept 246 neighbours, now 4. Countries and chosen terms count as words, so « pigment Italie » keeps its list. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -106,6 +106,12 @@ class Base extends ControllerBase {
|
||||
*/
|
||||
public const KNN_SIGMA = 2.0;
|
||||
|
||||
/**
|
||||
* For a single word typed, neighbours within this gap to the best score only
|
||||
* (Q9): « datte » kept 246 neighbours, 4 with it.
|
||||
*/
|
||||
public const KNN_SINGLE_WORD_MARGIN = 0.04;
|
||||
|
||||
/**
|
||||
* Best text matches merged with the neighbours (Q8).
|
||||
*
|
||||
@@ -167,6 +173,12 @@ class Base extends ControllerBase {
|
||||
*/
|
||||
private array $filterNames = [];
|
||||
|
||||
/**
|
||||
* Whether the whole phrase is a single word, stop words aside (Q9): a
|
||||
* country or a term is a word too, « pigment Italie » is two.
|
||||
*/
|
||||
private bool $singleWord = FALSE;
|
||||
|
||||
/**
|
||||
* Most terms taken from a request (Q7).
|
||||
*/
|
||||
@@ -242,6 +254,7 @@ class Base extends ControllerBase {
|
||||
$words = KeywordText::words($text, $lang);
|
||||
// Only stop words: search the phrase as typed, as before.
|
||||
$keys = $words ? implode(' ', $words) : implode(' ', $this->keys);
|
||||
$this->singleWord = count(KeywordText::words($this->phrase, $lang)) === 1;
|
||||
|
||||
// Terms chosen in the autocomplete (Q7) and a country named in the
|
||||
// phrase (Q6) are conditions, not keywords: « pigment Italie » searches
|
||||
@@ -345,6 +358,9 @@ class Base extends ControllerBase {
|
||||
$or = array_replace(array_flip($text_order), $or);
|
||||
}
|
||||
$neighbours = $neighbours === NULL ? [] : ScoreCut::aboveMean($neighbours, self::KNN_SIGMA);
|
||||
if ($this->singleWord) {
|
||||
$neighbours = ScoreCut::nearBest($neighbours, self::KNN_SINGLE_WORD_MARGIN);
|
||||
}
|
||||
$this->knn_count += count($neighbours);
|
||||
$weights = $neighbours ? [0 => self::TEXT_WITH_KNN_WEIGHT] : [];
|
||||
$nids = RankFusion::fuse(array_keys($exact), array_filter([array_keys($or), array_keys($neighbours)]), RankFusion::K, $weights);
|
||||
|
||||
@@ -41,4 +41,29 @@ final class ScoreCut {
|
||||
return array_filter($scores, static fn ($score) => $score > $limit);
|
||||
}
|
||||
|
||||
/**
|
||||
* Scores within $margin of the best one, in their order (Q9).
|
||||
*
|
||||
* For a single word the model has almost no meaning to compare: after
|
||||
* the few records that hold it, a plateau of records that only look alike
|
||||
* (« datte »: DATEFORM 0.772, Datecrete 0.758, then Damasteel, DalMagnetic
|
||||
* around 0.72). A meaningful word keeps a slow decline (« liège »: 37
|
||||
* records within 0.04).
|
||||
*
|
||||
* @param array<int, float> $scores
|
||||
* Scores keyed by nid.
|
||||
* @param float $margin
|
||||
* Largest gap to the best score.
|
||||
*
|
||||
* @return array<int, float>
|
||||
* The kept scores, keyed by nid.
|
||||
*/
|
||||
public static function nearBest(array $scores, float $margin): array {
|
||||
if (!$scores) {
|
||||
return $scores;
|
||||
}
|
||||
$limit = max($scores) - $margin;
|
||||
return array_filter($scores, static fn ($score) => $score >= $limit);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -44,4 +44,14 @@ final class ScoreCutTest extends UnitTestCase {
|
||||
$this->assertSame([], ScoreCut::aboveMean([], 2.0));
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Near the best score only: the plateau after a gap goes (Q9).
|
||||
*/
|
||||
public function testNearBest(): void {
|
||||
$scores = [1 => 0.772, 2 => 0.758, 3 => 0.739, 4 => 0.729, 5 => 0.722];
|
||||
$this->assertSame([1, 2, 3], array_keys(ScoreCut::nearBest($scores, 0.04)));
|
||||
$this->assertSame([], ScoreCut::nearBest([], 0.04));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user