forked from LeonieWeissweiler/CISTEM
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Cistem.cs
169 lines (139 loc) · 4 KB
/
Cistem.cs
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
using System;
using System.Text.RegularExpressions;
namespace Cistem
{
public static class Cistem
{
private const string GE_PATTERN = "^ge(.{4,})";
private const string DOLLAR1_PATTERN = "(.)\\1";
private const string ND_PATTERN = "nd$";
private const string EMR_PATTERN = "e[mr]$";
private const string T_PATTERN = "t$";
private const string ESN_PATTERN = "[esn]$";
private const string STAR_PATTERN = "(.)\\*";
public static String Stem(String word)
{
return Stem(word, false);
}
public static String Stem(String word, bool case_insensitive)
{
if (word.Length == 0) return word;
word = word.Replace("Ü", "U");
word = word.Replace("Ö", "O");
word = word.Replace("Ä", "A");
word = word.Replace("ü", "u");
word = word.Replace("ö", "o");
word = word.Replace("ä", "a");
var uppercase = char.IsUpper(word[0]);
word = word.ToLower();
word = word.Replace("ß", "ss");
word = Regex.Replace(word, GE_PATTERN, "$1");
word = word.Replace("sch", "$");
word = word.Replace("ei", "%");
word = word.Replace("ie", "&");
word = Regex.Replace(word, DOLLAR1_PATTERN, "$1*");
string newWord;
while (word.Length > 3)
{
if (word.Length > 5)
{
newWord = Regex.Replace(word, EMR_PATTERN, "");
if (word != newWord)
{
word = newWord;
continue;
}
newWord = Regex.Replace(word, ND_PATTERN, "");
if (word != newWord)
{
word = newWord;
continue;
}
}
if (!uppercase || case_insensitive)
{
newWord = Regex.Replace(word, T_PATTERN, "");
if (word != newWord)
{
word = newWord;
continue;
}
}
newWord = Regex.Replace(word, ESN_PATTERN, "");
if (word != newWord)
word = newWord;
else
break;
}
word = Regex.Replace(word, STAR_PATTERN, "$1$1");
word = word.Replace("&", "ie");
word = word.Replace("%", "ei");
word = word.Replace("$", "sch");
return word;
}
public static String[] Segment(String word)
{
return Segment(word, false);
}
public static String[] Segment(String word, bool case_insensitive)
{
if (word.Length == 0)
return new string[] { string.Empty, string.Empty };
var restLength = 0;
var uppercase = char.IsUpper(word[0]);
word = word.ToLower();
var original = word;
word = word.Replace("sch", "$");
word = word.Replace("ei", "%");
word = word.Replace("ie", "&");
word = Regex.Replace(word, DOLLAR1_PATTERN, "$1*");
string newWord;
while (word.Length > 3)
{
if (word.Length > 5)
{
newWord = Regex.Replace(word, EMR_PATTERN, "");
if (word != newWord)
{
restLength += 2;
word = newWord;
continue;
}
newWord = Regex.Replace(word, ND_PATTERN, "");
if (word != newWord)
{
restLength += 2;
word = newWord;
continue;
}
}
if (!uppercase || case_insensitive)
{
newWord = Regex.Replace(word, T_PATTERN, "");
if (word != newWord)
{
restLength += 1;
word = newWord;
continue;
}
}
newWord = Regex.Replace(word, ESN_PATTERN, "");
if (word != newWord)
{
restLength += 1;
word = newWord;
}
else
break;
}
word = Regex.Replace(word, STAR_PATTERN, "$1$1");
word = word.Replace("&", "ie");
word = word.Replace("%", "ei");
word = word.Replace("$", "sch");
var rest = string.Empty;
if (restLength != 0)
rest = original.Substring(original.Length - restLength);
return new string[] { word, rest };
}
}
}