-
Notifications
You must be signed in to change notification settings - Fork 9
/
main-norvig.cc
262 lines (236 loc) · 7.83 KB
/
main-norvig.cc
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
/*
Copyright 2009 University of Helsinki
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
/*
This is a toy commandline utility for testing spellers on standard io.
*/
#include "ospell.h"
#include <getopt.h>
#include <cassert>
#include <math.h>
#define PACKAGE_NAME "hfst-ospell"
#define PACKAGE_STRING "hfst-ospell 0.1"
#define PACKAGE_BUGREPORT "hfst-bugs@ling.helsinki.fi"
bool print_usage(void)
{
std::cerr <<
"\n" <<
"Usage: " << PACKAGE_NAME << " [OPTIONS] ERRORSOURCE LEXICON\n" <<
"Run a composition of ERRORSOURCE and LEXICON on standard input and\n" <<
"print corrected output\n" <<
"\n" <<
" -h, --help Print this help message\n" <<
" -V, --version Print version information\n" <<
" -v, --verbose Be verbose\n" <<
" -q, --quiet Don't be verbose (default)\n" <<
" -s, --silent Same as quiet\n" <<
"\n" <<
"\n" <<
"Report bugs to " << PACKAGE_BUGREPORT << "\n" <<
"\n";
return true;
}
bool print_version(void)
{
std::cerr <<
"\n" <<
PACKAGE_STRING << std::endl <<
"copyright (C) 2009 University of Helsinki\n";
return true;
}
bool print_short_help(void)
{
print_usage();
return true;
}
int main(int argc, char **argv)
{
FILE * mutator_file = NULL;
FILE * lexicon_file = NULL;
int c;
bool verbose = false;
while (true)
{
static struct option long_options[] =
{
// first the hfst-mandated options
{"help", no_argument, 0, 'h'},
{"version", no_argument, 0, 'V'},
{"verbose", no_argument, 0, 'v'},
{"quiet", no_argument, 0, 'q'},
{"silent", no_argument, 0, 's'},
{0, 0, 0, 0 }
};
int option_index = 0;
c = getopt_long(argc, argv, "hVvqs", long_options, &option_index);
if (c == -1) // no more options to look at
break;
switch (c) {
case 'h':
print_usage();
return EXIT_SUCCESS;
break;
case 'V':
print_version();
return EXIT_SUCCESS;
break;
case 'v':
verbose = true;
break;
case 'q': // fallthrough
case 's':
break;
default:
std::cerr << "Invalid option\n\n";
print_short_help();
return EXIT_FAILURE;
break;
}
}
// no more options, we should now be at the input filenames
if ( (optind + 2) < argc) {
std::cerr << "More than two input files given\n";
return EXIT_FAILURE;
} else if ( (optind + 2) > argc)
{
std::cerr << "Need two input files\n";
return EXIT_FAILURE;
} else {
mutator_file = fopen(argv[(optind)], "r");
if (mutator_file == NULL) {
std::cerr << "Could not open file " << argv[(optind)]
<< std::endl;
return 1;
}
lexicon_file = fopen(argv[(optind + 1)], "r");
if (lexicon_file == NULL) {
std::cerr << "Could not open file " << argv[(optind + 1)]
<< std::endl;
return 1;
}
}
hfst_ospell::Transducer * mutator;
hfst_ospell::Transducer * lexicon;
mutator = new hfst_ospell::Transducer(mutator_file);
if (!mutator->is_weighted()) {
std::cerr << "Error source was unweighted, exiting\n\n";
return EXIT_FAILURE;
}
lexicon = new hfst_ospell::Transducer(lexicon_file);
if (!lexicon->is_weighted()) {
std::cerr << "Lexicon was unweighted, exiting\n\n";
return EXIT_FAILURE;
}
hfst_ospell::Speller * speller;
try {
speller = new hfst_ospell::Speller(mutator, lexicon);
} catch (hfst_ospell::AlphabetTranslationException& e) {
std::cerr <<
"Unable to build speller - symbol " << e.what() << " not "
"present in lexicon's alphabet\n";
return EXIT_FAILURE;
}
char * str = (char*) malloc(2000);
// def spelltest(tests, bias=None, verbose=False):
// n, bad, unknown, start = 0, 0, 0, time.clock()
unsigned long n = 0;
unsigned long bad = 0;
unsigned long unknown = 0;
clock_t start = clock();
// if bias:
// for target in tests: NWORDS[target] += bias
// for target,wrongs in tests.items():
// for wrong in wrongs.split():
while (!std::cin.eof()) {
std::cin.getline(str, 2000);
if (str[0] == '\0') {
continue;
}
// n += 1
n++;
char* p = strdup(str);
char* tok = strtok(p, "\t");
assert(tok != NULL);
char* mispelt = strdup(tok);
tok = strtok(NULL, "\t");
assert(tok != NULL);
//w = correct(wrong)
char* corr = strdup(tok);
// unknown += (corr in NWORDS)
if (!speller->check(corr))
{
unknown++;
}
if (speller->check(mispelt))
{
// real word spelling error
bad++;
if (verbose)
{
fprintf(stdout, "correct(%s) => %s; expected %s\n",
mispelt, mispelt, corr);
}
}
else
{
hfst_ospell::CorrectionQueue corrections = speller->correct(mispelt);
if (corrections.size() == 0)
{
bad++;
if (verbose)
{
fprintf(stdout, "correct(%s) => %s; expected %s\n",
mispelt, mispelt, corr);
}
}
else
{
std::string first = corrections.top().first;
//if w!=target:
if (first != corr)
{
//bad += 1
bad++;
// if verbose:
// print 'correct(%r) => %r (%d); expected %r (%d)' % (
// wrong, w, NWORDS[w], target, NWORDS[target])
if (verbose)
{
fprintf(stdout, "correct(%s) => %s; "
"expected %s\n",
mispelt, first.c_str(),
corr);
}
} // first != corr
else
{
if (verbose)
{
fprintf(stdout, "correct(%s) => %s "
"as expected %s\n",
mispelt, first.c_str(),
corr);
}
}
} // corrections size != 0
} // word not in dictionary§
}
//return dict(bad=bad, n=n, bias=bias, pct=int(100. - 100.*bad/n),
// unknown=unknown, secs=int(time.clock()-start) )
int pct = (int)round(100.0f - 100.0f*(float)bad/(float)n);
float secs = (((float)clock()-(float)start)/(float)CLOCKS_PER_SEC);
fprintf(stdout,
"{'bad': %lu, 'bias': None, 'unknown': %lu, "
"'secs': %f, 'pct': %d, 'n': %lu}\n",
bad, unknown, secs, pct, n);
return EXIT_SUCCESS;
}