Added the Porter Stemmer module to improve searches. This doesn't deal with some...
[yaffs-website] / web / modules / contrib / porterstemmer / tests / src / Unit / TestItemsTrait.php
1 <?php
2
3 namespace Drupal\Tests\porterstemmer\Unit;
4
5 /**
6  * Provides a common method for testing the stemmer.
7  */
8 trait TestItemsTrait {
9
10   /**
11    * Load an associative array of known input/output pairs.
12    *
13    *  This list comes from
14    *   http://snowball.tartarus.org/algorithms/english/stemmer.html
15    *   The array count is determined by parameters, below.
16    *
17    * @param int $skipto
18    *    Line of file to start on (count starts at 0), not counting short ones.
19    * @param int $runto
20    *    Number of lines to test, not counting short ones.
21    *
22    * @return str[]
23    *    An associative array of word=stem pairs where element [0] is the word
24    *    and element [1] is the expected stem.
25    */
26   public function retrieveStemWords($skipto = 0, $runto = 5000) {
27     $file = __DIR__ . '/testwords.txt';
28     $handle = @fopen($file, "r");
29     $tests = [];
30     $skipped = 0;
31     $ran = 0;
32
33     while (!feof($handle) && $ran < $runto) {
34       // Read a line of the file, and split into words.
35       $line = trim(fgets($handle, 4096));
36       $words = preg_split("/=/", $line, -1, PREG_SPLIT_NO_EMPTY);
37       if (count($words) < 2) {
38         continue;
39       }
40       $skipped++;
41       if ($skipped < $skipto) {
42         continue;
43       }
44       $tests[] = $words;
45       $ran++;
46     }
47
48     fclose($handle);
49
50     return $tests;
51   }
52
53 }