German.php
6.58 KB
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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
<?php
namespace SearchWP\Dependencies\Wamania\Snowball;
/**
*
* @link http://snowball.tartarus.org/algorithms/german/stemmer.html
* @author wamania
*
*/
class German extends Stem
{
/**
* All German vowels
*/
protected static $vowels = array('a', 'e', 'i', 'o', 'u', 'y', 'ä', 'ö', 'ü');
protected static $sEndings = array('b', 'd', 'f', 'g', 'h', 'k', 'l', 'm', 'n', 'r', 't');
protected static $stEndings = array('b', 'd', 'f', 'g', 'h', 'k', 'l', 'm', 'n', 't');
/**
* {@inheritdoc}
*/
public function stem($word)
{
// we do ALL in UTF-8
if (!Utf8::check($word)) {
throw new \Exception('Word must be in UTF-8');
}
$this->plainVowels = \implode('', self::$vowels);
$this->word = Utf8::strtolower($word);
// First, replace ß by ss
$this->word = Utf8::str_replace('ß', 'ss', $this->word);
// put u and y between vowels into upper case
$this->word = \preg_replace('#([' . $this->plainVowels . '])y([' . $this->plainVowels . '])#u', '$1Y$2', $this->word);
$this->word = \preg_replace('#([' . $this->plainVowels . '])u([' . $this->plainVowels . '])#u', '$1U$2', $this->word);
// R1 and R2 are first set up in the standard way
$this->r1();
$this->r2();
// but then R1 is adjusted so that the region before it contains at least 3 letters.
if ($this->r1Index < 3) {
$this->r1Index = 3;
$this->r1 = Utf8::substr($this->word, 3);
}
$this->step1();
$this->step2();
$this->step3();
$this->finish();
return $this->word;
}
/**
* Step 1
*/
public function step1()
{
// delete if in R1
if (($position = $this->search(array('em', 'ern', 'er'))) !== \false) {
if ($this->inR1($position)) {
$this->word = Utf8::substr($this->word, 0, $position);
}
return \true;
}
// delete if in R1
if (($position = $this->search(array('es', 'en', 'e'))) !== \false) {
if ($this->inR1($position)) {
$this->word = Utf8::substr($this->word, 0, $position);
//If an ending of group (b) is deleted, and the ending is preceded by niss, delete the final s
if ($this->search(array('niss')) !== \false) {
$this->word = Utf8::substr($this->word, 0, -1);
}
}
return \true;
}
// s (preceded by a valid s-ending)
if (($position = $this->search(array('s'))) !== \false) {
if ($this->inR1($position)) {
$before = $position - 1;
$letter = Utf8::substr($this->word, $before, 1);
if (\in_array($letter, self::$sEndings)) {
$this->word = Utf8::substr($this->word, 0, $position);
}
}
return \true;
}
return \false;
}
/**
* Step 2
*/
public function step2()
{
// en er est
// delete if in R1
if (($position = $this->search(array('en', 'er', 'est'))) !== \false) {
if ($this->inR1($position)) {
$this->word = Utf8::substr($this->word, 0, $position);
}
return \true;
}
// st (preceded by a valid st-ending, itself preceded by at least 3 letters)
// delete if in R1
if (($position = $this->search(array('st'))) !== \false) {
if ($this->inR1($position)) {
$before = $position - 1;
if ($before >= 3) {
$letter = Utf8::substr($this->word, $before, 1);
if (\in_array($letter, self::$stEndings)) {
$this->word = Utf8::substr($this->word, 0, $position);
}
}
}
return \true;
}
return \false;
}
/**
* Step 3: d-suffixes
*/
public function step3()
{
// end ung
// delete if in R2
// if preceded by ig, delete if in R2 and not preceded by e
if (($position = $this->search(array('end', 'ung'))) !== \false) {
if ($this->inR2($position)) {
$this->word = Utf8::substr($this->word, 0, $position);
}
if (($position2 = $this->search(array('ig'))) !== \false) {
$before = $position2 - 1;
$letter = Utf8::substr($this->word, $before, 1);
if ($this->inR2($position2) && $letter != 'e') {
$this->word = Utf8::substr($this->word, 0, $position2);
}
}
return \true;
}
// ig ik isch
// delete if in R2 and not preceded by e
if (($position = $this->search(array('ig', 'ik', 'isch'))) !== \false) {
$before = $position - 1;
$letter = Utf8::substr($this->word, $before, 1);
if ($this->inR2($position) && $letter != 'e') {
$this->word = Utf8::substr($this->word, 0, $position);
}
return \true;
}
// lich heit
// delete if in R2
// if preceded by er or en, delete if in R1
if (($position = $this->search(array('lich', 'heit'))) != \false) {
if ($this->inR2($position)) {
$this->word = Utf8::substr($this->word, 0, $position);
}
if (($position2 = $this->search(array('er', 'en'))) !== \false) {
if ($this->inR1($position2)) {
$this->word = Utf8::substr($this->word, 0, $position2);
}
}
return \true;
}
// keit
// delete if in R2
// if preceded by lich or ig, delete if in R2
if (($position = $this->search(array('keit'))) != \false) {
if ($this->inR2($position)) {
$this->word = Utf8::substr($this->word, 0, $position);
}
if (($position2 = $this->search(array('lich', 'ig'))) !== \false) {
if ($this->inR2($position2)) {
$this->word = Utf8::substr($this->word, 0, $position2);
}
}
return \true;
}
return \false;
}
/**
* Finally
*/
public function finish()
{
// turn U and Y back into lower case, and remove the umlaut accent from a, o and u.
$this->word = Utf8::str_replace(array('U', 'Y', 'ä', 'ü', 'ö'), array('u', 'y', 'a', 'u', 'o'), $this->word);
}
}