metaphone_c.c

filter: Statementstmt 80.7% 134 / 166 Branchbran 49.3% 81 / 164 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 65.1% 215 / 330 SCAR 0
CC
0
Cov
0%
CRAP 0
↑ Summary Metaphone.xs »
1 /* STUFF TO DO
2 * - Make max_phonemes useful
3 */
4
5 #include "metaphone.h"
6 #include "metachar.h"
7  
8 #include <stdlib.h>
9 #include <string.h>
10 #include <ctype.h>
11  
12 /* I suppose I could have been using a character pointer instead of
13 * accesssing the array directly... */
14  
15 /* Look at the next letter in the word */
16 #define Next_Letter (toupper(word[w_idx+1]))
17 /* Look at the current letter in the word */
18 #define Curr_Letter (toupper(word[w_idx]))
19 /* Go N letters back. */
20 #define Look_Back_Letter(n) (w_idx >= n ? toupper(word[w_idx-n]) : '\0')
21 /* Previous letter. I dunno, should this return null on failure? */
22 #define Prev_Letter (Look_Back_Letter(1))
23 /* Look two letters down. It makes sure you don't walk off the string. */
24 #define After_Next_Letter (Next_Letter != '\0' ? toupper(word[w_idx+2]) \
25 : '\0')
26 #define Look_Ahead_Letter(n) (toupper(Lookahead(word+w_idx, n)))
27  
28
29 /* Allows us to safely look ahead an arbitrary # of letters */
30 /* I probably could have just used strlen... */
31 5 char Lookahead(char * word, int how_far) {
32 5 char letter_ahead = '\0'; /* null by default */
33 int idx;
34 17 ▶ for(idx = 0; word[idx] != '\0' && idx < how_far; idx++);
Branch: 2 branches
BranchTrueFalseTotal
for(idx = 0; word[idx] != '\0' && idx < how_far; idx++); 17 0 17
for(idx = 0; word[idx] != '\0' && idx < how_far; idx++); 12 5 17
35 /* Edge forward in the string... */
36
37 5 letter_ahead = word[idx]; /* idx will be either == to how_far or
38 * at the end of the string
39 */
40 5 return letter_ahead;
41 }
42  
43  
44 /* phonize one letter */
45 #define Phonize(c) {phoned_word[p_idx++] = c;}
46 /* How long is the phoned word? */
47 #define Phone_Len (p_idx)
48  
49 /* Note is a letter is a 'break' in the word */
50 #define Isbreak(c) (!isalpha(c))
51  
52  
53 21 char *metaphone ( char *word, size_t max_phonemes ) {
54 21 int w_idx = 0; /* point in the phonization we're at. */
55 21 int p_idx = 0; /* end of the phoned phrase */
56 char *phoned_word;
57
58 /* Assume largest possible if we're given no limit */
59 21 ▶ if( max_phonemes == 0 )
Branch: 1 branch
BranchTrueFalseTotal
if( max_phonemes == 0 ) 21 0 21
60 21 max_phonemes = strlen(word);
61  
62 /* It's +2 because X -> KS can result in the phoned word being
63 one larger than the original word.
64 */
65 21 phoned_word = calloc( max_phonemes + 2, sizeof(char) );
66  
67 /*-- The first phoneme has to be processed specially. --*/
68 /* Find our first letter */
69 21 ▶ for( ; !isalpha(Curr_Letter); w_idx++ ) {
Branch: 1 branch
BranchTrueFalseTotal
for( ; !isalpha(Curr_Letter); w_idx++ ) { 1 20 21
70 /* On the off chance we were given nothing but crap... */
71 1 ▶ if( Curr_Letter == '\0' ) {
Branch: 1 branch
BranchTrueFalseTotal
if( Curr_Letter == '\0' ) { 1 0 1
72 1 return phoned_word;
73 }
74 }
75
76 20 switch (Curr_Letter) {
77 /* AE becomes E */
78 3 case 'A':
79 3 ▶ if( Next_Letter == 'E' ) {
Branch: 1 branch
BranchTrueFalseTotal
if( Next_Letter == 'E' ) { 1 2 3
80 1 Phonize('E');
81 1 w_idx+=2;
82 }
83 /* Remember, preserve vowels at the beginning */
84 else {
85 2 Phonize('A');
86 2 w_idx++;
87 }
88 3 break;
89 /* [GKP]N becomes N */
90 2 case 'G':
91 case 'K':
92 case 'P':
93 2 ▶ if( Next_Letter == 'N' ) {
Branch: 1 branch
BranchTrueFalseTotal
if( Next_Letter == 'N' ) { 0 2 2
94 0 Phonize('N');
95 0 w_idx+=2;
96 }
97 2 break;
98 /* WH becomes H,
99 WR becomes R
100 W if followed by a vowel */
101 3 case 'W':
102 3 ▶ if( Next_Letter == 'H' ||
Branch: 1 branch
BranchTrueFalseTotal
if( Next_Letter == 'H' || 2 1 3
103 2 ▶ Next_Letter == 'R' )
Branch: 1 branch
BranchTrueFalseTotal
Next_Letter == 'R' ) 0 2 2
104 {
105 1 Phonize(Next_Letter);
106 1 w_idx+=2;
107 }
108 2 ▶ else if ( isvowel(Next_Letter) ) {
Branch: 2 branches
BranchTrueFalseTotal
else if ( isvowel(Next_Letter) ) { 2 0 2
else if ( isvowel(Next_Letter) ) { 2 0 2
109 2 Phonize('W');
110 2 w_idx+=2;
111 }
112 /* else ignore */
113 3 break;
114 /* X becomes S */
115 0 case 'X':
116 0 Phonize('S');
117 0 w_idx++;
118 0 break;
119 /* Vowels are kept */
120 /* We did A already
121 case 'A':
122 case 'a':
123 */
124 0 case 'E':
125 case 'I':
126 case 'O':
127 case 'U':
128 0 Phonize(Curr_Letter);
129 0 w_idx++;
130 0 break;
131 12 default:
132 /* do nothing */
133 12 break;
134 }
135
136
137
138 /* On to the metaphoning */
139 20 for(;
140 168 ▶ Curr_Letter != '\0' &&
Branch: 2 branches
BranchTrueFalseTotal
Curr_Letter != '\0' && 149 19 168
Curr_Letter != '\0' && 0 149 149
141 149 ▶ (max_phonemes == 0 || Phone_Len < max_phonemes);
Branch: 1 branch
BranchTrueFalseTotal
(max_phonemes == 0 || Phone_Len < max_phonemes); 148 1 149
142 148 w_idx++
143 ) {
144 /* How many letters to skip because an eariler encoding handled
145 * multiple letters */
146 148 unsigned short int skip_letter = 0;
147
148  
149 /* THOUGHT: It would be nice if, rather than having things like...
150 * well, SCI. For SCI you encode the S, then have to remember
151 * to skip the C. So the phonome SCI invades both S and C. It would
152 * be better, IMHO, to skip the C from the S part of the encoding.
153 * Hell, I'm trying it.
154 */
155
156 /* Ignore non-alphas */
157 148 ▶ if( !isalpha(Curr_Letter) )
Branch: 1 branch
BranchTrueFalseTotal
if( !isalpha(Curr_Letter) ) 2 146 148
158 2 continue;
159
160 /* Drop duplicates, except CC */
161 146 ▶ if( Curr_Letter == Prev_Letter &&
Branch: 2 branches
BranchTrueFalseTotal
if( Curr_Letter == Prev_Letter && 132 14 146
if( Curr_Letter == Prev_Letter && 4 142 146
162 4 ▶ Curr_Letter != 'C' )
Branch: 1 branch
BranchTrueFalseTotal
Curr_Letter != 'C' ) 4 0 4
163 4 continue;
164
165 142 switch (Curr_Letter) {
166 /* B -> B unless in MB */
167 4 case 'B':
168 4 ▶ if( Prev_Letter != 'M' )
Branch: 2 branches
BranchTrueFalseTotal
if( Prev_Letter != 'M' ) 4 0 4
if( Prev_Letter != 'M' ) 4 0 4
169 4 Phonize('B');
170 4 break;
171 /* 'sh' if -CIA- or -CH, but not SCH, except SCHW.
172 * (SCHW is handled in S)
173 * S if -CI-, -CE- or -CY-
174 * dropped if -SCI-, SCE-, -SCY- (handed in S)
175 * else K
176 */
177 11 case 'C':
178 11 ▶ if( MAKESOFT(Next_Letter) ) { /* C[IEY] */
Branch: 2 branches
BranchTrueFalseTotal
if( MAKESOFT(Next_Letter) ) { /* C[IEY] */ 11 0 11
if( MAKESOFT(Next_Letter) ) { /* C[IEY] */ 4 7 11
179 8 ▶ if( After_Next_Letter == 'A' &&
Branch: 2 branches
BranchTrueFalseTotal
if( After_Next_Letter == 'A' && 4 0 4
if( After_Next_Letter == 'A' && 0 4 4
180 0 ▶ Next_Letter == 'I' ) { /* CIA */
Branch: 1 branch
BranchTrueFalseTotal
Next_Letter == 'I' ) { /* CIA */ 0 0 0
181 0 Phonize(SH);
182 }
183 /* SC[IEY] */
184 4 ▶ else if ( Prev_Letter == 'S' ) {
Branch: 2 branches
BranchTrueFalseTotal
else if ( Prev_Letter == 'S' ) { 4 0 4
else if ( Prev_Letter == 'S' ) { 1 3 4
185 /* Dropped */
186 }
187 else {
188 3 Phonize('S');
189 }
190 }
191 7 ▶ else if ( Next_Letter == 'H' ) {
Branch: 1 branch
BranchTrueFalseTotal
else if ( Next_Letter == 'H' ) { 1 6 7
192 #ifndef USE_TRADITIONAL_METAPHONE
193 1 ▶ if( After_Next_Letter == 'R' ||
Branch: 3 branches
BranchTrueFalseTotal
if( After_Next_Letter == 'R' || 1 0 1
if( After_Next_Letter == 'R' || 0 1 1
if( After_Next_Letter == 'R' || 0 0 0
194 0 ▶ Prev_Letter == 'S' ) { /* Christ, School */
Branch: 1 branch
BranchTrueFalseTotal
Prev_Letter == 'S' ) { /* Christ, School */ 0 0 0
195 1 Phonize('K');
196 }
197 else {
198 0 Phonize(SH);
199 }
200 #else
201 Phonize(SH);
202 #endif
203 1 skip_letter++;
204 }
205 else {
206 6 Phonize('K');
207 }
208 11 break;
209 /* J if in -DGE-, -DGI- or -DGY-
210 * else T
211 */
212 7 case 'D':
213 7 ▶ if( Next_Letter == 'G' &&
Branch: 1 branch
BranchTrueFalseTotal
if( Next_Letter == 'G' && 0 7 7
214 0 ▶ MAKESOFT(After_Next_Letter) ) {
Branch: 4 branches
BranchTrueFalseTotal
MAKESOFT(After_Next_Letter) ) { 0 0 0
MAKESOFT(After_Next_Letter) ) { 0 0 0
MAKESOFT(After_Next_Letter) ) { 0 0 0
MAKESOFT(After_Next_Letter) ) { 0 0 0
215 0 Phonize('J');
216 0 skip_letter++;
217 }
218 else
219 7 Phonize('T');
220 7 break;
221 /* F if in -GH and not B--GH, D--GH, -H--GH, -H---GH
222 * else dropped if -GNED, -GN,
223 * else dropped if -DGE-, -DGI- or -DGY- (handled in D)
224 * else J if in -GE-, -GI, -GY and not GG
225 * else K
226 */
227 6 case 'G':
228 6 ▶ if( Next_Letter == 'H' ) {
Branch: 1 branch
BranchTrueFalseTotal
if( Next_Letter == 'H' ) { 2 4 6
229 2 ▶ if( !( NOGHTOF(Look_Back_Letter(3)) ||
Branch: 5 branches
BranchTrueFalseTotal
if( !( NOGHTOF(Look_Back_Letter(3)) || 2 0 2
if( !( NOGHTOF(Look_Back_Letter(3)) || 2 0 2
if( !( NOGHTOF(Look_Back_Letter(3)) || 2 0 2
if( !( NOGHTOF(Look_Back_Letter(3)) || 1 1 2
if( !( NOGHTOF(Look_Back_Letter(3)) || 0 1 1
230 0 ▶ Look_Back_Letter(4) == 'H' ) ) {
Branch: 1 branch
BranchTrueFalseTotal
Look_Back_Letter(4) == 'H' ) ) { 0 0 0
231 1 Phonize('F');
232 1 skip_letter++;
233 }
234 else {
235 /* silent */
236 }
237 }
238 4 ▶ else if( Next_Letter == 'N' ) {
Branch: 1 branch
BranchTrueFalseTotal
else if( Next_Letter == 'N' ) { 0 4 4
239 0 ▶ if( Isbreak(After_Next_Letter) ||
Branch: 2 branches
BranchTrueFalseTotal
if( Isbreak(After_Next_Letter) || 0 0 0
if( Isbreak(After_Next_Letter) || 0 0 0
240 0 ▶ ( After_Next_Letter == 'E' &&
Branch: 2 branches
BranchTrueFalseTotal
( After_Next_Letter == 'E' && 0 0 0
( After_Next_Letter == 'E' && 0 0 0
241 0 ▶ Look_Ahead_Letter(3) == 'D' ) ) {
Branch: 1 branch
BranchTrueFalseTotal
Look_Ahead_Letter(3) == 'D' ) ) { 0 0 0
242 /* dropped */
243 }
244 else
245 0 Phonize('K');
246 }
247 4 ▶ else if( MAKESOFT(Next_Letter) &&
Branch: 3 branches
BranchTrueFalseTotal
else if( MAKESOFT(Next_Letter) && 3 1 4
else if( MAKESOFT(Next_Letter) && 0 3 3
else if( MAKESOFT(Next_Letter) && 0 0 0
248 0 ▶ Prev_Letter != 'G' ) {
Branch: 1 branch
BranchTrueFalseTotal
Prev_Letter != 'G' ) { 0 0 0
249 0 Phonize('J');
250 }
251 else {
252 4 Phonize('K');
253 }
254 6 break;
255 /* H if before a vowel and not after C,G,P,S,T */
256 2 case 'H':
257 2 ▶ if( isvowel(Next_Letter) &&
Branch: 2 branches
BranchTrueFalseTotal
if( isvowel(Next_Letter) && 2 0 2
if( isvowel(Next_Letter) && 1 1 2
258 1 ▶ !AFFECTH(Prev_Letter) )
Branch: 4 branches
BranchTrueFalseTotal
!AFFECTH(Prev_Letter) ) 1 0 1
!AFFECTH(Prev_Letter) ) 1 0 1
!AFFECTH(Prev_Letter) ) 1 0 1
!AFFECTH(Prev_Letter) ) 1 0 1
259 1 Phonize('H');
260 2 break;
261 /* dropped if after C
262 * else K
263 */
264 2 case 'K':
265 2 ▶ if( Prev_Letter != 'C' )
Branch: 2 branches
BranchTrueFalseTotal
if( Prev_Letter != 'C' ) 2 0 2
if( Prev_Letter != 'C' ) 1 1 2
266 1 Phonize('K');
267 2 break;
268 /* F if before H
269 * else P
270 */
271 3 case 'P':
272 3 ▶ if( Next_Letter == 'H' ) {
Branch: 1 branch
BranchTrueFalseTotal
if( Next_Letter == 'H' ) { 0 3 3
273 0 Phonize('F');
274 }
275 else {
276 3 Phonize('P');
277 }
278 3 break;
279 /* K
280 */
281 2 case 'Q':
282 2 Phonize('K');
283 2 break;
284 /* 'sh' in -SH-, -SIO- or -SIA- or -SCHW-
285 * else S
286 */
287 11 case 'S':
288 11 ▶ if( Next_Letter == 'I' &&
Branch: 1 branch
BranchTrueFalseTotal
if( Next_Letter == 'I' && 1 10 11
289 1 ▶ ( After_Next_Letter == 'O' ||
Branch: 2 branches
BranchTrueFalseTotal
( After_Next_Letter == 'O' || 1 0 1
( After_Next_Letter == 'O' || 0 1 1
290 0 ▶ After_Next_Letter == 'A' ) ) {
Branch: 2 branches
BranchTrueFalseTotal
After_Next_Letter == 'A' ) ) { 0 0 0
After_Next_Letter == 'A' ) ) { 0 0 0
291 1 Phonize(SH);
292 }
293 10 ▶ else if ( Next_Letter == 'H' ) {
Branch: 1 branch
BranchTrueFalseTotal
else if ( Next_Letter == 'H' ) { 1 9 10
294 1 Phonize(SH);
295 1 skip_letter++;
296 }
297 #ifndef USE_TRADITIONAL_METAPHONE
298 9 ▶ else if ( Next_Letter == 'C' &&
Branch: 1 branch
BranchTrueFalseTotal
else if ( Next_Letter == 'C' && 3 6 9
299 3 ▶ Look_Ahead_Letter(2) == 'H' &&
Branch: 1 branch
BranchTrueFalseTotal
Look_Ahead_Letter(2) == 'H' && 2 1 3
300 2 ▶ Look_Ahead_Letter(3) == 'W' ) {
Branch: 1 branch
BranchTrueFalseTotal
Look_Ahead_Letter(3) == 'W' ) { 2 0 2
301 2 Phonize(SH);
302 2 skip_letter += 2;
303 }
304 #endif
305 else {
306 7 Phonize('S');
307 }
308 11 break;
309 /* 'sh' in -TIA- or -TIO-
310 * else 'th' before H
311 * else T
312 */
313 7 case 'T':
314 7 ▶ if( Next_Letter == 'I' &&
Branch: 1 branch
BranchTrueFalseTotal
if( Next_Letter == 'I' && 0 7 7
315 0 ▶ ( After_Next_Letter == 'O' ||
Branch: 2 branches
BranchTrueFalseTotal
( After_Next_Letter == 'O' || 0 0 0
( After_Next_Letter == 'O' || 0 0 0
316 0 ▶ After_Next_Letter == 'A' ) ) {
Branch: 2 branches
BranchTrueFalseTotal
After_Next_Letter == 'A' ) ) { 0 0 0
After_Next_Letter == 'A' ) ) { 0 0 0
317 0 Phonize(SH);
318 }
319 7 ▶ else if ( Next_Letter == 'H' ) {
Branch: 1 branch
BranchTrueFalseTotal
else if ( Next_Letter == 'H' ) { 0 7 7
320 0 Phonize(TH);
321 0 skip_letter++;
322 }
323 else {
324 7 Phonize('T');
325 }
326 7 break;
327 /* F */
328 4 case 'V':
329 4 Phonize('F');
330 4 break;
331 /* W before a vowel, else dropped */
332 2 case 'W':
333 2 ▶ if( isvowel(Next_Letter) )
Branch: 2 branches
BranchTrueFalseTotal
if( isvowel(Next_Letter) ) 2 0 2
if( isvowel(Next_Letter) ) 2 0 2
334 2 Phonize('W');
335 2 break;
336 /* KS */
337 8 case 'X':
338 8 Phonize('K');
339 8 Phonize('S');
340 8 break;
341 /* Y if followed by a vowel */
342 1 case 'Y':
343 1 ▶ if( isvowel(Next_Letter) )
Branch: 2 branches
BranchTrueFalseTotal
if( isvowel(Next_Letter) ) 0 1 1
if( isvowel(Next_Letter) ) 0 0 0
344 0 Phonize('Y');
345 1 break;
346 /* S */
347 1 case 'Z':
348 1 Phonize('S');
349 1 break;
350 /* No transformation */
351 32 case 'F':
352 case 'J':
353 case 'L':
354 case 'M':
355 case 'N':
356 case 'R':
357 32 Phonize(Curr_Letter);
358 32 break;
359 39 default:
360 /* nothing */
361 39 break;
362 }
363
364 142 w_idx += skip_letter;
365 }
366
367 20 return phoned_word;
368 }
↑ Summary Metaphone.xs »