-
-
Notifications
You must be signed in to change notification settings - Fork 184
/
WordStemmer.php
57 lines (51 loc) · 1.25 KB
/
WordStemmer.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
<?php
namespace Rubix\ML\Tokenizers;
use Rubix\ML\Helpers\Params;
use Wamania\Snowball\StemmerFactory;
/**
* Word Stemmer
*
* Word Stemmer reduces inflected and derived words to their root form using the Snowball method. For example,
* the sentence "Majority voting is likely foolish" might stem to "Major vote is like foolish."
*
* @category Machine Learning
* @package Rubix/ML
* @author Andrew DalPino
*/
class WordStemmer extends Word
{
/**
* The word stemmer.
*
* @var \Wamania\Snowball\Stemmer\Stemmer
*/
protected $stemmer;
/**
* @param string $language
*/
public function __construct(string $language)
{
$this->stemmer = StemmerFactory::create($language);
}
/**
* Tokenize a block of text.
*
* @param string $string
* @return string[]
*/
public function tokenize(string $string) : array
{
return array_map([$this->stemmer, 'stem'], parent::tokenize($string));
}
/**
* Return the string representation of the object.
*
* @internal
*
* @return string
*/
public function __toString() : string
{
return 'Word Stemmer (language: ' . Params::shortName(get_class($this->stemmer)) . ')';
}
}