-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathStemmer.php
169 lines (144 loc) · 3.86 KB
/
Stemmer.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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
<?php
namespace ArabyPHP\Stemmer;
class Stemmer
{
/**
* @var string
*/
private $_verbPre = 'وأسفلي';
/**
* @var string
*/
private $_verbPost = 'ومكانيه';
/**
* @var string
*/
private $_verbMay;
/**
* @var string
*/
private $_verbMaxPre = 4;
/**
* @var string
*/
private $_verbMaxPost = 6;
/**
* @var string
*/
private $_verbMinStem = 2;
/**
* @var string
*/
private $_nounPre = 'ابفكلوأ';
/**
* @var string
*/
private $_nounPost = 'اتةكمنهوي';
/**
* @var string
*/
private $_nounMay;
/**
* @var string
*/
private $_nounMaxPre = 4;
/**
* @var string
*/
private $_nounMaxPost = 6;
/**
* @var string
*/
private $_nounMinStem = 2;
/**
* Loads initialize values.
*
* @ignore
*/
public function __construct()
{
$this->_verbMay = $this->_verbPre.$this->_verbPost;
$this->_nounMay = $this->_nounPre.$this->_nounPost;
}
/**
* Get rough stem of the given Arabic word.
*
* @param string $word Arabic word you would like to get its stem
*
* @return string Arabic stem of the word
*
*/
public function stem($word)
{
$nounStem = $this->roughStem(
$word, $this->_nounMay, $this->_nounPre, $this->_nounPost,
$this->_nounMaxPre, $this->_nounMaxPost, $this->_nounMinStem
);
$verbStem = $this->roughStem(
$word, $this->_verbMay, $this->_verbPre, $this->_verbPost,
$this->_verbMaxPre, $this->_verbMaxPost, $this->_verbMinStem
);
if (mb_strlen($nounStem, 'UTF-8') < mb_strlen($verbStem, 'UTF-8')) {
$stem = $nounStem;
} else {
$stem = $verbStem;
}
return $stem;
}
/**
* Get rough stem of the given Arabic word (under specific rules).
*
* @param string $word Arabic word you would like to get its stem
* @param string $notChars Arabic chars those can't be in postfix or prefix
* @param string $preChars Arabic chars those may exists in the prefix
* @param string $postChars Arabic chars those may exists in the postfix
* @param int $maxPre Max prefix length
* @param int $maxPost Max postfix length
* @param int $minStem Min stem length
*
* @return string Arabic stem of the word under giving rules
*
*/
protected function roughStem(
$word, $notChars, $preChars, $postChars, $maxPre, $maxPost, $minStem
) {
$right = -1;
$left = -1;
$max = mb_strlen($word, 'UTF-8');
for ($i = 0; $i < $max; $i++) {
$needle = mb_substr($word, $i, 1, 'UTF-8');
if (mb_strpos($notChars, $needle, 0, 'UTF-8') === false) {
if ($right == -1) {
$right = $i;
}
$left = $i;
}
}
if ($right > $maxPre) {
$right = $maxPre;
}
if ($max - $left - 1 > $maxPost) {
$left = $max - $maxPost - 1;
}
for ($i = 0; $i < $right; $i++) {
$needle = mb_substr($word, $i, 1, 'UTF-8');
if (mb_strpos($preChars, $needle, 0, 'UTF-8') === false) {
$right = $i;
break;
}
}
for ($i = $max - 1; $i > $left; $i--) {
$needle = mb_substr($word, $i, 1, 'UTF-8');
if (mb_strpos($postChars, $needle, 0, 'UTF-8') === false) {
$left = $i;
break;
}
}
if ($left - $right >= $minStem) {
$stem = mb_substr($word, $right, $left - $right + 1, 'UTF-8');
} else {
$stem = null;
}
return $stem;
}
}