|
10 | 10 | */ |
11 | 11 | class RegionService |
12 | 12 | { |
13 | | - private const SCORE_EXACT = 6; // Exact match |
14 | | - private const SCORE_EXACT_SUFFIX_PREFIX = 5; // Matches like `Regio X`, `Provincie X`, or `X + deelgemeenten` for search string `X` |
| 13 | + private const SCORE_EXACT = 8; // Exact match |
| 14 | + private const SCORE_PROVINCE = 7; // Exact match with "Provincie X" |
| 15 | + private const SCORE_REGION = 6; // Exact match with "Regio X" |
| 16 | + private const SCORE_TOWN_WITH_SUBMUNICIPALITIES = 5; // Exact match with "X + deelgemeenten" |
15 | 17 | private const SCORE_TYPEAHEAD_COMPLETE = 4; // Typeahead match with a whitespace afterward, e.g. `Zele (Zele)` for search string `Zele` |
16 | 18 | private const SCORE_TYPEAHEAD_PARTIAL = 3; // Typeahead match with a non-whitespace character afterward, e.g. `Zelem (Halen)` for search string `Zele` |
17 | 19 | private const SCORE_SUBSTRING = 2; // If the search string is not at the start but after a ( or -, e.g. `Hypothetical example (Zele)` for search string `Zele` |
@@ -72,14 +74,31 @@ public function getAutocompletResults($searchString, $language = 'nl') |
72 | 74 | continue; |
73 | 75 | } |
74 | 76 |
|
75 | | - // Exact matches with a known suffix / prefix like `Regio X`, `X + deelgemeenten`, ... get the 2nd |
76 | | - // highest score. (Keep in mind that everything is already converted to lowercase) |
77 | | - // Otherwise "Provincie Antwerpen" would get buried by all submunicipalities of Antwerpen |
78 | | - if ($compareString === 'regio ' . $searchString || |
79 | | - $compareString === $searchString . ' + deelgemeenten' || |
80 | | - $compareString === 'provincie ' . $searchString) { |
| 77 | + // Next we look for exact matches with common prefixes/suffixes like |
| 78 | + // "Provincie", "Regio", and "+ deelgemeenten" |
| 79 | + // We give each one a slightly different score to avoid that province names that are also names of towns |
| 80 | + // get mixed up in a weird order, for example we want to AVOID: |
| 81 | + // 1. Limburg + deelgemeenten (= town in Wallonia) |
| 82 | + // 2. Provincie Limburg (= Flemish province) |
| 83 | + // 3. Limbourg (Limburg) (= earlier town in Wallonia) |
| 84 | + // 4. Bilstain (Limburg) (= submunicipality in earlier town in Wallonia) |
| 85 | + if ($compareString === 'provincie ' . $searchString) { |
81 | 86 | $matches[$region->key] = [ |
82 | | - 'score' => self::SCORE_EXACT_SUFFIX_PREFIX, |
| 87 | + 'score' => self::SCORE_PROVINCE, |
| 88 | + 'name' => $translatedRegion, |
| 89 | + ]; |
| 90 | + continue; |
| 91 | + } |
| 92 | + if ($compareString === 'regio ' . $searchString) { |
| 93 | + $matches[$region->key] = [ |
| 94 | + 'score' => self::SCORE_REGION, |
| 95 | + 'name' => $translatedRegion, |
| 96 | + ]; |
| 97 | + continue; |
| 98 | + } |
| 99 | + if ($compareString === $searchString . ' + deelgemeenten') { |
| 100 | + $matches[$region->key] = [ |
| 101 | + 'score' => self::SCORE_TOWN_WITH_SUBMUNICIPALITIES, |
83 | 102 | 'name' => $translatedRegion, |
84 | 103 | ]; |
85 | 104 | continue; |
|
0 commit comments