]> git.openstreetmap.org Git - nominatim.git/blob - test/php/Nominatim/SimpleWordListTest.php
fe543b3faaaaabc09d757e3de698f9f8445523a5
[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
92     public function testMaxWordSets()
93     {
94         $aWords = array_fill(0, 4, 'a');
95         $oList = new SimpleWordList(join(' ', $aWords));
96         $this->assertEquals(8, count($oList->getWordSets(new TokensFullSet())));
97
98         $aWords = array_fill(0, 18, 'a');
99         $oList = new SimpleWordList(join(' ', $aWords));
100         $this->assertEquals(100, count($oList->getWordSets(new TokensFullSet())));
101     }
102
103
104     public function testPartialTokensShortTerm()
105     {
106         $oList = new SimpleWordList('a b c d');
107         $this->assertEquals(
108             '(a|b c d),(a|b c|d)',
109             $this->serializeSets($oList->getWordSets(new TokensPartialSet(array('a', 'b', 'd', 'b c', 'b c d'))))
110         );
111     }
112
113
114     public function testPartialTokensLongTerm()
115     {
116         $aWords = array_fill(0, 18, 'a');
117         $oList = new SimpleWordList(join(' ', $aWords));
118         $this->assertEquals(80, count($oList->getWordSets(new TokensPartialSet(array('a', 'a a a a a')))));
119     }
120 }