-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathregexer.cpp
389 lines (335 loc) · 10.5 KB
/
regexer.cpp
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
#include "stdafx.h"
#include "regexer.h"
using namespace regex;
using namespace std;
typedef vector<string> strvec;
// Ïðàâèëà ïî óìîë÷àíèþ
// ëó÷øå ñäåëàòü ñèìâîëüíûé ìàññèâ ñ ñîäåðæèìûì rules.ini
// è ïàðñèòü åãî êàê ôàéë
void REParser::defrules()
{
string dfltrulesR[][3]=
{ {"PF","éêè","åê"},
{"PF","(6)êè","$1åê"},
{"PF","6)êè","$1îê"},
{"PF","êè","ê"},
{"P","êè","êîâ"},
{"PF","(6)ãè","ã"},
{"P","ãè","ãîâ"},
{"PF","êè","ê"},
{"PF","(ó6)è","$1"},
{"F","êà","êå"},
{"PF","(.)û ","$1"} };
string dfltrulesD[][3]=
{{"","",""}};
string dfltrulesV[][3]=
{ {"PFA","éêè","åê"},
{"PFA","(4)êè","$1åê"},
{"PFA","(0)êè","$1îê"},
{"PFA","êè","ê"},
{"PA","êè","êîâ"},
{"PFA","(6)ãè","$1îã"},
{"PFA","ãè","ã"},
{"PFA","(0)êè","$1îê"},
{"PA","ãè","ãîâ"} };
string dfltrulesT[][3]=
{{"","(4)à","$1åé"}};
string dfltrulesT2[][3]=
{ {"S","(4)à","$1åþ"},
{"S","à","îþ"},
{"SF","(.)","$1þ"},
{"S","àÿ","îþ"}
};
string dfltrulesP[][3]=
{{"","",""}};
string dfltrulesC[][8]=
{
{ "PU", "8è", "_åâ", "_ÿì", "_è", "_åâ", "_ÿìè", "_ÿõ" },
{ "SBE", "34", "df", "SBE", "34", "df", "SBE" },
{ "a", "56", "jh", "SBE", "34", "df", "SBE"},
{ "nU", "5v", "45", "SBE", "34", "df", "SBE"}
};
string dfltrulesVerb[][3]=
{ { "Abs","123","321" } };
struct formular_t
{
std::string (*arr)[3];
size_t size;
};
vector< vector<COneCaseRule> > cases(6);
cases.push_back(Geni); cases.push_back(Dati);
cases.push_back(Accu);cases.push_back(Inst);
cases.push_back(Inst2);cases.push_back(Prep);
formular_t formular[6]= {
{dfltrulesR, sizeof(dfltrulesR)/sizeof(*dfltrulesR)},
{dfltrulesD, sizeof(dfltrulesD)/sizeof(*dfltrulesD)},
{dfltrulesV, sizeof(dfltrulesV)/sizeof(*dfltrulesV)},
{dfltrulesT, sizeof(dfltrulesT)/sizeof(*dfltrulesT)},
{dfltrulesT2, sizeof(dfltrulesT2)/sizeof(*dfltrulesT2)},
{dfltrulesP, sizeof(dfltrulesP)/sizeof(*dfltrulesP)},
};
for(size_t f = 0; f < sizeof(formular)/sizeof(*formular); f++)
{
for(size_t r = 0; r < formular[f].size; r++)
{
COneCaseRule newrule( readOpts(formular[f].arr[r][0]), formular[f].arr[r][1], formular[f].arr[r][2] );
cases[f].push_back(newrule);
}
}
};
// Ôóíêöèÿ ÷òåíèÿ ïðàâèë
void REParser::readrules(LPSTR fname)
{
FILE *f;
// ïûòàåìñÿ îòêðûòü ôàéë
if (!(f = fopen(fname, "r")))
{
this->defrules(); // çàãðóæàåì ïðàâèëà ïî-óìîë÷àíèþ
return;
}
string text, mode="", word_set_name="";
char instr[MAX_STRLEN];
match_results results;
subst_results sub_results;
int opts=0;
vector<COneCaseRule> *ruleslist[6]={&Geni, &Dati, &Accu, &Inst, &Inst2, &Prep};
// ïîñòðî÷íî ÷èòàåì ôàéë
while (fgets( instr, MAX_STRLEN, f)!=NULL)
{
text = instr;
// ïðîâåðÿåì íàëè÷èå äèðåêòèâû (ìåòêè)
if (instr[0]=='#')
{
static const rpattern pat( MARK_SCHEME );
match_results::backref_type basmat = pat.match( text, results, 0 );
if (basmat.matched) mode = results.backref(1).str();
}
if (mode.size()>1 && text.size()>1) // íå áóäåì ïàðñèòü ëèøíèé ðàç
{
if (mode=="ñèìâîëû")
{
static const rpattern pat( SYM_SCHEME );
match_results::backref_type basmat = pat.match( text, results, 0 );
if (basmat.matched)
{
symbols.push_back( results.backref(1).str() );
symbols.push_back( results.backref(2).str() );
}
}
else
if (mode=="âñå")
{
// ïðîâåðêà ïðàâèëà îáû÷íîãî ïàäåæà
static const rpattern pat( COMMON_SCHEME );
match_results::backref_type basmat = pat.match( text, results, 0 );
if (basmat.matched)
{
int opts = readOpts(results.backref(1).str());
string strpattern = results.backref(2).str()+"$";
// ìåíÿåì öèôðû íà ñòðîêè
for (int i=0; i<symbols.size()/2; i++)
{
rpattern s_crlf( symbols[i*2] , symbols[i*2+1], GLOBAL );
s_crlf.substitute( strpattern, sub_results );
}
CCommonCaseRule newrule(opts, strpattern, results.backref(3).str(),
results.backref(4).str(),results.backref(5).str(),results.backref(6).str(),
results.backref(7).str());
CRules.push_back(newrule);
}
}
else
{
// ïðàâèëà íà îäèí ïàäåæ
static const rpattern pat( SCHEME );
match_results::backref_type basmat = pat.match( text, results, 0 );
if (basmat.matched)
{
if (mode=="ðîäèòåëüíûé") opts=GENITIVE;
if (mode=="äàòåëüíûé") opts=DATIVE;
if (mode=="âèíèòåëüíûé") opts=ACCUSATIVE;
if (mode=="òâîðèòåëüíûé") opts=INSTRUMENTAL;
if (mode=="òâîðèòåëüíûé2") opts=INSTRUMENTAL2;
if (mode=="ïðåäëîæíûé") opts=PREPOSITIONAL;
opts |= readOpts(results.backref(1).str());
string strpattern = results.backref(2).str()+"$";
// ìåíÿåì öèôðû íà ñòðîêè
for ( int i=0; i<symbols.size()/2; i++ )
{
rpattern s_crlf( symbols[i*2] , symbols[i*2+1], GLOBAL );
s_crlf.substitute( strpattern, sub_results );
}
COneCaseRule newrule( opts, strpattern, results.backref(3).str() );
ruleslist[((opts&ALL)>>8)-1]->push_back(newrule);
}
}
}
}
fclose(f);
};
// Ðàñøèôðîâêà îïöèé èç ñòðîêè è ïåðâîä â áèòîâûé êîä
int REParser::readOpts(string opts)
{
char str[MAX_STRLEN];
strncpy(str,opts.c_str(),16);
int len = strlen(str);
unsigned int result=0;
for (int i=0; i<len; i++)
{
switch(str[i])
{
case 'S': result|=SINGL; break;
case 'P': result|=PLURAL; break;
case 'N': result|=NEUTER; break;
case 'M': result|=MALE; break;
case 'F': result|=FEMALE; break;
case 'A': result|=ANIMATE; break;
case 'U': result|=UDAR; break;
case 'n': result|=NOUN; break;
case 'a': result|=ADJ; break;
}
}
return result;
}
void REParser::init()
{
// ïîïîëíÿåì íàáîð âñòðîåííûõ escape-ïîñëåäîâàòåëüíîñòåé
// \R - ðóññêèå è àíãèéñêèå ñëîâàðíûå ñèìâîëû + '$' + '(' + ')'
perl_syntax<char>::register_intrinsic_charset( 'R', "[à-ÿÀ-ß\\w()\$]" );
};
// Âîçâðàùàåò âûáðàííûé ïàäåæ
string REParser::TakePad(string in, int mode)
{
bool found=false;
match_results results;
subst_results sub_results;
// ñíà÷àëà îáðàáàòûâàåì ÷àñòíûå ñëó÷àè
vector<COneCaseRule> *ruleslist[6]={&Geni, &Dati, &Accu, &Inst, &Inst2, &Prep};
int indx = ((mode&ALL)>>8)-1;
for (int i=0; i<ruleslist[indx]->size() && !found; i++)
{
const int opts = (*ruleslist)[indx][i].getOpts();
if ( ( !((opts^mode)&3) || !(opts&3) ) && ( !((opts^mode)&12) || !(opts&12) ) && ( !((opts^mode)&UDAR) || !(opts&UDAR) )
&& ( !((opts^mode)&ANIMATE) || !(opts&ANIMATE) ) && (!(opts&NOUN) || !((opts^mode)&NOUN)) && (!(opts&ADJ) || !((opts^mode)&ADJ)) )
{
rpattern pat( (*ruleslist)[indx][i].getPatternStr() );
match_results::backref_type basmat = pat.match( in, results, 0 );
if (basmat.matched)
{
rpattern s_crlf( (*ruleslist)[indx][i].getPatternStr(), (*ruleslist)[indx][i].getReplaceStr(), RIGHTMOST );
s_crlf.substitute( in, sub_results );
return in;
}
}
}
// îáðàáàòûâàåì îáùèé ñëó÷àé
for ( i=0; i<CRules.size() && !found; i++)
{
const int opts = CRules[i].getOpts();
if ( ( !((opts^mode)&3) || !(opts&3) ) && ( !((opts^mode)&12) || !(opts&12) ) && ( !((opts^mode)&UDAR) || !(opts&UDAR) )
&& ( !((opts^mode)&ANIMATE) || !(opts&ANIMATE) ) && (!(opts&NOUN) || !((opts^mode)&NOUN)) && (!(opts&ADJ) || !((opts^mode)&ADJ)) )
{
rpattern pat( CRules[i].getPatternStr() );
match_results::backref_type basmat = pat.match( in, results, 0 );
if (basmat.matched)
{
rpattern s_crlf( CRules[i].getPatternStr(), CRules[i].getCase(mode), RIGHTMOST );
s_crlf.substitute( in, sub_results );
return in;
}
}
}
return in;
};
//---------------------------------------------------------------------------
// Ïðîáåë?
bool isspace1( char c ) {
if ( c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == 0 )
return true;
return false;
}
//---------------------------------------------------------------------------
// ïîäñ÷èòàòü êîë-âî ñëîâ â ñòðîêå
int StrCount( string str ) {
char strin[2550];
strcpy(strin,str.c_str());
int cnt=0, len, mlen = (int) strlen( strin );
for (int i=0; i<mlen; i++) {
for ( len=0; ( !isspace1( strin[i+len]) ) && ( i+len != mlen ); len++ );
if ( len > 0)
cnt++;
i += len;
}
return cnt;
}
//---------------------------------------------------------------------------
// âçÿòü ñëîâî ïî íåîìåðó â ñòðîêå
string TakeWord( int num, string oldStr) {
char oldstr[2000],newstr[2000];
std::string newStr;
strcpy(oldstr, oldStr.c_str());
int svstr=0, len, mlen = (int) strlen(oldstr);
for (int i=0; i<mlen; i++) {
for ( len=0;(!isspace1(oldstr[i+len]))&&(i+len<mlen);len++ );
if ( len > 0 ) {
if ( num == ( svstr + 1 ) ) {
for ( int j=i ; j <= i + len ; j++ )
newstr[ j - i ] = oldstr[ j ];
if (isspace1(oldstr[i+len]))
newstr[len]=0;
else
newstr[len+1]=0;
newStr = newstr;
return newStr;
}
i += len;
svstr++;
}
}
lstrcpy( newstr, "error" );
newStr = newstr;
return newStr;
}
//---------------------------------------------------------------------------
// äëèíà ïðåäëîæåíèÿ äî ïðåäëîãîâ
int StrCount1( string strin ) {
match_results results;
static const rpattern pat( PREPOSIT );
int cnt=0, len;
int mlen = strin.size();
string lstw;
for (int i=0; i<mlen; i++) {
for (len=0;(!isspace1(strin.c_str()[i+len]))&&(i+len!=mlen);len++);
if (len>0) cnt++;
i+=len;
lstw = TakeWord( cnt, strin );
match_results::backref_type basmat = pat.match( lstw, results, 0 );
if (basmat.matched && cnt > 1 ) return ( cnt-1 );
}
return cnt;
}
//---------------------------------------------------------------------------
string toLowWithE( string str ) {
char up[] = "ÀÁÂÃÄŨ¸ÆÇÈÉÊËÌÍÎÏÐÑÒÓÔÕ×ÖØÙÚÛÜÝÞß";
char down[] = "àáâãäåååæçèéêëìíîïðñòóôõ÷öøùúûüýþÿ";
char s[2000];
strncpy(s,str.c_str(),1999);
for ( int i = 0; i < (int) strlen( s ); i++ )
{
for ( int j = 0; j < (int) strlen( up ); j++ )
if ( s[i] == up[j] )
s[i] = down[j];
}
return s;
}
//---------------------------------------------------------------------------
int strcmpe( string str, string str1) {
int len1 = str.size();
int len2 = str1.size();
int i=0;
for (i=0; i<len1 && i<len2; i++)
{
if ((i==len1 || i==len2) || (str.c_str()[i]!=str1.c_str()[i])) return i;
}
return i;
}