]> git.openstreetmap.org Git - nominatim.git/blob - test/php/Nominatim/SimpleWordListTest.php
run final index creation in parallel
[nominatim.git] / test / php / Nominatim / SimpleWordListTest.php
1 <?php
2 /**
3  * SPDX-License-Identifier: GPL-2.0-only
4  *
5  * This file is part of Nominatim. (https://nominatim.org)
6  *
7  * Copyright (C) 2022 by the Nominatim developer community.
8  * For a full list of authors see the git log.
9  */
10
11 namespace Nominatim;
12
13 require_once(CONST_LibDir.'/SimpleWordList.php');
14
15 class TokensFullSet
16 {
17     public function containsAny($sTerm)
18     {
19         return true;
20     }
21 }
22
23 // phpcs:ignore PSR1.Classes.ClassDeclaration.MultipleClasses
24 class TokensPartialSet
25 {
26     public function __construct($aTokens)
27     {
28         $this->aTokens = array_flip($aTokens);
29     }
30
31     public function containsAny($sTerm)
32     {
33         return isset($this->aTokens[$sTerm]);
34     }
35 }
36
37 // phpcs:ignore PSR1.Classes.ClassDeclaration.MultipleClasses
38 class SimpleWordListTest extends \PHPUnit\Framework\TestCase
39 {
40
41
42     private function serializeSets($aSets)
43     {
44         $aParts = array();
45         foreach ($aSets as $aSet) {
46             $aParts[] = '(' . join('|', $aSet) . ')';
47         }
48         return join(',', $aParts);
49     }
50
51
52     public function testEmptyPhrase()
53     {
54         $oList = new SimpleWordList('');
55         $this->assertNull($oList->getWordSets(new TokensFullSet()));
56     }
57
58
59     public function testSingleWordPhrase()
60     {
61         $oList = new SimpleWordList('a');
62
63         $this->assertEquals(
64             '(a)',
65             $this->serializeSets($oList->getWordSets(new TokensFullSet()))
66         );
67     }
68
69
70     public function testMultiWordPhrase()
71     {
72         $oList = new SimpleWordList('a b');
73         $this->assertEquals(
74             '(a b),(a|b)',
75             $this->serializeSets($oList->getWordSets(new TokensFullSet()))
76         );
77
78         $oList = new SimpleWordList('a b c');
79         $this->assertEquals(
80             '(a b c),(a b|c),(a|b c),(a|b|c)',
81             $this->serializeSets($oList->getWordSets(new TokensFullSet()))
82         );
83
84         $oList = new SimpleWordList('a b c d');
85         $this->assertEquals(
86             '(a b c d),(a b c|d),(a b|c d),(a|b c d),(a b|c|d),(a|b c|d),(a|b|c d),(a|b|c|d)',
87             $this->serializeSets($oList->getWordSets(new TokensFullSet()))
88         );
89     }
90
91     public function testCmpByArraylen()
92     {
93         // Array elements are phrases, we want to sort so longest phrases are first
94         $aList1 = array('hackney', 'bridge', 'london', 'england');
95         $aList2 = array('hackney', 'london', 'bridge');
96         $aList3 = array('bridge', 'hackney', 'london', 'england');
97
98         $this->assertEquals(0, \Nominatim\SimpleWordList::cmpByArraylen($aList1, $aList1));
99
100         // list2 "wins". Less array elements
101         $this->assertEquals(1, \Nominatim\SimpleWordList::cmpByArraylen($aList1, $aList2));
102         $this->assertEquals(-1, \Nominatim\SimpleWordList::cmpByArraylen($aList2, $aList3));
103
104         // list1 "wins". Same number of array elements but longer first element
105         $this->assertEquals(-1, \Nominatim\SimpleWordList::cmpByArraylen($aList1, $aList3));
106     }
107
108     public function testMaxWordSets()
109     {
110         $aWords = array_fill(0, 4, 'a');
111         $oList = new SimpleWordList(join(' ', $aWords));
112         $this->assertEquals(8, count($oList->getWordSets(new TokensFullSet())));
113
114         $aWords = array_fill(0, 18, 'a');
115         $oList = new SimpleWordList(join(' ', $aWords));
116         $this->assertEquals(100, count($oList->getWordSets(new TokensFullSet())));
117     }
118
119
120     public function testPartialTokensShortTerm()
121     {
122         $oList = new SimpleWordList('a b c d');
123         $this->assertEquals(
124             '(a|b c d),(a|b c|d)',
125             $this->serializeSets($oList->getWordSets(new TokensPartialSet(array('a', 'b', 'd', 'b c', 'b c d'))))
126         );
127     }
128
129
130     public function testPartialTokensLongTerm()
131     {
132         $aWords = array_fill(0, 18, 'a');
133         $oList = new SimpleWordList(join(' ', $aWords));
134         $this->assertEquals(80, count($oList->getWordSets(new TokensPartialSet(array('a', 'a a a a a')))));
135     }
136 }