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
//
// GENERATED FILE
//
use super::*;
use f2rust_std::*;
pub const LBCELL: i32 = -5;
const BSIZE: i32 = 10;
//$Procedure BESTWD ( Perform a spell match against a set of words )
pub fn BESTWD(
WORD: &[u8],
KNOWN: CharArray,
CUTOFF: i32,
BEST: &mut [i32],
SCORES: &mut [i32],
MSSG: &mut [u8],
ctx: &mut Context,
) -> f2rust_std::Result<()> {
let KNOWN = DummyCharArray::new(KNOWN, None, LBCELL..);
let mut BEST = DummyArrayMut::new(BEST, LBCELL..);
let mut SCORES = DummyArrayMut::new(SCORES, LBCELL..);
let mut BSCORE = StackArray::<i32, 10>::new(1..=BSIZE);
let mut ITEM = StackArray::<i32, 10>::new(1..=BSIZE);
let mut HELP = StackArray::<i32, 10>::new(1..=BSIZE);
let mut USIZE: i32 = 0;
let mut NBEST: i32 = 0;
let mut NTH = [b' '; 80];
let mut NKNOWN: i32 = 0;
let mut CSCORE: i32 = 0;
let mut OSCORE: i32 = 0;
let mut MAXSC: i32 = 0;
let mut I: i32 = 0;
let mut J: i32 = 0;
let mut L: i32 = 0;
let mut LOC: i32 = 0;
let mut TRANS = [b' '; 16];
let mut HIT: bool = false;
let mut HITS: i32 = 0;
let mut CASE = [b' '; 32];
let mut MYWD = [b' '; 32 as usize];
let mut TRIES: i32 = 0;
let mut LENGTH: i32 = 0;
//
// SPICELIB functions
//
//
// Local variables
//
//
// First determine how many words we have to compare with
// and the amount of room for reporting indices of "good"
// matches.
//
NKNOWN = spicelib::CARDC(KNOWN.as_arg(), ctx)?;
NBEST = spicelib::SIZEI(BEST.as_slice(), ctx)?;
//
// This routine only works on words of 32 or fewer characters
//
fstr::assign(&mut MYWD, b" ");
fstr::assign(&mut MYWD, WORD);
//
// USIZE refers to the amount of space we will actually
// use in the buffers that store the best MATCHC scores and
// the associated KNOWN word.
//
USIZE = intrinsics::MIN0(&[BSIZE, NKNOWN, NBEST]);
{
let m1__: i32 = 1;
let m2__: i32 = USIZE;
let m3__: i32 = 1;
I = m1__;
for _ in 0..((m2__ - m1__ + m3__) / m3__) as i32 {
BSCORE[I] = 0;
ITEM[I] = 0;
HELP[I] = 0;
SCORES[I] = 0;
I += m3__;
}
}
//
// First apply MATCHC against each of the KNOWNs and keep the
// top USIZE words that match.
//
{
let m1__: i32 = 1;
let m2__: i32 = NKNOWN;
let m3__: i32 = 1;
I = m1__;
for _ in 0..((m2__ - m1__ + m3__) / m3__) as i32 {
//
// Just in case, see if we have an exact match.
//
if spicelib::EQSTR(&MYWD, &KNOWN[I]) {
spicelib::SCARDI(1, BEST.as_slice_mut(), ctx)?;
spicelib::SCARDI(1, SCORES.as_slice_mut(), ctx)?;
BEST[1] = I;
SCORES[1] = 1000;
spicelib::INTORD(I, &mut NTH, ctx);
spicelib::LCASE(&NTH.clone(), &mut NTH, ctx);
fstr::assign(MSSG, &MYWD);
spicelib::SUFFIX(b"is equal to the ", 1, MSSG);
spicelib::SUFFIX(&NTH, 1, MSSG);
spicelib::SUFFIX(b" known word.", 1, MSSG);
return Ok(());
}
CSCORE = MATCHC(&MYWD, &KNOWN[I], ctx);
J = spicelib::LSTLTI(CSCORE, USIZE, BSCORE.as_slice());
for K in 1..=(J - 1) {
BSCORE[K] = BSCORE[(K + 1)];
ITEM[K] = ITEM[(K + 1)];
}
if (J > 0) {
BSCORE[J] = CSCORE;
ITEM[J] = I;
}
I += m3__;
}
}
//
// Now for the top USIZE matches, perform a MATCHO comparison.
// If we get a match of CUTOFF or higher. Run MATCHE against it
// to see if we can guess at what went wrong.
//
// So far our best score is 0 and we haven't HIT any good matches.
//
MAXSC = 0;
HITS = 0;
{
let m1__: i32 = 1;
let m2__: i32 = USIZE;
let m3__: i32 = 1;
I = m1__;
for _ in 0..((m2__ - m1__ + m3__) / m3__) as i32 {
//
// Only examine items that have legitimate indices.
//
if (ITEM[I] != 0) {
BSCORE[I] = MATCHO(&MYWD, &KNOWN[ITEM[I]], ctx);
CSCORE = MATCHC(&MYWD, &KNOWN[ITEM[I]], ctx);
MAXSC = intrinsics::MAX0(&[BSCORE[I], MAXSC]);
if ((BSCORE[I] >= CUTOFF) && (CSCORE >= CUTOFF)) {
//
// We've HIT a good match.
//
HITS = (HITS + 1);
//
// See if the problem with this word can be diagnosed
// with MATCHE.
//
MATCHE(&MYWD, &KNOWN[ITEM[I]], &mut TRANS, &mut LOC, ctx)?;
//
// If a diagnosis can be performed on this item, we
// say that HELP is available at level 2. Otherwise
// since we have a good match anyway we say HELP is
// available at level 1.
//
if fstr::ne(&TRANS, b"NONE") {
HELP[I] = 2;
} else {
HELP[I] = 1;
}
}
}
I += m3__;
}
}
//
// If none of the words had a sufficiently high score, just
// report those that had the maximum score.
//
if (HITS == 0) {
//
// Just report the item(s) that had the biggest score.
//
// First see how many had the maximum score.
//
{
let m1__: i32 = 1;
let m2__: i32 = USIZE;
let m3__: i32 = 1;
I = m1__;
for _ in 0..((m2__ - m1__ + m3__) / m3__) as i32 {
if (ITEM[I] != 0) {
if (BSCORE[I] == MAXSC) {
HITS = (HITS + 1);
}
}
I += m3__;
}
}
//
// If there were no KNOWN words that had letters in common
// with MYWD, all of the elements of the array ITEM will be
// zero and we will not have made any HITS against MAXSC.
// There is nothing at all we can do in this case.
//
if (HITS == 0) {
fstr::assign(MSSG, b"The word");
spicelib::SUFFIX(&MYWD, 1, MSSG);
spicelib::SUFFIX(b"has nothing in common with any of", 1, MSSG);
spicelib::SUFFIX(b"the words I can recognize. If ", 1, MSSG);
spicelib::SUFFIX(b"this word was typed interactively,", 1, MSSG);
spicelib::SUFFIX(b"you may want to see if your ", 1, MSSG);
spicelib::SUFFIX(b"fingers are over the correct keys.", 1, MSSG);
spicelib::SCARDI(0, BEST.as_slice_mut(), ctx)?;
spicelib::SCARDI(0, SCORES.as_slice_mut(), ctx)?;
return Ok(());
}
//
// Still here. Then we have at least some item that has
// something in common with MYWD. Set up a closing string so
// that grammar will be correct.
//
if (HITS > 1) {
fstr::assign(&mut CASE, b"my closest matches are: ");
} else {
fstr::assign(&mut CASE, b"my closest match is: ");
}
fstr::assign(MSSG, b"The word \'");
spicelib::SUFFIX(&MYWD, 1, MSSG);
spicelib::SUFFIX(b"\' did not match up well with any of", 1, MSSG);
spicelib::SUFFIX(b"the words I was comparing against.", 1, MSSG);
spicelib::SUFFIX(b"However,", 1, MSSG);
spicelib::SUFFIX(&CASE, 1, MSSG);
//
// Now append the list of KNOWN words that matched MYWD with
// the highest score.
//
HIT = false;
J = 0;
{
let m1__: i32 = 1;
let m2__: i32 = USIZE;
let m3__: i32 = 1;
I = m1__;
for _ in 0..((m2__ - m1__ + m3__) / m3__) as i32 {
if (ITEM[I] == 0) {
//
// don't do anything
//
} else if (BSCORE[I] == MAXSC) {
J = (J + 1);
BEST[J] = ITEM[I];
L = QRTRIM(&KNOWN[ITEM[I]]);
if HIT {
spicelib::SUFFIX(b", \'", 0, MSSG);
spicelib::SUFFIX(fstr::substr(&KNOWN[ITEM[I]], 1..=L), 0, MSSG);
spicelib::SUFFIX(b"\'", 0, MSSG);
} else {
HIT = true;
spicelib::SUFFIX(b"\'", 1, MSSG);
spicelib::SUFFIX(fstr::substr(&KNOWN[ITEM[I]], 1..=L), 0, MSSG);
spicelib::SUFFIX(b"\'", 0, MSSG);
}
spicelib::SUFFIX(b".", 0, MSSG);
}
I += m3__;
}
}
//
// Set the cardinality of the window of BEST indices.
//
spicelib::SCARDI(J, BEST.as_slice_mut(), ctx)?;
} else if (HITS == 1) {
//
// There was just one KNOWN word for which there was a good
// match. Call MSPELD to produce a diagnosis of the problem
// and record the index of the item.
//
I = 1;
while (HELP[I] == 0) {
I = (I + 1);
}
MSPELD(&MYWD, &KNOWN[ITEM[I]], MSSG, ctx)?;
BEST[1] = ITEM[I];
spicelib::SCARDI(1, BEST.as_slice_mut(), ctx)?;
} else {
//
// There were at least two "good" words. If any of them
// could be diagnosed, then report them. Otherwise
// report only those that had a maximum MATCHO score.
//
TRIES = 0;
{
let m1__: i32 = 1;
let m2__: i32 = 5;
let m3__: i32 = 1;
I = m1__;
for _ in 0..((m2__ - m1__ + m3__) / m3__) as i32 {
if (HELP[I] == 2) {
TRIES = (TRIES + 1);
}
I += m3__;
}
}
if (TRIES == 0) {
//
// None of the KNOWN words had diagnostics available.
//
fstr::assign(MSSG, b"Although a the spelling error can\'t be described in a simple way, I have found the following words that may be what you were trying to say. ");
J = 0;
{
let m1__: i32 = 1;
let m2__: i32 = USIZE;
let m3__: i32 = 1;
I = m1__;
for _ in 0..((m2__ - m1__ + m3__) / m3__) as i32 {
if (HELP[I] != 0) {
spicelib::SUFFIX(b"\'", 2, MSSG);
spicelib::SUFFIX(&KNOWN[ITEM[I]], 0, MSSG);
spicelib::SUFFIX(b"\',", 0, MSSG);
J = (J + 1);
BEST[J] = ITEM[I];
}
I += m3__;
}
}
spicelib::SCARDI(J, BEST.as_slice_mut(), ctx)?;
fstr::assign(fstr::substr_mut(MSSG, QRTRIM(MSSG)..=QRTRIM(MSSG)), b" ");
} else if (TRIES == 1) {
//
// Only one of the KNOWN words had diagnostics available.
//
{
let m1__: i32 = 1;
let m2__: i32 = 5;
let m3__: i32 = 1;
I = m1__;
for _ in 0..((m2__ - m1__ + m3__) / m3__) as i32 {
if (HELP[I] == 2) {
MSPELD(&MYWD, &KNOWN[ITEM[I]], MSSG, ctx)?;
BEST[1] = ITEM[I];
}
I += m3__;
}
}
spicelib::SCARDI(1, BEST.as_slice_mut(), ctx)?;
} else {
//
// At least two of the KNOWN words had diagnostics available.
// Report all of them.
//
fstr::assign(
MSSG,
b"The following common spelling mistakes may be the reason I did not recognize ",
);
spicelib::SUFFIX(&MYWD, 1, MSSG);
spicelib::SUFFIX(b".", 1, MSSG);
LENGTH = intrinsics::LEN(MSSG);
J = 0;
{
let m1__: i32 = 1;
let m2__: i32 = USIZE;
let m3__: i32 = 1;
I = m1__;
for _ in 0..((m2__ - m1__ + m3__) / m3__) as i32 {
if (HELP[I] == 2) {
if (QRTRIM(MSSG) < (LENGTH - 3)) {
MSPELD(
&MYWD,
&KNOWN[ITEM[I]],
fstr::substr_mut(MSSG, (QRTRIM(MSSG) + 3)..),
ctx,
)?;
J = (J + 1);
BEST[J] = ITEM[I];
}
}
I += m3__;
}
}
spicelib::SCARDI(J, BEST.as_slice_mut(), ctx)?;
}
}
//
// As for the scores, we will report the average of the MATCHO and
// MATCHC scores for the best matches.
//
{
let m1__: i32 = 1;
let m2__: i32 = spicelib::CARDI(BEST.as_slice(), ctx)?;
let m3__: i32 = 1;
I = m1__;
for _ in 0..((m2__ - m1__ + m3__) / m3__) as i32 {
OSCORE = MATCHO(&MYWD, &KNOWN[BEST[I]], ctx);
CSCORE = MATCHC(&MYWD, &KNOWN[BEST[I]], ctx);
SCORES[I] = ((OSCORE + CSCORE) / 2);
if ((OSCORE < CUTOFF) || (CSCORE < CUTOFF)) {
SCORES[I] = intrinsics::MIN0(&[SCORES[I], (CUTOFF - 1)]);
}
I += m3__;
}
}
spicelib::SCARDI(
spicelib::CARDI(BEST.as_slice(), ctx)?,
SCORES.as_slice_mut(),
ctx,
)?;
Ok(())
}