<?php
/**
* SeekQuarry/Yioop --
* Open Source Pure PHP Search Engine, Crawler, and Indexer
*
* Copyright (C) 2009 - 2026 Chris Pollett chris@pollett.org
*
* LICENSE:
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*
* @author Chris Pollett chris@pollett.org
* @license https://www.gnu.org/licenses/ GPL3
* @link https://www.seekquarry.com/
* @copyright 2009 - 2026
* @filesource
*/
namespace seekquarry\yioop\tests;
use seekquarry\yioop\library\JavascriptUnitTest;
/**
* SuggestJavascriptTest tests the spelling correction of suggest.js, which
* offers a corrected query under the search box on a page of results.
*
* @author Chris Pollett
*/
class SuggestJavascriptTest extends JavascriptUnitTest
{
/**
* correctSpellingKeepsNumbersAndShortWordsTestCase checks
* correctSpelling against a small dictionary holding the words p,
* cities, legacy and pandemic. The number 6 and the two letter word
* at must be kept as typed, though p is one edit from 6 and a is in
* the dictionary's letters. The misspelled word pandemc must still be
* corrected to pandemic, and a word that no candidate beats must be
* left alone.
*
* @return string html and Javascript that checks the corrections in a
* browser or, from the command line, under node
*/
public function correctSpellingKeepsNumbersAndShortWordsTestCase()
{
ob_start();
?>
<div id="suggestTest">
</div>
<script src="../scripts/basic.js" ></script>
<script src="../scripts/suggest.js" ></script>
<script src="../scripts/javascript_unit_test.js" ></script>
<script>
END_OF_TERM_MARKER = " ";
alpha = "abcdefghijklmnopqrstuvwxyz0123456789";
dictionary = {};
let frequencies = {"p": 900, "a": 900, "cities": 300,
"legacy": 200, "pandemic": 100};
for (let word in frequencies) {
let node = dictionary;
for (let i = 0; i < word.length; i++) {
let key = encode(word.charAt(i));
if (!node[key]) {
node[key] = {};
}
node = node[key];
}
node[END_OF_TERM_MARKER] = frequencies[word];
}
let results = [
{ name: "6 is kept", pass: correctSpelling("6") === "6" },
{ name: "a two letter word is kept",
pass: correctSpelling("at") === "at" },
{ name: "pandemc becomes pandemic",
pass: correctSpelling("pandemc") === "pandemic" },
{ name: "a word in the dictionary is kept",
pass: correctSpelling("cities") === "cities" }];
reportUnitTestResults("suggestTest",
"correctSpellingKeepsNumbersAndShortWordsTestCase", results);
</script>
<?php
$out_data = ob_get_contents();
ob_end_clean();
return $out_data;
}
}