-
Notifications
You must be signed in to change notification settings - Fork 1
/
rspell.cpp
272 lines (229 loc) · 4.7 KB
/
rspell.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
// rspell
// (c) Copyright 2009, Recursive Pizza
#include <stdio.h>
#include <stdlib.h> // For getenv()
#include <string.h>
#include <ctype.h>
#include <string>
#include <map>
#include <list>
#include "dict.h"
#include "rspell.h"
#ifdef _MSC_VER
#pragma warning(disable: 4996) // stdlib
#endif
static void Usage()
{
fprintf(stderr, "Usage: rspell [options are ignored] < file\n");
exit(1);
}
//-----------------------------------------------------------------------------
// Suggestions
inline bool ceq(const char *a, const char *b)
{
#ifdef _MSC_VER
return _stricmp(a,b) == 0;
#else
return strcasecmp(a,b) == 0;
#endif
}
typedef std::string SPELL_WORD;
typedef enum { W_NOT_IN_DICT, W_THE_WORD, W_SWAP, W_DELETE, W_INSERT } WHERE;
typedef std::map<SPELL_WORD, int> WORD_MAP;
typedef std::list<SPELL_WORD> WORD_LIST;
static void Mark(WORD_MAP &a, const char *word, const WHERE where)
{
if (a.find(word) == a.end())
{
a[word] = where;
}
}
static void CheckSuggestions(const WORD_MAP &a, WORD_LIST &b)
{
const char *word;
b.clear();
for (WORD_MAP::const_iterator p = a.begin(); p != a.end(); p++)
{
word = (*p).first.c_str();
if (InDict(word))
{
b.push_back(word);
}
}
}
inline char *Shuffle(char *dest, const char *src)
{
return (char *) memmove(dest, src, strlen(src) + 1);
}
static char insert_chars[] = "-abcdefghijklmnopqrstuvwxyz";
static void Suggestions(const char *word_in, WORD_LIST &b)
{
WORD_MAP::iterator it;
WORD_MAP a;
int i;
int len, len1;
char buf[MAX_WORD];
char c;
int j;
const char *word;
int type;
// Add the word itself
Mark(a, word_in, W_THE_WORD);
// Swap adjacent
for (it = a.begin(); it != a.end(); it++)
{
type = it->second;
if (type == W_SWAP) continue; // Its from me
word = it->first.c_str();
len1 = (int)strlen(word) - 1;
for (i = 0; i < len1; i++)
{
strcpy(buf, word);
c = buf[i];
buf[i] = buf[i + 1];
buf[i + 1] = c;
Mark(a, buf, W_SWAP);
}
}
// Delete letters
for (it = a.begin(); it != a.end(); it++)
{
type = it->second;
if (type == W_DELETE) continue; // Its from me
word = it->first.c_str();
len = (int)strlen(word);
for (i = 0; i < len; i++)
{
strcpy(buf, word);
memmove(&buf[i], &buf[i + 1], len - i);
Mark(a, buf, W_DELETE);
}
}
// Insert
for (it = a.begin(); it != a.end(); it++)
{
type = it->second;
if (type == W_INSERT) continue; // Its from me
word = it->first.c_str();
len = (int)strlen(word);
for (i = 0; i <= len; i++)
{
strcpy(buf, word);
Shuffle(&buf[i + 1], &buf[i]);
for (j = 0; insert_chars[j]; j++)
{
buf[i] = insert_chars[j];
Mark(a, buf, W_INSERT);
}
}
}
// Don't want the original word as a suggestion any more
a.erase(word_in);
CheckSuggestions(a, b);
}
//--------------------------------------------------------------------------
// Reading input file
static bool IsWordChar(const char c, const int len)
{
if (isalnum(c)) return true;
if (c == '\'' && len > 1) return true;
return false;
}
static bool GetWord(const char *&pRead, char *wordOut, const size_t sizeWordOut, int &char_offset, int &word_offset)
{
char *pWord = wordOut;
int n = 0;
int c;
wordOut[0] = '\0';
// Skip leading non-word chars
for (;;)
{
if ((c = *pRead++) == '\0') return false;
char_offset++;
if (IsWordChar(c, n))
{
pRead--;
char_offset--;
break;
}
}
word_offset = char_offset;
for (;;)
{
if ((c = *pRead++) == '\0') break;
char_offset++;
if (!IsWordChar(c, n)) break;
*pWord++ = c;
n++;
if (n >= (int)sizeWordOut) break;
}
*pWord = '\0';
return true;
}
static void SpellLine(const char *line)
{
const char *pRead = line;
char word[MAX_WORD];
int char_offset = 0;
int word_offset = 0;
for (;;)
{
if (!GetWord(pRead, word, sizeof(word), char_offset, word_offset)) break;
if (InDict(word))
{
printf("*\n");
}
else
{
WORD_LIST b;
Suggestions(word, b);
if (b.size() == 0)
{
printf("# %s %d\n", word, word_offset);
}
else
{
bool bNeedComma = false;
printf("& %s %d %d: ", word, (int)b.size(), word_offset);
for (WORD_LIST::const_iterator p = b.begin(); p != b.end(); p++)
{
if (bNeedComma) printf(", ");
printf("%s", (*p).c_str());
bNeedComma = true;
}
printf("\n");
}
}
}
}
void Chomp(char *s)
{
char *p;
if ((p = strchr(s, '\n')) != NULL)
{
*p = '\0';
}
}
static void Spell(FILE *f)
{
char line[MAX_LINE];
for (;;)
{
if (fgets(line, sizeof(line), f) == NULL) break;
Chomp(line);
SpellLine(line);
printf("\n");
}
}
int main(int argc, char *argv[])
{
if (argc > 1) {
if (ceq(argv[1], "-?")) {
Usage();
return 0;
}
}
printf("@(#) International Ispell Version 3.1.20 (but really rspell .1)\n");
Spell(stdin);
exit(0);
}