-
Notifications
You must be signed in to change notification settings - Fork 51
/
Copy pathsphinxmetaphone.cpp
665 lines (535 loc) · 17.1 KB
/
sphinxmetaphone.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
//
// $Id: sphinxmetaphone.cpp 4885 2015-01-20 07:02:07Z deogar $
//
//
// 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 "sphinxint.h"
struct CurrentWord_t
{
BYTE * pWord;
int iLength;
int iLengthPadded;
};
static bool IsVowel ( BYTE c )
{
return c=='A' || c=='E' || c=='I' || c=='O' || c=='U' || c=='Y';
}
static bool SlavoGermanic ( BYTE * pString )
{
// OPTIMIZE!
char * szWord = (char *) pString;
if ( strstr ( szWord, "W" ) )
return true;
if ( strstr ( szWord, "K" ) )
return true;
if ( strstr ( szWord, "CZ" ) )
return true;
if ( strstr ( szWord, "WITZ" ) )
return true;
return false;
}
static bool StrAt ( const CurrentWord_t & Word, int iStart, int iLength, const char * szStr1 )
{
if ( iStart<0 || iStart>=Word.iLengthPadded )
return false;
char * szPos = (char *)Word.pWord + iStart;
return !strncmp ( szPos, szStr1, iLength );
}
static bool StrAt ( const CurrentWord_t & Word, int iStart, int iLength, const char * szStr1, const char * szStr2 )
{
if ( iStart<0 || iStart>=Word.iLengthPadded )
return false;
char * szPos = (char *)Word.pWord + iStart;
return !strncmp ( szPos, szStr1, iLength ) || !strncmp ( szPos, szStr2, iLength );
}
static bool StrAt ( const CurrentWord_t & Word, int iStart, int iLength, const char * szStr1, const char * szStr2, const char * szStr3 )
{
if ( iStart<0 || iStart>=Word.iLengthPadded )
return false;
char * szPos = (char *)Word.pWord + iStart;
return !strncmp ( szPos, szStr1, iLength ) || !strncmp ( szPos, szStr2, iLength ) || !strncmp ( szPos, szStr3, iLength );
}
static bool StrAt ( const CurrentWord_t & Word, int iStart, int iLength, const char * szStr1,
const char * szStr2, const char * szStr3, const char * szStr4 )
{
if ( iStart<0 || iStart>=Word.iLengthPadded )
return false;
char * szPos = (char *)Word.pWord + iStart;
return !strncmp ( szPos, szStr1, iLength ) || !strncmp ( szPos, szStr2, iLength ) || !strncmp ( szPos, szStr3, iLength )
|| !strncmp ( szPos, szStr4, iLength );
}
static bool StrAt ( const CurrentWord_t & Word, int iStart, int iLength, const char * szStr1,
const char * szStr2, const char * szStr3, const char * szStr4, const char * szStr5 )
{
if ( iStart<0 || iStart>=Word.iLengthPadded )
return false;
char * szPos = (char *)Word.pWord + iStart;
return !strncmp ( szPos, szStr1, iLength ) || !strncmp ( szPos, szStr2, iLength ) || !strncmp ( szPos, szStr3, iLength )
|| !strncmp ( szPos, szStr4, iLength ) || !strncmp ( szPos, szStr5, iLength );
}
static void MetaphAdd ( BYTE * pPrimary, BYTE * pSecondary, const char * szAddPrimary, const char * szAddSecondary )
{
strcat ( (char*)pPrimary, szAddPrimary ); // NOLINT
strcat ( (char*)pSecondary, szAddSecondary ); // NOLINT
}
#define ADD_RET(prim,sec,adv)\
{\
MetaphAdd ( sPrimary, sSecondary, prim, sec );\
return (adv);\
}
#define ADD(prim,sec)\
MetaphAdd ( sPrimary, sSecondary, prim, sec )
static int ProcessCode ( int iCode, int iCur, CurrentWord_t & Word, BYTE * sPrimary, BYTE * sSecondary )
{
BYTE * pWord = Word.pWord;
// codepoints, not bytes
int iAdvance = 1;
// bytes, not codepoints
int iLast = Word.iLength - 1;
switch ( iCode )
{
case 'A':
case 'E':
case 'I':
case 'O':
case 'U':
case 'Y':
// all init vowels now map to 'A'
if ( !iCur )
ADD ( "A", "A" );
break;
case 'B':
// "-mb", e.g", "dumb", already skipped over...
ADD_RET ( "P", "P", ( pWord[iCur+1]=='B' ) ? 2 : 1 )
case 0xC7:
case 0xE7:
ADD_RET ( "S", "S", 1 )
case 'C':
// various germanic
if ( iCur > 1 && !IsVowel ( pWord[iCur-2] ) && StrAt ( Word, iCur-1, 3, "ACH" )
&& ( pWord[iCur+2]!='I' && ( pWord[iCur+2]!='E' || StrAt ( Word, iCur-2, 6, "BACHER", "MACHER" ) ) ) )
ADD_RET ( "K", "K", 2 )
// special case 'caesar'
if ( iCur==0 && StrAt ( Word, 0, 6, "CAESAR" ) )
ADD_RET ( "S", "S", 2 )
// italian 'chianti'
if ( StrAt ( Word, iCur, 4, "CHIA" ) )
ADD_RET ( "K", "K", 2 )
if ( StrAt ( Word, iCur, 2, "CH" ) )
{
// find 'michael'
if ( iCur > 0 && StrAt ( Word, iCur, 4, "CHAE" ) )
ADD_RET ( "K", "X", 2 )
// greek roots e.g. 'chemistry', 'chorus'
if ( iCur==0
&& ( StrAt ( Word, iCur+1, 5, "HARAC", "HARIS" )
|| StrAt ( Word, iCur+1, 3, "HOR", "HYM", "HIA", "HEM" ) )
&& !StrAt ( Word, 0, 5, "CHORE" ) )
{
ADD_RET ( "K", "K", 2 )
}
// germanic, greek, or otherwise 'ch' for 'kh' sound
if ( ( StrAt ( Word, 0, 4, "VAN ", "VON " ) || StrAt ( Word, 0, 3, "SCH" ) )
// 'architect but not 'arch', 'orchestra', 'orchid'
|| StrAt ( Word, iCur-2, 6, "ORCHES", "ARCHIT", "ORCHID" )
|| StrAt ( Word, iCur+2, 1, "T", "S" )
|| ( ( StrAt ( Word, iCur-1, 1, "A", "O", "U", "E" ) || iCur==0 ) // e.g., 'wachtler', 'wechsler', but not 'tichner'
&& ( StrAt ( Word, iCur+2, 1, "L", "R", "N", "M" ) || StrAt ( Word, iCur+2, 1, "B", "H", "F", "V" )
|| StrAt ( Word, iCur+2, 1, "W", " " ) ) ) )
{
ADD ( "K", "K" );
} else
{
if ( iCur > 0 )
{
if ( StrAt ( Word, 0, 2, "MC" ) ) // e.g., "McHugh"
ADD ( "K", "K" );
else
ADD ( "X", "K" );
} else
ADD ( "X", "X" );
}
return 2;
}
// e.g, 'czerny'
if ( StrAt ( Word, iCur, 2, "CZ" ) && !StrAt ( Word, iCur-2, 4, "WICZ" ) )
ADD_RET ( "S", "X", 2 )
// e.g., 'focaccia'
if ( StrAt ( Word, iCur+1, 3, "CIA" ) )
ADD_RET ( "X", "X", 3 )
// double 'C', but not if e.g. 'McClellan'
if ( StrAt ( Word, iCur, 2, "CC" ) && !( iCur==1 && pWord[0]=='M' ) )
{
// 'bellocchio' but not 'bacchus'
if ( StrAt ( Word, iCur+2, 1, "I", "E", "H" ) && !StrAt ( Word, iCur+2, 2, "HU" ) )
{
// 'accident', 'accede' 'succeed'
if ( ( iCur==1 && pWord[iCur-1]=='A' ) || StrAt ( Word, iCur-1, 5, "UCCEE", "UCCES" ) )
ADD_RET ( "KS", "KS", 2 )
else // 'bacci', 'bertucci', other italian
ADD_RET ( "X", "X", 2 )
} else // Pierce's rule
ADD_RET ( "K", "K", 2 )
}
if ( StrAt ( Word, iCur, 2, "CK", "CG", "CQ" ) )
ADD_RET ( "K", "K", 2 )
if ( StrAt ( Word, iCur, 2, "CI", "CE", "CY" ) )
{
// italian vs. english
if ( StrAt ( Word, iCur, 3, "CIO", "CIE", "CIA" ) )
ADD_RET ( "S", "X", 2 )
else
ADD_RET ( "S", "S", 2 )
}
// else
ADD ( "K", "K" );
// name sent in 'mac caffrey', 'mac gregor
if ( StrAt ( Word, iCur+1, 2, " C", " Q", " G" ) )
return 3;
else
{
if ( StrAt ( Word, iCur+1, 1, "C", "K", "Q" ) && !StrAt ( Word, iCur+1, 2, "CE", "CI" ) )
return 2;
}
break;
case 'D':
if ( StrAt ( Word, iCur, 2, "DG" ) )
{
if ( StrAt ( Word, iCur+2, 1, "I", "E", "Y" ) ) // e.g. 'edge'
ADD_RET ( "J", "J", 3 )
else // e.g. 'edgar'
ADD_RET ( "TK", "TK", 2 )
}
if ( StrAt ( Word, iCur, 2, "DT", "DD" ) )
ADD_RET ( "T", "T", 2 )
// else
ADD_RET ( "T", "T", 1 )
case 'F':
ADD_RET ( "F", "F", pWord[iCur+1]=='F' ? 2 : 1 )
case 'G':
if ( pWord[iCur+1]=='H' )
{
if ( iCur > 0 && !IsVowel ( pWord[iCur-1] ) )
ADD_RET ( "K", "K", 2 )
if ( iCur < 3 )
{
// 'ghislane', ghiradelli
if ( iCur==0 )
{
if ( pWord[iCur+2]=='I' )
ADD_RET ( "J", "J", 2 )
else
ADD_RET ( "K", "K", 2 )
}
}
// Parker's rule (with some further refinements) - e.g., 'hugh'
if ( ( iCur > 1 && StrAt ( Word, iCur-2, 1, "B", "H", "D" ) )
|| ( iCur > 2 && StrAt ( Word, iCur-3, 1, "B", "H", "D" ) ) // e.g., 'bough'
|| ( iCur > 3 && StrAt ( Word, iCur-4, 1, "B", "H" ) ) ) // e.g., 'broughton'
return 2;
else
{
// e.g., 'laugh', 'McLaughlin', 'cough', 'gough', 'rough', 'tough'
if ( iCur > 2 && pWord[iCur-1]=='U' && StrAt ( Word, iCur-3, 1, "C", "G", "L", "R", "T" ) )
ADD ( "F", "F" );
else
if ( iCur > 0 && pWord[iCur-1]!='I' )
ADD ( "K", "K" );
return 2;
}
}
if ( pWord[iCur+1]=='N' )
{
if ( iCur==1 && IsVowel ( pWord[0] ) && !SlavoGermanic ( pWord ) )
ADD ( "KN", "N" );
else // not e.g. 'cagney'
if ( !StrAt ( Word, iCur+2, 2, "EY" ) && pWord[iCur+1]!='Y' && !SlavoGermanic ( pWord ) )
ADD ( "N", "KN" );
else
ADD ( "KN", "KN" );
return 2;
}
// 'tagliaro'
if ( StrAt ( Word, iCur+1, 2, "LI" ) && !SlavoGermanic ( pWord ) )
ADD_RET ( "KL", "L", 2 )
// -ges-,-gep-,-gel-, -gie- at beginning
if ( iCur==0 && ( pWord[iCur+1]=='Y' || StrAt ( Word, iCur+1, 2, "ES", "EP", "EB", "EL" )
|| StrAt ( Word, iCur+1, 2, "EY", "IB", "IL", "IN" ) || StrAt ( Word, iCur+1, 2, "IE", "EI", "ER" ) ) )
ADD_RET ( "K", "J", 2 )
// -ger-, -gy-
if ( ( StrAt ( Word, iCur+1, 2, "ER" ) || pWord[iCur+1]=='Y' ) && !StrAt ( Word, 0, 6, "DANGER", "RANGER", "MANGER" )
&& !StrAt ( Word, iCur-1, 1, "E", "I" ) && !StrAt ( Word, iCur-1, 3, "RGY", "OGY" ) )
ADD_RET ( "K", "J", 2 )
// italian e.g, 'biaggi'
if ( StrAt ( Word, iCur+1, 1, "E", "I", "Y" ) || StrAt ( Word, iCur-1, 4, "AGGI", "OGGI" ) )
{
// obvious germanic
if ( StrAt ( Word, 0, 4, "VAN ", "VON " ) || StrAt ( Word, 0, 3, "SCH" ) || StrAt ( Word, iCur+1, 2, "ET" ) )
ADD ( "K", "K" );
else
{
// always soft if french ending
if ( StrAt ( Word, iCur+1, 4, "IER " ) )
ADD ( "J", "J" );
else
ADD ( "J", "K" );
}
return 2;
}
ADD_RET ( "K", "K", pWord[iCur+1]=='G' ? 2 : 1 )
case 'H':
// only keep if first & before vowel or btw. 2 vowels
if ( ( iCur==0 || IsVowel ( pWord[iCur-1] ) ) && IsVowel ( pWord[iCur+1] ) )
ADD_RET ( "H", "H", 2 )
break; // also takes care of 'HH'
case 'J':
// obvious spanish, 'jose', 'san jacinto'
if ( StrAt ( Word, iCur, 4, "JOSE" ) || StrAt ( Word, 0, 4, "SAN " ) )
{
if ( ( iCur==0 && pWord[iCur+4]==' ' ) || StrAt ( Word, 0, 4, "SAN " ) )
ADD_RET ( "H", "H", 1 )
else
ADD_RET ( "J", "H", 1 )
}
if ( iCur==0 && !StrAt ( Word, iCur, 4, "JOSE" ) )
ADD ( "J", "A" ); // Yankelovich/Jankelowicz
else
{
// spanish pron. of e.g. 'bajador'
if ( ( iCur>0 && IsVowel ( pWord[iCur-1] ) )&& !SlavoGermanic ( pWord ) && ( pWord[iCur+1]=='A' || pWord[iCur+1]=='O' ) )
ADD ( "J", "H" );
else
{
if ( iCur==iLast )
ADD ( "J", "" );
else
if ( !StrAt ( Word, iCur+1, 1, "L", "T", "K", "S" )
&& !StrAt ( Word, iCur+1, 1, "N", "M", "B", "Z" )
&& !StrAt ( Word, iCur-1, 1, "S", "K", "L" ) )
{
ADD ( "J", "J" );
}
}
}
if ( pWord[iCur+1]=='J' ) // it could happen!
return 2;
break;
case 'K':
ADD_RET ( "K", "K", pWord[iCur+1]=='K' ? 2 : 1 )
case 'L':
if ( pWord[iCur+1]=='L' )
{
// spanish e.g. 'cabrillo', 'gallegos'
if ( ( iCur==iLast-2 && StrAt ( Word, iCur-1, 4, "ILLO", "ILLA", "ALLE" ) )
|| ( ( StrAt ( Word, iLast - 1, 2, "AS", "OS" ) || StrAt ( Word, iLast, 1, "A", "O" ) ) && StrAt ( Word, iCur-1, 4, "ALLE" ) ) )
ADD_RET ( "L", "", 2 )
iAdvance = 2;
}
ADD ( "L", "L" );
break;
case 'M':
ADD ( "M", "M" );
// 'dumb','thumb'
if ( ( StrAt ( Word, iCur-1, 3, "UMB" ) && ( iCur+1==iLast || StrAt ( Word, iCur+2, 2, "ER" ) ) ) || pWord[iCur+1]=='M' )
return 2;
break;
case 'N':
ADD_RET ( "N", "N", pWord[iCur+1]=='N' ? 2 : 1 )
case 0xD1:
case 0xF1:
ADD_RET ( "N", "N", 1 )
case 'P':
if ( pWord[iCur+1]=='H' )
ADD_RET ( "F", "F", 2 )
// also account for "campbell", "raspberry"
ADD_RET ( "P", "P", StrAt ( Word, iCur+1, 1, "P", "B" ) ? 2 : 1 )
case 'Q':
ADD_RET ( "K", "K", pWord[iCur+1]=='Q' ? 2 : 1 )
case 'R':
// french e.g. 'rogier', but exclude 'hochmeier'
if ( iCur==iLast && !SlavoGermanic ( pWord ) && StrAt ( Word, iCur-2, 2, "IE" ) && !StrAt ( Word, iCur-4, 2, "ME", "MA" ) )
ADD ( "", "R" );
else
ADD ( "R", "R" );
return pWord[iCur+1]=='R' ? 2 : 1;
case 'S':
// special cases 'island', 'isle', 'carlisle', 'carlysle'
if ( StrAt ( Word, iCur-1, 3, "ISL", "YSL" ) )
return 1;
// special case 'sugar-'
if ( iCur==0 && StrAt ( Word, iCur, 5, "SUGAR" ) )
ADD_RET ( "X", "S", 1 )
if ( StrAt ( Word, iCur, 2, "SH" ) )
{
// germanic
if ( StrAt ( Word, iCur+1, 4, "HEIM", "HOEK", "HOLM", "HOLZ" ) )
ADD_RET ( "S", "S", 2 )
else
ADD_RET ( "X", "X", 2 )
}
// italian & armenian
if ( StrAt ( Word, iCur, 3, "SIO", "SIA" ) || StrAt ( Word, iCur, 4, "SIAN" ) )
{
if ( !SlavoGermanic ( pWord ) )
ADD_RET ( "S", "X", 3 )
else
ADD_RET ( "S", "S", 3 )
}
// german & anglicisations, e.g. 'smith' match 'schmidt', 'snider' match 'schneider'
// also, -sz- in slavic language altho in hungarian it is pronounced 's'
if ( ( iCur==0 && StrAt ( Word, iCur+1, 1, "M", "N", "L", "W" ) ) || StrAt ( Word, iCur+1, 1, "Z" ) )
ADD_RET ( "S", "X", StrAt ( Word, iCur+1, 1, "Z" ) ? 2 : 1 )
if ( StrAt ( Word, iCur, 2, "SC" ) )
{
// Schlesinger's rule
if ( pWord[iCur+2]=='H' )
{
if ( StrAt ( Word, iCur+3, 2, "OO", "ER", "EN", "UY" )
|| StrAt ( Word, iCur+3, 2, "ED", "EM" ) ) // dutch origin, e.g. 'school', 'schooner'
{
// 'schermerhorn', 'schenker'
if ( StrAt ( Word, iCur+3, 2, "ER", "EN" ) )
ADD_RET ( "X", "SK", 3 )
else
ADD_RET ( "SK", "SK", 3 )
} else
{
if ( iCur==0 && !IsVowel ( pWord[3] ) && pWord[3]!='W' )
ADD_RET ( "X", "S", 3 )
else
ADD_RET ( "X", "X", 3 )
}
}
if ( StrAt ( Word, iCur+2, 1, "I", "E", "Y" ) )
ADD_RET ( "S", "S", 3 )
// else
ADD_RET ( "SK", "SK", 3 )
}
// french e.g. 'resnais', 'artois'
if ( iCur==iLast && StrAt ( Word, iCur-2, 2, "AI", "OI" ) )
ADD ( "", "S" );
else
ADD ( "S", "S" );
return StrAt ( Word, iCur+1, 1, "S", "Z" ) ? 2 : 1;
case 'T':
if ( StrAt ( Word, iCur, 4, "TION" ) )
ADD_RET ( "X", "X", 3 )
if ( StrAt ( Word, iCur, 3, "TIA", "TCH" ) )
ADD_RET ( "X", "X", 3 )
if ( StrAt ( Word, iCur, 2, "TH" ) || StrAt ( Word, iCur, 3, "TTH" ) )
{
// special case 'thomas', 'thames' or germanic
if ( StrAt ( Word, iCur+2, 2, "OM", "AM" ) || StrAt ( Word, 0, 4, "VAN ", "VON " ) || StrAt ( Word, 0, 3, "SCH" ) )
ADD_RET ( "T", "T", 2 )
else
ADD_RET ( "0", "T", 2 ) // yes, zero
}
ADD_RET ( "T", "T", StrAt ( Word, iCur+1, 1, "T", "D" ) ? 2 : 1 )
case 'V':
ADD_RET ( "F", "F", pWord[iCur+1]=='V' ? 2 : 1 )
case 'W':
// can also be in middle of word
if ( StrAt ( Word, iCur, 2, "WR" ) )
ADD_RET ( "R", "R", 2 )
if ( iCur==0 && ( IsVowel ( pWord[iCur+1] ) || StrAt ( Word, iCur, 2, "WH" ) ) )
{
// Wasserman should match Vasserman
if ( IsVowel ( pWord[iCur+1] ) )
ADD ( "A", "F" );
else // need Uomo to match Womo
ADD ( "A", "A" );
}
// Arnow should match Arnoff
if ( ( iCur==iLast && iCur > 0 && IsVowel ( pWord[iCur-1] ) ) || StrAt ( Word, iCur-1, 5, "EWSKI", "EWSKY", "OWSKI", "OWSKY" )
|| StrAt ( Word, 0, 3, "SCH" ) )
ADD_RET ( "", "F", 1 )
// polish e.g. 'filipowicz'
if ( StrAt ( Word, iCur, 4, "WICZ", "WITZ" ) )
ADD_RET ( "TS", "FX", 4 )
break;
case 'X':
// french e.g. breaux
if ( !( iCur==iLast && ( StrAt ( Word, iCur-3, 3, "IAU", "EAU" ) || StrAt ( Word, iCur-2, 2, "AU", "OU" ) ) ) )
ADD ( "KS", "KS" );
return ( pWord[iCur+1]=='C' || pWord[iCur+1]=='X' ) ? 2 : 1;
case 'Z':
// chinese pinyin e.g. 'zhao'
if ( pWord[iCur+1]=='H' )
ADD_RET ( "J", "J", 2 )
else
if ( StrAt ( Word, iCur+1, 2, "ZO", "ZI", "ZA" ) || ( SlavoGermanic ( pWord ) && ( iCur > 0 && pWord[iCur-1]!='T' ) ) )
MetaphAdd ( sPrimary, sSecondary, "S", "TS" );
else
MetaphAdd ( sPrimary, sSecondary, "S", "S" );
return pWord[iCur+1]=='Z' ? 2 : 1;
}
return iAdvance;
}
void stem_dmetaphone ( BYTE * pWord )
{
BYTE sOriginal [3*SPH_MAX_WORD_LEN+3];
BYTE sPrimary [3*SPH_MAX_WORD_LEN+3];
BYTE sSecondary [ 3*SPH_MAX_WORD_LEN+3 ];
int iLength = strlen ( (const char *)pWord );
memcpy ( sOriginal, pWord, iLength + 1 );
sPrimary[0] = '\0';
sSecondary[0] = '\0';
BYTE * pStart = sOriginal;
while ( *pStart )
{
if ( *pStart>='a' && *pStart<='z' )
*pStart = (BYTE) toupper ( *pStart );
++pStart;
}
strcat ( (char *) sOriginal, " " ); // NOLINT
int iAdvance = 0;
CurrentWord_t Word;
Word.pWord = sOriginal;
Word.iLength = iLength;
Word.iLengthPadded = strlen ( (const char *)sOriginal );
// skip these when at start of word
if ( StrAt ( Word, 0, 2, "GN", "KN", "PN", "WR", "PS" ) )
iAdvance = 1;
// Initial 'X' is pronounced 'Z' e.g. 'Xavier'
if ( sOriginal[0]=='X' )
{
ADD ( "S", "S" ); // 'Z' maps to 'S'
iAdvance = 1;
}
const BYTE * pPtr = sOriginal;
const BYTE * pLastPtr = sOriginal;
int iCode = -1;
iCode = sphUTF8Decode ( pPtr );
while ( iCode!=0 )
{
int iCur = pLastPtr-sOriginal;
if ( iCur>=iLength )
break;
for ( int i = 0; i < iAdvance; ++i )
{
pLastPtr = pPtr;
iCode = sphUTF8Decode ( pPtr );
}
if ( iCode<=0 )
break;
// unknown code: don't copy, just return
if ( iCode>128 && iCode!=0xC7 && iCode!=0xE7 && iCode!=0xD1 && iCode!=0xF1 )
return;
iAdvance = ProcessCode ( iCode, pLastPtr-sOriginal, Word, sPrimary, sSecondary );
}
if ( !pWord[0] || sPrimary [0] )
strcpy ( (char*)pWord, (char*)sPrimary ); // NOLINT
// TODO: handle secondary too
}
//
// $Id: sphinxmetaphone.cpp 4885 2015-01-20 07:02:07Z deogar $
//