From 36086fd1915f5c9599caa7596ee4a0b82181089a Mon Sep 17 00:00:00 2001 From: bach Date: Tue, 1 Dec 2026 15:10:49 +0100 Subject: [PATCH] Search: a single word keeps only its closest neighbours (Q9) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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 --- .../materio_sapi/src/Controller/Base.php | 16 ++++++++++++ .../custom/materio_sapi/src/ScoreCut.php | 25 +++++++++++++++++++ .../tests/src/Unit/ScoreCutTest.php | 10 ++++++++ 3 files changed, 51 insertions(+) diff --git a/web/modules/custom/materio_sapi/src/Controller/Base.php b/web/modules/custom/materio_sapi/src/Controller/Base.php index 0523b8b6..c1769435 100644 --- a/web/modules/custom/materio_sapi/src/Controller/Base.php +++ b/web/modules/custom/materio_sapi/src/Controller/Base.php @@ -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); diff --git a/web/modules/custom/materio_sapi/src/ScoreCut.php b/web/modules/custom/materio_sapi/src/ScoreCut.php index 09b11c1b..b8b69f4c 100644 --- a/web/modules/custom/materio_sapi/src/ScoreCut.php +++ b/web/modules/custom/materio_sapi/src/ScoreCut.php @@ -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 $scores + * Scores keyed by nid. + * @param float $margin + * Largest gap to the best score. + * + * @return array + * 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); + } + } diff --git a/web/modules/custom/materio_sapi/tests/src/Unit/ScoreCutTest.php b/web/modules/custom/materio_sapi/tests/src/Unit/ScoreCutTest.php index 516f4659..6d9ca644 100644 --- a/web/modules/custom/materio_sapi/tests/src/Unit/ScoreCutTest.php +++ b/web/modules/custom/materio_sapi/tests/src/Unit/ScoreCutTest.php @@ -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)); + } + }