5 require_once(CONST_BasePath.'/lib/Phrase.php');
9 public function containsAny($sTerm)
15 // phpcs:ignore PSR1.Classes.ClassDeclaration.MultipleClasses
16 class TokensPartialSet
18 public function __construct($aTokens)
20 $this->aTokens = array_flip($aTokens);
23 public function containsAny($sTerm)
25 return isset($this->aTokens[$sTerm]);
29 // phpcs:ignore PSR1.Classes.ClassDeclaration.MultipleClasses
30 class PhraseTest extends \PHPUnit\Framework\TestCase
34 private function serializeSets($aSets)
37 foreach ($aSets as $aSet) {
38 $aParts[] = '(' . join('|', $aSet) . ')';
40 return join(',', $aParts);
44 public function testEmptyPhrase()
46 $oPhrase = new Phrase('', '');
47 $oPhrase->computeWordSets(new TokensFullSet());
51 $oPhrase->getWordSets()
56 public function testSingleWordPhrase()
58 $oPhrase = new Phrase('a', '');
59 $oPhrase->computeWordSets(new TokensFullSet());
63 $this->serializeSets($oPhrase->getWordSets())
68 public function testMultiWordPhrase()
70 $oPhrase = new Phrase('a b', '');
71 $oPhrase->computeWordSets(new TokensFullSet());
74 $this->serializeSets($oPhrase->getWordSets())
77 $oPhrase = new Phrase('a b c', '');
78 $oPhrase->computeWordSets(new TokensFullSet());
80 '(a b c),(a|b c),(a b|c),(a|b|c)',
81 $this->serializeSets($oPhrase->getWordSets())
84 $oPhrase = new Phrase('a b c d', '');
85 $oPhrase->computeWordSets(new TokensFullSet());
87 '(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)',
88 $this->serializeSets($oPhrase->getWordSets())
93 public function testInverseWordSets()
95 $oPhrase = new Phrase('a b c', '');
96 $oPhrase->computeWordSets(new TokensFullSet());
97 $oPhrase->invertWordSets();
100 '(a b c),(b c|a),(c|a b),(c|b|a)',
101 $this->serializeSets($oPhrase->getWordSets())
106 public function testMaxWordSets()
108 $oPhrase = new Phrase(join(' ', array_fill(0, 4, 'a')), '');
109 $oPhrase->computeWordSets(new TokensFullSet());
110 $this->assertEquals(8, count($oPhrase->getWordSets()));
111 $oPhrase->invertWordSets();
112 $this->assertEquals(8, count($oPhrase->getWordSets()));
114 $oPhrase = new Phrase(join(' ', array_fill(0, 18, 'a')), '');
115 $oPhrase->computeWordSets(new TokensFullSet());
116 $this->assertEquals(100, count($oPhrase->getWordSets()));
117 $oPhrase->invertWordSets();
118 $this->assertEquals(100, count($oPhrase->getWordSets()));
122 public function testPartialTokensShortTerm()
124 $oPhrase = new Phrase('a b c d', '');
125 $oPhrase->computeWordSets(new TokensPartialSet(array('a', 'b', 'd', 'b c', 'b c d')));
127 '(a|b c d),(a|b c|d)',
128 $this->serializeSets($oPhrase->getWordSets())
133 public function testPartialTokensLongTerm()
135 $oPhrase = new Phrase(join(' ', array_fill(0, 18, 'a')), '');
136 $oPhrase->computeWordSets(new TokensPartialSet(array('a', 'a a a a a')));
137 $this->assertEquals(80, count($oPhrase->getWordSets()));