File: indword.c

package info (click to toggle)
ted 2.6-1
  • links: PTS
  • area: main
  • in suites: potato
  • size: 7,928 kB
  • ctags: 8,734
  • sloc: ansic: 71,878; makefile: 2,363; sh: 159
file content (245 lines) | stat: -rw-r--r-- 6,353 bytes parent folder | download
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
#   include	"config.h"

#   include	<ind.h>
#   include	"indlocal.h"
#   include	<charnames.h>
#   include	<debugon.h>

/************************************************************************/
/*  See whether a word is accepted.					*/
/*  As this is a utility routine for spelling checking, the following	*/
/*  attempts are made to accept it when it is not quite acceptable:	*/
/************************************************************************/

int indWRDget(	IND *			ind,
		int *			pWhatWasShifted,
		const unsigned char *	word,
		int			asPrefix,
		const unsigned char *	charKinds,
		const unsigned char *	charShifts )
    {
    static unsigned char *	copy;

    int				l;
    const unsigned char *	r;
    unsigned char *		s;
    int				accepted;
    int				tn;

    tn= indINDget( &accepted, ind, -1, word );
    if  ( tn >= 0 && ( accepted || asPrefix ) )
	{ *pWhatWasShifted= INDhASIS; return 0;	}

    l= strlen( (char *)word );
    copy= realloc( copy, l+ 2 );

    if  ( ! copy )
	{ LDEB(copy); return -1;	}

    strcpy( (char *)copy, (const char *)word );

    if  ( charKinds[ word[0] ] & CHARisUPPER )
	{
	if  ( word[0] == 'I' && word[1] == 'J' )
	    {
	    copy[0]= 'i'; copy[1]= 'j';
	    if  ( indINDget( &accepted, ind, -1, copy ) >= 0	&&
		  ( accepted || asPrefix )			)
		{ *pWhatWasShifted= INDhIJUP; return 0;	}
	    }
	else{
	    if  ( ! ( charKinds[ word[1] ] & CHARisUPPER ) )
		{
		copy[0]= charShifts[ word[0] ];
		if  ( indINDget( &accepted, ind, -1, copy ) >= 0	&&
		      ( accepted || asPrefix )			)
		    { *pWhatWasShifted= INDhFIRSTUP; return 0;	}
		}
	    }

	/************************************************/
	/*  Shift down.					*/
	/************************************************/
	r= word; s= copy;
	while( *r && ( charKinds[ *r ] & CHARisUPPER ) )
	    {
	    *s= charShifts[*r];
	    s++; r++;
	    }

	/************************************************/
	/*  Not completely in upper case		*/
	/************************************************/
	if  ( *r )
	    { return -1; }

	/************************************************/
	/*  Try shift down.				*/
	/************************************************/
	if  ( indINDget( &accepted, ind, -1, copy ) >= 0	&&
	      ( accepted || asPrefix )			)
	    { *pWhatWasShifted= INDhALLUP; return 0;	}

	/************************************************/
	/*  Try tail shifted down.			*/
	/************************************************/
	if  ( word[0] == 'I' && word[1] == 'J' )
	    {
	    copy[0]= word[0]; copy[1]= word[1];
	    if  ( indINDget( &accepted, ind, -1, copy ) >= 0	&&
		  ( accepted || asPrefix )			)
		{ *pWhatWasShifted= INDhIJTAILUP; return 0;	}
	    }
	else{
	    copy[0]= word[0];
	    if  ( indINDget( &accepted, ind, -1, copy ) >= 0	&&
		  ( accepted || asPrefix )			)
		{ *pWhatWasShifted= INDhTAILUP; return 0;	}
	    }
	}

    return -1;
    }

/************************************************************************/
/*  Generate guesses for a word.					*/
/************************************************************************/
int	indWRDguess(	IND *				ind,
			const unsigned char *		word,
			SpellGuessContext *		sgc,
			const GuessSubstitution *	typos,
			int				count,
			const unsigned char *		charKinds,
			const unsigned char *		charShifts )
    {
    int				l;
    static unsigned char *	copy;

    const unsigned char *	r;
    unsigned char *		s;

    l= strlen( (char *)word );
    copy= realloc( copy, l+ 2 );
    if  ( ! copy )
	{ LDEB(copy); return -1;	}
    strcpy( (char *)copy, (const char *)word );

    if  ( charKinds[ word[0] ] & CHARisUPPER )
	{
	if  ( word[0] == 'I' && word[1] == 'J' )
	    {
	    copy[0]= 'i'; copy[1]= 'j';
	    indINDguess( ind, copy, sgc, INDhIJUP,
				    typos, count, charKinds, charShifts );
	    }
	else{
	    if  ( ! ( charKinds[ word[1] ] & CHARisUPPER ) )
		{
		copy[0]= charShifts[ word[0] ];
		indINDguess( ind, copy, sgc, INDhFIRSTUP,
				    typos, count, charKinds, charShifts );
		}
	    }

	/************************************************/
	/*  Shift down.					*/
	/************************************************/
	r= word; s= copy;
	while( *r && ( charKinds[ *r ] & CHARisUPPER ) )
	    {
	    *s= charShifts[*r];
	    s++; r++;
	    }

	/************************************************/
	/*  Not completely in upper case		*/
	/************************************************/
	if  ( *r )
	    { return 0; }

	/************************************************/
	/*  Try shift down.				*/
	/************************************************/
	indINDguess( ind, copy, sgc, INDhALLUP,
				    typos, count, charKinds, charShifts );

	/************************************************/
	/*  Try tail shifted down.			*/
	/************************************************/
	if  ( word[0] == 'I' && word[1] == 'J' )
	    {
	    copy[0]= word[0]; copy[1]= word[1];
	    indINDguess( ind, copy, sgc, INDhIJTAILUP,
				    typos, count, charKinds, charShifts );
	    }
	else{
	    copy[0]= word[0];
	    indINDguess( ind, copy, sgc, INDhTAILUP,
				    typos, count, charKinds, charShifts );
	    }
	}
    else{
	indINDguess( ind, word, sgc, INDhASIS,
				    typos, count, charKinds, charShifts );
	}

    return 0;
    }

int	indShiftWord(	unsigned char *		copy,
			const unsigned char *	word,
			int			how,
			const unsigned char *	charKinds,
			const unsigned char *	charShifts )
    {
    int	i;

    strcpy( (char *)copy, (const char *)word );

    switch( how )
	{
	case	INDhASIS:
	    break;
	case	INDhFIRSTUP:
	    if  ( charKinds[ copy[0] ] & CHARisLOWER )
		{ copy[0]= charShifts[ copy[0] ]; }
	    break;
	case	INDhIJUP:
	    if  ( copy[0] == 'i' && copy[1] == 'j' )
		{ copy[0]= 'I'; copy[1]= 'J';	}
	    break;
	case	INDhALLUP:
	    i= 0;
	    while( copy[i] )
		{
		if  ( charKinds[ copy[i] ] & CHARisLOWER )
		    { copy[i]= charShifts[ copy[i] ]; }
		i++;
		}
	    break;
	case	INDhTAILUP:
	    i= 0;
	    while( copy[i] )
		{
		if  ( charKinds[ copy[i] ] & CHARisLOWER )
		    { copy[i]= charShifts[ copy[i] ]; }
		i++;
		}
	    break;
	case	INDhIJTAILUP:
	    if  ( copy[0] != 'I' || copy[1] != 'J' )
		{ SDEB((char *)copy); return -1;	}
	    i= 2;
	    while( copy[i] )
		{
		if  ( charKinds[ copy[i] ] & CHARisLOWER )
		    { copy[i]= charShifts[ copy[i] ]; }
		i++;
		}
	    break;
	default:
	    LDEB(how); return -1;
	}

    return 0;
    }