<?php /** * Example of stemming using NlpTools PorterStemmer * * @author Glenn De Backer <*****@*****.**> */ include 'vendor/autoload.php'; use NlpTools\Stemmers\PorterStemmer; // init PorterStemmer $stemmer = new PorterStemmer(); // stemming variants of upload printf("%s\n", $stemmer->stem("uploading")); printf("%s\n", $stemmer->stem("uploaded")); printf("%s\n", $stemmer->stem("uploads")); // stemming variants of delete printf("%s\n", $stemmer->stem("delete")); printf("%s\n", $stemmer->stem("deleted")); printf("%s\n", $stemmer->stem("deleting")); // stemming variants of computer printf("%s\n", $stemmer->stem("computer")); printf("%s\n", $stemmer->stem("computers"));
/* * words.txt and stems.txt are taken from * http://tartarus.org/~martin/PorterStemmer/ * * The other porter stemmer implementation is the one mentioned in * http://tartarus.org/~martin/PorterStemmer/ as a php implementation * */ include '../../../autoloader.php'; include 'other_php_porter_stemmer.php'; use NlpTools\Stemmers\PorterStemmer as OurPorterStemmer; $stemmer = new OurPorterStemmer(); $wordlist = file('words.txt', FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES); $start = microtime(true); foreach ($wordlist as $word) { $stemmer->stem($word); } $dur1 = microtime(true) - $start; $start = microtime(true); foreach ($wordlist as $word) { PorterStemmer::Stem($word); } $dur2 = microtime(true) - $start; echo $dur1, PHP_EOL; echo $dur2, PHP_EOL; $speedup = $dur1 / $dur2; if ($speedup > 1) { printf("NlpTools implementation is %.2f%% slower\n", 100 * (1 - 1 / $speedup)); } else { printf("NlpTools implementation is %.2f%% faster\n", 100 * (1 - $speedup)); }
<?php /* * words.txt and stems.txt are taken from * http://tartarus.org/~martin/PorterStemmer/ * */ include '../../../autoloader.php'; $words = fopen('words.txt', 'r'); $stems = fopen('stems.txt', 'r'); use NlpTools\Stemmers\PorterStemmer; $stemmer = new PorterStemmer(); while (!feof($words)) { $word = substr(fgets($words), 0, -1); $stem = substr(fgets($stems), 0, -1); $our_stem = $stemmer->stem($word); if ($our_stem !== $stem) { echo $word, ', stem: ', $stem, ' our_stem: ', $our_stem, PHP_EOL; } }