-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathUStemming.pas
399 lines (341 loc) · 10.5 KB
/
UStemming.pas
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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
unit UStemming;
Interface
Uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls,
UStringContainers;
Type
TStemming = Class(TObject)
Private
{ Private declarations }
Lexicon: TStringContainer;
sRules, sMokassar: TStringList;
Public
DataPath: String;
StemList, PatternList: TStringList;
{ Public declarations }
constructor Create;
destructor Destroy; override;
Function Run(Input: string): string;
function Normalization(Const s: string): string;
function NormalizeValidation(const sWord: string): Boolean;
procedure ReLoadPatterns;
procedure AddMatchedRule(Msg: String);
procedure AddStem(Msg: String);
procedure AddToLog(sStem, sRule: string);
function Validation(const sWord: string): Boolean;
function VerbValidation(const sWord: string; var sSuffix: string): Boolean;
function GetMokassarStem(const sWord: string; var Stem: String): Boolean;
function isMatch(const sInput, sRule: string): Boolean;
function ExtractStem(sInput, sRule: string; const sReplacement: string = '${stem}'): String;
procedure LoadDictionary(var List: TStringList; const sFileName: TFileName; NameValueSep: Char;
Sorting: Boolean = True);
End;
Implementation
Uses
System.RegularExpressions, System.IOUtils, System.Types, System.Math, UTools, UPublicConstant,
JclStrings, UIniOptions;
function ByHighLength(List: TStringList; Index1, Index2: Integer): Integer;
var
L1, L2: Integer;
begin
L1 := Length(List[Index1]);
L2 := Length(List[Index2]);
Result := CompareValue(L2, L1);
if Result = EqualsValue then
Result := CompareStr(List[Index2], List[Index1]);
end;
function ByLowLength(List: TStringList; Index1, Index2: Integer): Integer;
var
L1, L2: Integer;
begin
L1 := Length(List[Index1]);
L2 := Length(List[Index2]);
Result := CompareValue(L1, L2);
if Result = EqualsValue then
Result := CompareStr(List[Index1], List[Index2]);
end;
procedure TStemming.LoadDictionary(var List: TStringList; const sFileName: TFileName;
NameValueSep: Char; Sorting: Boolean);
begin
with List do
begin
LoadFromFile(sFileName, TEncoding.UTF8);
NameValueSeparator := NameValueSep;
Sorted := Sorting
end;
{ if Sorting then
QuickSort(List, 0, List.Count - 1); }
end;
constructor TStemming.Create;
Begin
DataPath := BasePath + IniOptions.MainDataDir;
StemList := TStringList.Create;
PatternList := TStringList.Create;
sRules := TStringList.Create;
sRules.LoadFromFile(DataPath + IniOptions.MainPatternFile);
{ sDict := TStringList.Create;
LoadDictionary(sDict, DataPath + IniOptions.MainDictionaryFile, NativeTab); }
Lexicon := TStringContainer.New(DataPath + IniOptions.MainDictionaryFile, TEncoding.UTF8);
sMokassar := TStringList.Create;
LoadDictionary(sMokassar, DataPath + IniOptions.MainMokassarDictionaryFile, NativeTab);
End;
destructor TStemming.Destroy;
Begin
sRules.Free;
// sDict.Free;
Lexicon.Free;
sMokassar.Free;
StemList.Free;
PatternList.Free;
End;
procedure TStemming.AddMatchedRule(Msg: String);
begin
PatternList.Add(Msg);
end;
procedure TStemming.AddStem(Msg: String);
begin
if (not ParamIsEmpty(Msg)) and (Find(StemList, Msg) = -1) then
StemList.Add(Msg);
end;
procedure TStemming.AddToLog(sStem, sRule: string);
begin
AddStem(sStem);
AddMatchedRule(sRule);
end;
function TStemming.VerbValidation(const sWord: string; var sSuffix: string): Boolean;
const
sVerbSuffix: array [0 .. 8] of string = ('*ش', '*نده', '*ا', '*ار', 'وا*', 'اثر*', 'فرو*',
'پیش*', 'گرو*');
var
J: Integer;
sTemp: string;
begin
// اصلاح اعتبارسنجی فعل
// افزودن پسوندهایی که مصدر فعل را میسازد مثل
// بریدن : یدن
// گفتن : ن
// و ...
// خواندن فایل ورد گرامر فارسی برای یافتن این پسوندهای مصدر ساز
// مصدر جعلی : یدن
// دن
// ادن
for J := 0 to High(sVerbSuffix) do
begin
if (J = 0) and (sWord[Length(sWord)] in ['ا', 'و']) then
sTemp := StringReplace(sVerbSuffix[J], '*', sWord + 'ی', [])
else
sTemp := StringReplace(sVerbSuffix[J], '*', sWord, []);
Result := NormalizeValidation(sTemp);
if Result then
begin
sSuffix := sVerbSuffix[J];
Break;
end;
end;
end;
Function TStemming.Run(Input: string): string;
var
I, J, K, iStemLength: Integer;
s, Pattern, sTag: string;
sArr, sReplace: TStringDynArray;
Begin
StemList.Clear;
PatternList.Clear;
Input := Normalization(Input);
If ParamIsEmpty(Input) Then
Exit('');
if GetMokassarStem(Input, s) then
begin
AddToLog(s, '[جمع مکسر]');
Exit(s);
end
else if NormalizeValidation(Input) then
begin
AddToLog(Input, '[فرهنگ لغت]');
Exit(Input);
end;
For I := 0 To sRules.Count - 1 Do
Begin
sArr := Explode(sRules[I], ',');
Pattern := sArr[0];
sReplace := Explode(sArr[1], ';');
sTag := sArr[2];
iStemLength := StrToIntDef(sArr[3], 2);
if isMatch(Input, Pattern) then
begin
K := 0;
for J := 0 to High(sReplace) do
begin
if K > 0 Then
Break;
s := Trim(ExtractStem(Input, Pattern, sReplace[J]));
if Length(s) < iStemLength then
Continue;
if sTag = 'K' then // Kasre Ezafe
begin
if StemList.Count = 0 then
begin
if GetMokassarStem(s, sTag) then
begin
AddToLog(sTag, Pattern + ' [جمع مکسر]');
Inc(K);
end
else if NormalizeValidation(s) then
begin
AddToLog(s, Pattern);
Inc(K);
end
else
AddToLog('', Pattern + ' : {' + s + '}');
end;
end
else if sTag = 'V' then
begin
if VerbValidation(s, sTag) then
begin
AddToLog(s, Pattern + ' : [' + sTag + ']');
Inc(K);
end
else
AddToLog('', Pattern + ' : {تمام وندها}');
end
else
begin
if NormalizeValidation(s) then
begin
AddToLog(s, Pattern);
Inc(K);
end
else
AddToLog('', Pattern + ' : {' + s + '}');
end;
end;
end
End;
if StemList.Count = 0 then
AddToLog(Input, '');
if IniOptions.MainPatternCount <> 0 then
begin
if IniOptions.MainPatternCount < 0 then
StemList.CustomSort(ByHighLength)
Else
StemList.CustomSort(ByLowLength);
I := 0;
while (I < StemList.Count) and (StemList.Count > Abs(IniOptions.MainPatternCount)) do
begin
StemList.Delete(I);
PatternList.Delete(I);
end;
end;
Result := StemList.CommaText;
end;
procedure TStemming.ReLoadPatterns;
begin
// LoadDictionary(sDict, DataPath + IniOptions.MainDictionaryFile, NativeTab);
Lexicon.LoadFromFile(DataPath + IniOptions.MainDictionaryFile, TEncoding.UTF8);
LoadDictionary(sMokassar, DataPath + IniOptions.MainMokassarDictionaryFile, NativeTab);
sRules.LoadFromFile(DataPath + IniOptions.MainPatternFile);
end;
function TStemming.ExtractStem(sInput, sRule: string;
const sReplacement: string = '${stem}'): String;
var
Match: TMatch;
I: Integer;
M: TMatchCollection;
sList: TStringList;
begin
(* Result := '';
sList := TStringList.Create;
try
M := TRegEx.Matches(sInput, sRule);
I := 0;
for I := 0 to M.Count - 1 do
begin
Match := M.Item[I];
while Match.Success do
begin
sList.Add(Match.Groups.Item['stem'].Value);
Match := Match.NextMatch;
end;
end;
{ Match := TRegEx.Match(sInput, sRule);
while Match.Success do
begin
sList.Add(Match.Groups.Item['stem'].Value);
Match := Match.NextMatch;
end; }
sList.Sort;
if sList.Count > 0 then
Result := sList[sList.Count - 1];
Result := StringReplace(sReplacement, '${stem}', Result, [rfReplaceAll]);
finally
sList.Free;
end; *)
Result := TRegEx.Replace(sInput, sRule, sReplacement);
end;
function TStemming.isMatch(const sInput, sRule: string): Boolean;
begin
Result := TRegEx.isMatch(sInput, sRule);
end;
function TStemming.Normalization(const s: string): string;
begin
Result := PersianStr(s);
Result := Trim(StringReplace(Result, 'ۀ', 'ه', [rfReplaceAll]));
Result := Trim(StringReplace(Result, ZWNJ, NativeSpace, [rfReplaceAll]));
end;
function TStemming.GetMokassarStem(const sWord: string; var Stem: String): Boolean;
begin
Stem := sMokassar.Values[sWord];
Result := not ParamIsEmpty(Stem);
end;
function TStemming.Validation(const sWord: string): Boolean;
var
Idx: Integer;
begin
// Result := sDict.Find(sWord, Idx);
Result := Lexicon.Validation(sWord);
end;
function TStemming.NormalizeValidation(const sWord: string): Boolean;
{ const
sSuffix: array [0 .. 18] of string = ('كار', 'ناك', 'وار', 'آسا', 'آگین', 'بار', 'بان', 'دان',
'زار', 'سار', 'سان', 'لاخ', 'مند', 'دار', 'مرد', 'کننده', 'گرا', 'نما', 'متر');
sPrefix: array [0 .. 7] of string = ('بی', 'با', 'پیش', 'غیر', 'فرو', 'هم', 'نا', 'یک');
var
s: string;
L, I: Integer; }
begin
Result := Lexicon.NormalizeValidation(sWord);
{ L := Length(Trim(sWord)) - 1;
Result := Self.Validation(sWord);
if (not Result) and (Pos('ا', sWord) = 1) then
Result := Self.Validation(StringReplace(sWord, 'ا', 'آ', []));
if (not Result) and (Pos('ا', sWord) in [2 .. L]) then
Result := Self.Validation(StringReplace(sWord, 'ا', 'أ', []));
if (not Result) and (Pos('ا', sWord) in [2 .. L]) then
Result := Self.Validation(StringReplace(sWord, 'ا', 'إ', []));
if (not Result) and (Pos('ئو', sWord) in [2 .. L]) then
Result := Self.Validation(StringReplace(sWord, 'ئو', 'ؤ', []));
if (not Result) and (Pos(' ', sWord) in [2 .. L]) then
Result := Self.Validation(StringReplace(sWord, ' ', '', []));
// دیندار
// دین دار
if (not Result) then
begin
L := StrSuffixIndex(sWord, sSuffix);
if L > -1 then
begin
if sSuffix[L] = 'مند' then
Result := Self.Validation(StringReplace(sWord, sSuffix[L], 'ه ' + sSuffix[L], []))
else
Result := Self.Validation(StringReplace(sWord, sSuffix[L], ' ' + sSuffix[L], []));
end;
end;
if (not Result) then
begin
L := StrPrefixIndex(sWord, sPrefix);
if L > -1 then
Result := Self.Validation(StringReplace(sWord, sPrefix[L], sPrefix[L] + ' ', []));
end;
}
end;
End.