-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathspelldump.cpp
1193 lines (940 loc) · 26 KB
/
spelldump.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
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//
// $Id$
//
//
// Copyright (c) 2001-2015, Andrew Aksyonoff
// Copyright (c) 2008-2015, Sphinx Technologies Inc
// All rights reserved
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License. You should have
// received a copy of the GPL license along with this program; if you
// did not, you can find it at http://www.gnu.org/
//
#include "sphinx.h"
#include "sphinxutils.h"
#include <locale.h>
const int MAX_STR_LENGTH = 512;
//////////////////////////////////////////////////////////////////////////
BYTE GetWordchar ( const char * & szSet )
{
if ( *szSet=='\\' )
{
if ( !szSet[1] || !szSet[2] || !szSet[3] )
return 0;
char szBuf[3];
memcpy ( szBuf, szSet+2, 2 );
szBuf[2] = 0;
char * szStop = NULL;
int iRes = strtol ( szBuf, &szStop, 16 );
if ( szStop!=szBuf+2 || iRes<0 || iRes>255 )
return 0;
szSet += 4;
return (BYTE) iRes;
}
return *szSet++;
}
bool IsInSet ( BYTE uLetter, const char * szSet )
{
if ( !szSet )
return false;
bool bInvert = ( *szSet=='^' );
if ( bInvert )
++szSet;
const char * szSep = strchr ( szSet, '-' );
bool bRange = ( szSep!=NULL );
if ( bRange )
{
BYTE uRange1 = GetWordchar ( szSet );
szSep++;
BYTE uRange2 = GetWordchar ( szSep );
if ( uLetter>=Min ( uRange1, uRange2 ) && uLetter<=Max ( uRange1, uRange2 ) )
return !bInvert;
} else
{
BYTE uChar = 0;
while ( ( uChar = GetWordchar ( szSet ) )!=0 )
if ( uChar==uLetter )
break;
bool bEnd = !uChar;
if ( bInvert && bEnd )
return true;
if ( !bInvert && !bEnd )
return true;
}
return false;
}
bool GetSetMinMax ( const char * szSet, BYTE & uMin, BYTE & uMax )
{
if ( !szSet || !*szSet )
return false;
uMin = GetWordchar ( szSet );
uMax = uMin;
BYTE uChar;
while ( ( uChar = GetWordchar ( szSet ) )!=0 )
if ( uChar!='-' )
{
uMin = Min ( uMin, uChar );
uMax = Max ( uMax, uChar );
}
if ( !uMin || !uMax )
return false;
return true;
}
//////////////////////////////////////////////////////////////////////////
class CISpellDict
{
public:
struct CISpellDictWord
{
CSphString m_sWord;
CSphString m_sFlags;
};
bool Load ( const char * szFilename );
void IterateStart ();
const CISpellDictWord * IterateNext ();
private:
CSphVector < CISpellDictWord > m_dEntries;
int m_iIterator;
};
bool CISpellDict::Load ( const char * szFilename )
{
if ( !szFilename )
return false;
m_dEntries.Reset ();
m_dEntries.Reserve ( 131072 );
FILE * pFile = fopen ( szFilename, "rt" );
if ( !pFile )
return false;
char szWordBuffer [MAX_STR_LENGTH];
while ( !feof ( pFile ) )
{
char * szResult = fgets ( szWordBuffer, MAX_STR_LENGTH, pFile );
if ( !szResult )
break;
int iPos = strlen ( szWordBuffer ) - 1;
while ( iPos>=0 && isspace ( (unsigned char)szWordBuffer[iPos] ) )
szWordBuffer [iPos--] = '\0';
CISpellDictWord Word;
char * szPosition = strchr ( szWordBuffer, '/' );
if ( !szPosition )
{
szPosition = szWordBuffer;
while ( *szPosition && !isspace ( (unsigned char)*szPosition ) )
++szPosition;
*szPosition = '\0';
Word.m_sWord = szWordBuffer;
} else
{
*szPosition = '\0';
Word.m_sWord = szWordBuffer;
++szPosition;
char * szFlags = szPosition;
while ( *szPosition && !isspace ( (unsigned char)*szPosition ) )
++szPosition;
*szPosition = '\0';
Word.m_sFlags = szFlags;
}
m_dEntries.Add ( Word );
}
fclose ( pFile );
return true;
}
void CISpellDict::IterateStart ()
{
m_iIterator = 0;
}
const CISpellDict::CISpellDictWord * CISpellDict::IterateNext ()
{
if ( m_iIterator>=m_dEntries.GetLength() )
return NULL;
return &m_dEntries [m_iIterator++];
}
//////////////////////////////////////////////////////////////////////////
enum RuleType_e
{
RULE_NONE,
RULE_PREFIXES,
RULE_SUFFIXES
};
class CISpellAffixRule
{
public:
CISpellAffixRule () {}
CISpellAffixRule ( RuleType_e eRule, char cFlag, bool bCrossProduct, char * szCondition, char * szStrip, char * szAppend );
bool Apply ( CSphString & sWord );
char Flag () const;
bool IsCrossProduct () const;
bool IsPrefix () const;
private:
RuleType_e m_eRule;
char m_cFlag;
bool m_bCrossProduct;
CSphString m_sCondition;
CSphString m_sStrip;
CSphString m_sAppend;
int m_iWordLen;
int m_iCondLen;
int m_iStripLen;
int m_iAppendLen;
bool CheckSuffix ( const CSphString & sWord ) const;
bool CheckPrefix ( const CSphString & sWord ) const;
bool StripAppendSuffix ( CSphString & sWord ) const;
bool StripAppendPrefix ( CSphString & sWord ) const;
};
CISpellAffixRule::CISpellAffixRule ( RuleType_e eRule, char cFlag, bool bCrossProduct, char * szCondition, char * szStrip, char * szAppend )
: m_eRule ( eRule )
, m_cFlag ( cFlag )
, m_bCrossProduct ( bCrossProduct )
, m_sCondition ( szCondition )
, m_sStrip ( szStrip )
, m_sAppend ( szAppend )
, m_iWordLen ( 0 )
{
m_iCondLen = szCondition ? strlen ( szCondition ) : 0;
m_iStripLen = szStrip ? strlen ( szStrip ) : 0;
m_iAppendLen = szAppend ? strlen ( szAppend ) : 0;
}
bool CISpellAffixRule::Apply ( CSphString & sWord )
{
if ( m_sCondition.IsEmpty () )
return true;
if ( sWord.IsEmpty () )
return false;
m_iWordLen = strlen ( sWord.cstr () );
bool bDotCond = ( m_sCondition=="." );
if ( m_eRule==RULE_SUFFIXES )
{
if ( !bDotCond && !CheckSuffix ( sWord ) )
return false;
if ( !StripAppendSuffix ( sWord ) )
return false;
} else
{
if ( !bDotCond && !CheckPrefix ( sWord ) )
return false;
if ( !StripAppendPrefix ( sWord ) )
return false;
}
return true;
}
bool CISpellAffixRule::CheckSuffix ( const CSphString & sWord ) const
{
int iCondI = m_iCondLen-1;
for ( int i=m_iWordLen-1; iCondI>=0 && i>=0; --i )
{
if ( m_sCondition.cstr()[iCondI]=='.' )
{
--iCondI;
} else if ( m_sCondition.cstr()[iCondI]!=']' )
{
if ( m_sCondition.cstr()[iCondI]!=sWord.cstr()[i] )
return false;
--iCondI;
} else
{
int iRangeStart = -1;
for ( int j=iCondI; j>=0 && iRangeStart==-1; --j )
if ( m_sCondition.cstr()[j]=='[' )
iRangeStart = j;
if ( iRangeStart==-1 )
return false;
else
{
if ( !IsInSet ( sWord.cstr () [i], m_sCondition.SubString ( iRangeStart + 1, iCondI - iRangeStart - 1 ).cstr () ) )
return false;
iCondI = iRangeStart - 1;
}
}
}
return true;
}
bool CISpellAffixRule::StripAppendSuffix ( CSphString & sWord ) const
{
static char szTmp [ MAX_STR_LENGTH];
if ( !m_sStrip.IsEmpty () )
{
if ( m_iWordLen < m_iStripLen )
return false;
if ( strncmp ( sWord.cstr () + m_iWordLen - m_iStripLen, m_sStrip.cstr (), m_iStripLen ) )
return false;
}
strncpy ( szTmp, sWord.cstr (), m_iWordLen - m_iStripLen );
szTmp [m_iWordLen - m_iStripLen] = '\0';
if ( !m_sAppend.IsEmpty () )
strcat ( szTmp, m_sAppend.cstr () ); // NOLINT
sWord = szTmp;
return true;
}
bool CISpellAffixRule::CheckPrefix ( const CSphString & sWord ) const
{
int iCondI = 0;
for ( int i = 0; iCondI < m_iCondLen && i < m_iWordLen; ++i )
{
if ( m_sCondition.cstr()[iCondI]!='[' )
{
if ( m_sCondition.cstr()[iCondI]!=sWord.cstr()[i] )
return false;
++iCondI;
} else
{
int iRangeEnd = -1;
for ( int j=iCondI; j<m_iCondLen && iRangeEnd==-1; ++j )
if ( m_sCondition.cstr()[j]==']' )
iRangeEnd = j;
if ( iRangeEnd==-1 )
return false;
else
{
if ( !IsInSet ( sWord.cstr () [i], m_sCondition.SubString ( iCondI + 1, iRangeEnd - iCondI - 1 ).cstr () ) )
return false;
}
}
}
return true;
}
bool CISpellAffixRule::StripAppendPrefix ( CSphString & sWord ) const
{
static char szTmp [MAX_STR_LENGTH];
if ( !m_sStrip.IsEmpty () )
{
const char * Pos = strstr ( sWord.cstr (), m_sStrip.cstr () );
if ( Pos!=sWord.cstr() )
return false;
}
if ( !m_sAppend.IsEmpty () )
strcpy ( szTmp, m_sAppend.cstr() ); // NOLINT
strncpy ( szTmp + m_iAppendLen, sWord.cstr () + m_iStripLen, m_iWordLen - m_iStripLen );
szTmp [m_iWordLen - m_iStripLen + m_iAppendLen] = '\0';
sWord = szTmp;
return true;
}
char CISpellAffixRule::Flag () const
{
return m_cFlag;
}
bool CISpellAffixRule::IsCrossProduct () const
{
return m_bCrossProduct;
}
bool CISpellAffixRule::IsPrefix () const
{
return m_eRule==RULE_PREFIXES;
}
//////////////////////////////////////////////////////////////////////////
enum AffixFormat_e
{
AFFIX_FORMAT_ISPELL = 0,
AFFIX_FORMAT_MYSPELL = 1,
AFFIX_FORMAT_UNKNOWN
};
const char * AffixFormatName[] =
{
"ISpell",
"MySpell"
};
class CISpellAffix
{
public:
CISpellAffix ( const char * szLocale, const char * szCharsetFile );
bool Load ( const char * szFilename );
CISpellAffixRule * GetRule ( int iRule );
int GetNumRules () const;
bool CheckCrosses () const;
private:
CSphVector < CISpellAffixRule > m_dRules;
char m_dCharset [256];
bool m_bFirstCaseConv;
CSphString m_sLocale;
CSphString m_sCharsetFile;
bool m_bCheckCrosses;
CSphLowercaser m_LowerCaser;
bool m_bUseLowerCaser;
bool m_bUseDictConversion;
bool AddToCharset ( char * szRangeL, char * szRangeU );
void AddCharPair ( BYTE uCharL, BYTE uCharU );
void Strip ( char * szText );
char ToLowerCase ( char cChar );
void LoadLocale ();
AffixFormat_e DetectFormat ( FILE * );
bool LoadISpell ( FILE * );
bool LoadMySpell ( FILE * );
};
CISpellAffix::CISpellAffix ( const char * szLocale, const char * szCharsetFile )
: m_bFirstCaseConv ( true )
, m_sLocale ( szLocale )
, m_sCharsetFile ( szCharsetFile )
, m_bCheckCrosses ( false )
, m_bUseLowerCaser ( false )
, m_bUseDictConversion ( false )
{
}
AffixFormat_e CISpellAffix::DetectFormat ( FILE * pFile )
{
char sBuffer [MAX_STR_LENGTH];
while ( !feof ( pFile ) )
{
char * sLine = fgets ( sBuffer, MAX_STR_LENGTH, pFile );
if ( !sLine )
break;
if ( !strncmp ( sLine, "SFX", 3 ) ) return AFFIX_FORMAT_MYSPELL;
if ( !strncmp ( sLine, "PFX", 3 ) ) return AFFIX_FORMAT_MYSPELL;
if ( !strncmp ( sLine, "REP", 3 ) ) return AFFIX_FORMAT_MYSPELL;
if ( !strncasecmp ( sLine, "prefixes", 8 ) ) return AFFIX_FORMAT_ISPELL;
if ( !strncasecmp ( sLine, "suffixes", 8 ) ) return AFFIX_FORMAT_ISPELL;
if ( !strncasecmp ( sLine, "flag", 4 ) ) return AFFIX_FORMAT_ISPELL;
}
return AFFIX_FORMAT_UNKNOWN;
}
bool CISpellAffix::Load ( const char * szFilename )
{
if ( !szFilename )
return false;
m_dRules.Reset ();
memset ( m_dCharset, 0, sizeof ( m_dCharset ) );
m_bFirstCaseConv = true;
m_bUseLowerCaser = false;
m_bUseDictConversion = false;
m_LowerCaser.Reset ();
FILE * pFile = fopen ( szFilename, "rt" );
if ( !pFile )
return false;
bool bResult = false;
AffixFormat_e eFormat = DetectFormat ( pFile );
if ( eFormat==AFFIX_FORMAT_UNKNOWN )
printf ( "Failed to detect affix file format\n" );
else
{
fseek ( pFile, SEEK_SET, 0 );
printf ( "Using %s affix file format\n", AffixFormatName[eFormat] );
switch ( eFormat )
{
case AFFIX_FORMAT_MYSPELL: bResult = LoadMySpell ( pFile ); break;
case AFFIX_FORMAT_ISPELL: bResult = LoadISpell ( pFile ); break;
case AFFIX_FORMAT_UNKNOWN: break;
}
}
fclose ( pFile );
bool bHaveCrossPrefix = false;
for ( int i = 0; i < m_dRules.GetLength () && !bHaveCrossPrefix; i++ )
if ( m_dRules[i].IsPrefix() && m_dRules[i].IsCrossProduct() )
bHaveCrossPrefix = true;
bool bHaveCrossSuffix = false;
for ( int i = 0; i < m_dRules.GetLength () && !bHaveCrossSuffix; i++ )
if ( !m_dRules[i].IsPrefix() && m_dRules[i].IsCrossProduct() )
bHaveCrossSuffix = true;
m_bCheckCrosses = bHaveCrossPrefix && bHaveCrossSuffix;
return bResult;
}
bool CISpellAffix::LoadISpell ( FILE * pFile )
{
char szBuffer [ MAX_STR_LENGTH ];
char szCondition [ MAX_STR_LENGTH ];
char szStrip [ MAX_STR_LENGTH ];
char szAppend [ MAX_STR_LENGTH ];
RuleType_e eRule = RULE_NONE;
char cFlag = '\0';
bool bCrossProduct = false;
int iLine = 0;
// TODO: parse all .aff character replacement commands
while ( !feof ( pFile ) )
{
char * szResult = fgets ( szBuffer, MAX_STR_LENGTH, pFile );
if ( !szResult )
break;
iLine++;
if ( !strncasecmp ( szBuffer, "prefixes", 8 ) )
{
eRule = RULE_PREFIXES;
continue;
}
if ( !strncasecmp ( szBuffer, "suffixes", 8 ) )
{
eRule = RULE_SUFFIXES;
continue;
}
if ( !strncasecmp ( szBuffer, "wordchars", 9 ) )
{
char * szStart = szBuffer + 9;
while ( *szStart && isspace ( (unsigned char) *szStart ) )
++szStart;
char * szRangeL = szStart;
while ( *szStart && !isspace ( (unsigned char) *szStart ) )
++szStart;
if ( !*szStart )
{
printf ( "WARNING: Line %d: invalid 'wordchars' statement\n", iLine );
continue;
}
*szStart = '\0';
++szStart;
while ( *szStart && isspace ( (unsigned char) *szStart ) )
++szStart;
char * szRangeU = szStart;
while ( *szStart && !isspace ( (unsigned char) *szStart ) )
++szStart;
*szStart = '\0';
if ( !AddToCharset ( szRangeL, szRangeU ) )
printf ( "WARNING: Line %d: cannot add to charset: '%s' '%s'\n", iLine, szRangeL, szRangeU );
continue;
}
if ( !strncasecmp ( szBuffer, "flag", 4 ) )
{
if ( eRule==RULE_NONE )
{
printf ( "WARNING: Line %d: 'flag' appears before preffixes or suffixes\n", iLine );
continue;
}
char * szStart = szBuffer + 4;
while ( *szStart && isspace ( (unsigned char) *szStart ) )
++szStart;
bCrossProduct = ( *szStart=='*' );
cFlag = bCrossProduct ? *(szStart + 1) : *(szStart);
continue;
}
if ( eRule==RULE_NONE )
continue;
char * szComment = strchr ( szBuffer, '#' );
if ( szComment )
*szComment = '\0';
if ( !* szBuffer )
continue;
szCondition[0] = '\0';
szStrip[0] = '\0';
szAppend[0] = '\0';
int nFields = sscanf ( szBuffer, "%[^>\n]>%[^,\n],%[^\n]", szCondition, szStrip, szAppend ); // NOLINT
Strip ( szCondition );
Strip ( szStrip );
Strip ( szAppend );
switch ( nFields )
{
case 2: // no optional strip-string
strcpy ( szAppend, szStrip ); // NOLINT
szStrip[0] = '\0';
break;
case 3: // all read
break;
default: // invalid repl
continue;
}
CISpellAffixRule Rule ( eRule, cFlag, bCrossProduct, szCondition, szStrip, szAppend );
m_dRules.Add ( Rule );
}
return true;
}
bool CISpellAffix::LoadMySpell ( FILE * pFile )
{
char sBuffer [MAX_STR_LENGTH];
char sCondition [MAX_STR_LENGTH];
char sRemove [MAX_STR_LENGTH];
char sAppend [MAX_STR_LENGTH];
RuleType_e eRule = RULE_NONE;
BYTE cFlag = 0;
BYTE cCombine = 0;
int iCount = 0, iLine = 0;
const char * sMode = 0;
while ( !feof ( pFile ) )
{
char * sLine = fgets ( sBuffer, MAX_STR_LENGTH, pFile );
if ( !sLine )
break;
++iLine;
// prefix and suffix rules
RuleType_e eNewRule = RULE_NONE;
if ( !strncmp ( sLine, "PFX", 3 ) )
{
eNewRule = RULE_PREFIXES;
sMode = "prefix";
} else if ( !strncmp ( sLine, "SFX", 3 ) )
{
eNewRule = RULE_SUFFIXES;
sMode = "suffix";
}
if ( eNewRule!=RULE_NONE )
{
sLine += 3;
while ( *sLine && isspace ( (unsigned char) *sLine ) )
++sLine;
if ( eNewRule!=eRule ) // new rule header
{
if ( iCount )
printf ( "WARNING: Line %d: Premature end of entries.\n", iLine );
if ( sscanf ( sLine, "%c %c %d", &cFlag, &cCombine, &iCount )!=3 ) // NOLINT
printf ( "WARNING; Line %d: Malformed %s header\n", iLine, sMode );
eRule = eNewRule;
} else // current rule continued
{
*sRemove = *sAppend = 0;
char cNewFlag;
if ( sscanf ( sLine, "%c %s %s %s", &cNewFlag, sRemove, sAppend, sCondition )==4 ) // NOLINT
{
if ( cNewFlag!=cFlag )
printf ( "WARNING: Line %d: Flag character mismatch\n", iLine );
if ( *sRemove=='0' && *(sRemove + 1)==0 ) *sRemove = 0;
if ( *sAppend=='0' && *(sAppend + 1)==0 ) *sAppend = 0;
CISpellAffixRule Rule ( eRule, cFlag, cCombine=='Y', sCondition, sRemove, sAppend );
m_dRules.Add ( Rule );
} else
printf ( "WARNING: Line %d: Malformed %s rule\n", iLine, sMode );
if ( !--iCount ) eRule = RULE_NONE;
}
continue;
}
}
return true;
}
CISpellAffixRule * CISpellAffix::GetRule ( int iRule )
{
return &m_dRules [iRule];
}
int CISpellAffix::GetNumRules () const
{
return m_dRules.GetLength ();
}
bool CISpellAffix::CheckCrosses () const
{
return m_bCheckCrosses;
}
bool CISpellAffix::AddToCharset ( char * szRangeL, char * szRangeU )
{
if ( !szRangeL || !szRangeU )
return false;
int iLengthL = strlen ( szRangeL );
int iLengthU = strlen ( szRangeU );
bool bSetL = ( iLengthL>0 && szRangeL[0]=='[' && szRangeL[iLengthL-1]==']' );
bool bSetR = ( iLengthU>0 && szRangeU[0]=='[' && szRangeU[iLengthU-1]==']' );
if ( bSetL!=bSetR )
return false;
if ( bSetL )
{
szRangeL [iLengthL - 1] = '\0';
szRangeL = szRangeL + 1;
szRangeU [iLengthU - 1] = '\0';
szRangeU = szRangeU + 1;
BYTE uMinL, uMaxL;
if ( !GetSetMinMax ( szRangeL, uMinL, uMaxL ) )
return false;
BYTE uMinU, uMaxU;
if ( !GetSetMinMax ( szRangeU, uMinU, uMaxU ) )
return false;
if ( ( uMaxU - uMinU )!=( uMaxL - uMinL ) )
return false;
for ( BYTE i=0; i<=( uMaxL - uMinL ); ++i )
if ( IsInSet ( uMinL + i, szRangeL ) && IsInSet ( uMinU + i, szRangeU ) )
AddCharPair ( uMinL + i, uMinU + i );
} else
{
if ( iLengthL > 4 || iLengthU > 4 )
return false;
const char * szL = szRangeL;
const char * szU = szRangeU;
AddCharPair ( GetWordchar(szL), GetWordchar(szU) );
}
m_bUseDictConversion = true;
return true;
}
void CISpellAffix::AddCharPair ( BYTE uCharL, BYTE uCharU )
{
m_dCharset [uCharU] = uCharL;
}
void CISpellAffix::Strip ( char * szText )
{
char * szIterator1 = szText;
char * szIterator2 = szText;
while ( *szIterator1 )
{
if ( !isspace ( (unsigned char) *szIterator1 ) && *szIterator1!='-' )
{
*szIterator2 = *szIterator1;
++szIterator2;
}
++szIterator1;
}
*szIterator2 = '\0';
while ( *szText )
{
*szText = ToLowerCase ( *szText );
++szText;
}
}
char CISpellAffix::ToLowerCase ( char cChar )
{
if ( m_bFirstCaseConv )
{
LoadLocale ();
m_bFirstCaseConv = false;
}
// dictionary conversion
if ( m_bUseDictConversion )
return m_dCharset [(BYTE) cChar] ? m_dCharset [(BYTE) cChar] : cChar;
// user-defined character mapping
if ( m_bUseLowerCaser )
{
char cResult = (char)m_LowerCaser.ToLower ( (BYTE) cChar );
return cResult ? cResult : cChar;
}
// user-specified code page conversion
return (char)tolower ( (BYTE)cChar ); // workaround for systems (eg. FreeBSD) which default to signed char. marvelous!
}
void CISpellAffix::LoadLocale ()
{
if ( m_bUseDictConversion )
printf ( "Using dictionary-defined character set\n" );
else
if ( !m_sCharsetFile.IsEmpty () )
{
FILE * pFile = fopen ( m_sCharsetFile.cstr (), "rt" );
if ( pFile )
{
printf ( "Using charater set from '%s'\n", m_sCharsetFile.cstr () );
const int MAX_CHARSET_LENGTH = 4096;
char szBuffer [MAX_CHARSET_LENGTH];
char * szResult = fgets ( szBuffer, MAX_CHARSET_LENGTH, pFile );
if ( szResult )
{
CSphVector<CSphRemapRange> dRemaps;
if ( sphParseCharset ( szBuffer, dRemaps ) )
{
m_bUseLowerCaser = true;
m_LowerCaser.AddRemaps ( dRemaps, 0 );
} else
{
printf ( "Failed to parse charset from '%s'\n", m_sCharsetFile.cstr() );
}
} else
{
printf ( "Failed to read charset from '%s'\n", m_sCharsetFile.cstr() );
}
fclose ( pFile );
} else
{
printf ( "Failed to open '%s'\n", m_sCharsetFile.cstr() );
}
} else
{
if ( !m_sLocale.IsEmpty () )
{
char dLocaleC[256], dLocaleUser[256];
setlocale ( LC_ALL, "C" );
for ( int i=0; i<256; i++ )
dLocaleC[i] = (char) tolower(i);
char * szLocale = setlocale ( LC_CTYPE, m_sLocale.cstr() );
if ( szLocale )
{
printf ( "Using user-defined locale (locale=%s)\n", m_sLocale.cstr() );
for ( int i=0; i<256; i++ )
dLocaleUser[i] = (char) tolower(i);
if ( !memcmp ( dLocaleC, dLocaleUser, 256 ) )
printf ( "WARNING: user-defined locale provides the same case conversion as the default \"C\" locale\n" );
} else
printf ( "WARNING: could not set user-defined locale for case conversions (locale=%s)\n", m_sLocale.cstr() );
} else
printf ( "WARNING: no character set specified\n" );
}
}
//////////////////////////////////////////////////////////////////////////
enum OutputMode_e
{
M_DEBUG,
M_DUPLICATES,
M_LAST,
M_EXACT_OR_LONGEST,
M_DEFAULT = M_EXACT_OR_LONGEST
};
const char * dModeName[] =
{
"debug",
"duplicates",
"last"
};
struct MapInfo_t
{
CSphString m_sWord;
char m_sRules[3];
};
struct WordLess
{
inline bool IsLess ( const char * a, const char * b ) const
{
return strcoll ( a, b ) < 0;
}
};
typedef CSphOrderedHash < CSphVector<MapInfo_t>, CSphString, CSphStrHashFunc, 100000 > WordMap_t;
static void EmitResult ( WordMap_t & tMap , const CSphString & sFrom, const CSphString & sTo, char cRuleA = 0, char cRuleB = 0 )
{
if ( !tMap.Exists(sFrom) )
tMap.Add ( CSphVector<MapInfo_t>(), sFrom );
MapInfo_t tInfo;
tInfo.m_sWord = sTo;
tInfo.m_sRules[0] = cRuleA;
tInfo.m_sRules[1] = cRuleB;
tInfo.m_sRules[2] = 0;
tMap[sFrom].Add ( tInfo );
}
int main ( int iArgs, char ** dArgs )
{
OutputMode_e eMode = M_DEFAULT;
bool bUseCustomCharset = false;
CSphString sDict, sAffix, sLocale, sCharsetFile, sResult = "result.txt";
printf ( "spelldump, an ispell dictionary dumper\n\n" );
int i = 1;