Skip to content

Commit 27c4c92

Browse files
committed
Always put provinces above results with same name like "x + deelgemeenten" to avoid a weird order
1 parent 0d01211 commit 27c4c92

File tree

3 files changed

+71
-10
lines changed

3 files changed

+71
-10
lines changed

src/Widget/RegionService.php

Lines changed: 28 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,10 @@
1010
*/
1111
class RegionService
1212
{
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"
1517
private const SCORE_TYPEAHEAD_COMPLETE = 4; // Typeahead match with a whitespace afterward, e.g. `Zele (Zele)` for search string `Zele`
1618
private const SCORE_TYPEAHEAD_PARTIAL = 3; // Typeahead match with a non-whitespace character afterward, e.g. `Zelem (Halen)` for search string `Zele`
1719
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')
7274
continue;
7375
}
7476

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) {
8186
$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,
83102
'name' => $translatedRegion,
84103
];
85104
continue;

test/Widget/RegionServiceTest.php

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ public function it_will_find_match_the_start_of_a_city_name()
9696
/**
9797
* @test
9898
*/
99-
public function it_will_find_match_provinces()
99+
public function it_will_find_provinces()
100100
{
101101
$this->assertSame(
102102
[
@@ -109,6 +109,24 @@ public function it_will_find_match_provinces()
109109
);
110110
}
111111

112+
/**
113+
* @test
114+
*/
115+
public function it_puts_provinces_above_municipalities_with_the_same_name()
116+
{
117+
$this->assertSame(
118+
[
119+
'nis-70000' => 'Provincie Limburg',
120+
'nis-63046' => 'Limburg + deelgemeenten',
121+
'nis-63046A' => 'Limbourg (Limburg)',
122+
'nis-63046B' => 'Bilstain (Limburg)',
123+
'nis-63046C' => 'Goé (Limburg)',
124+
'reg-limburgse-kempen' => 'Regio Limburgse Kempen',
125+
],
126+
$this->regionService->getAutocompletResults('Limburg')
127+
);
128+
}
129+
112130
/**
113131
* @test
114132
*/

test/Widget/regions_sample.json

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -246,5 +246,29 @@
246246
{
247247
"name": "Bressoux (Luik)",
248248
"key": "nis-62063E"
249+
},
250+
{
251+
"name": "Limburg + deelgemeenten",
252+
"key": "nis-63046"
253+
},
254+
{
255+
"name": "Limbourg (Limburg)",
256+
"key": "nis-63046A"
257+
},
258+
{
259+
"name": "Bilstain (Limburg)",
260+
"key": "nis-63046B"
261+
},
262+
{
263+
"name": "Go\u00e9 (Limburg)",
264+
"key": "nis-63046C"
265+
},
266+
{
267+
"name": "Provincie Limburg",
268+
"key": "nis-70000"
269+
},
270+
{
271+
"name": "Regio Limburgse Kempen",
272+
"key": "reg-limburgse-kempen"
249273
}
250274
]

0 commit comments

Comments
 (0)