-
Notifications
You must be signed in to change notification settings - Fork 9
/
hfst-ol.h
464 lines (400 loc) · 12.9 KB
/
hfst-ol.h
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
/* -*- Mode: C++ -*- */
// Copyright 2010 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 file contains some classes, typedefs and constant common to all
* hfst-optimized-lookup stuff. This is just to get them out of the way
* of the actual ospell code.
*/
#ifndef HFST_OSPELL_HFST_OL_H_
#define HFST_OSPELL_HFST_OL_H_
#include "hfstol-stdafx.h"
#include <vector>
#include <map>
#include <climits>
#include <cstdio>
#include <cstdlib>
#include <cstdint>
#include <iostream>
#include <cstring>
#include <set>
#include <utility>
#include "ol-exceptions.h"
namespace hfst_ospell {
typedef uint16_t SymbolNumber;
typedef uint32_t TransitionTableIndex;
typedef std::vector<SymbolNumber> SymbolVector;
typedef std::vector<std::string> KeyTable;
typedef std::map<std::string, SymbolNumber> StringSymbolMap;
typedef short ValueNumber;
typedef float Weight;
// Forward declarations to typedef some more containers
class TransitionIndex;
class Transition;
class FlagDiacriticOperation;
typedef std::vector<TransitionIndex*> TransitionIndexVector;
typedef std::vector<Transition*> TransitionVector;
typedef std::map<SymbolNumber, FlagDiacriticOperation> OperationMap;
const SymbolNumber NO_SYMBOL = USHRT_MAX;
const TransitionTableIndex NO_TABLE_INDEX = UINT_MAX;
const Weight INFINITE_WEIGHT = static_cast<float>(NO_TABLE_INDEX);
const unsigned int MAX_SYMBOL_BYTES = 1000;
// This is 2^31, hopefully equal to UINT_MAX/2 rounded up.
// For some profound reason it can't be replaced with (UINT_MAX+1)/2.
const TransitionTableIndex TARGET_TABLE = 2147483648u;
// the flag diacritic operators as given in
// Beesley & Karttunen, Finite State Morphology (U of C Press 2003)
enum FlagDiacriticOperator {P, N, R, D, C, U};
enum HeaderFlag {Weighted, Deterministic, Input_deterministic, Minimized,
Cyclic, Has_epsilon_epsilon_transitions,
Has_input_epsilon_transitions, Has_input_epsilon_cycles,
Has_unweighted_input_epsilon_cycles};
// Will probably turn into a compile-time constant
bool is_big_endian(void);
uint16_t read_uint16_flipping_endianness(FILE * f);
uint16_t read_uint16_flipping_endianness(char * raw);
uint32_t read_uint32_flipping_endianness(char * raw);
float read_float_flipping_endianness(FILE * f);
// Utility function for dealing with raw memory
void skip_c_string(char ** raw);
//! Internal class for Transducer processing.
//! Contains low-level processing stuff.
class TransducerHeader
{
private:
SymbolNumber number_of_symbols;
SymbolNumber number_of_input_symbols;
TransitionTableIndex size_of_transition_index_table;
TransitionTableIndex size_of_transition_target_table;
TransitionTableIndex number_of_states;
TransitionTableIndex number_of_transitions;
bool weighted;
bool deterministic;
bool input_deterministic;
bool minimized;
bool cyclic;
bool has_epsilon_epsilon_transitions;
bool has_input_epsilon_transitions;
bool has_input_epsilon_cycles;
bool has_unweighted_input_epsilon_cycles;
void read_property(bool &property, FILE * f);
void read_property(bool &property, char ** raw);
void skip_hfst3_header(FILE * f);
void skip_hfst3_header(char ** f);
public:
//!
//! @brief read header from file @a f
TransducerHeader(FILE * f);
//!
//! read header from raw memory data @a raw
TransducerHeader(char ** raw);
//!
//! count symbols
SymbolNumber symbol_count(void);
//!
//! count input symbols
SymbolNumber input_symbol_count(void);
//!
//! index table size
TransitionTableIndex index_table_size(void);
//!
//! target table size
TransitionTableIndex target_table_size(void);
//!
//! check for flag
bool probe_flag(HeaderFlag flag);
};
//! Internal class for flag diacritic processing.
//! Contains low-level processing stuff.
class FlagDiacriticOperation
{
private:
const FlagDiacriticOperator operation;
const SymbolNumber feature;
const ValueNumber value;
public:
//!
//! Construct flag diacritic of from \@ @a op . @a feat . @a val \@.
FlagDiacriticOperation(const FlagDiacriticOperator op,
const SymbolNumber feat,
const ValueNumber val):
operation(op), feature(feat), value(val) {}
// dummy constructor
FlagDiacriticOperation():
operation(P), feature(NO_SYMBOL), value(0) {}
//!
//! check if flag
bool isFlag(void) const;
//!
//! Operation something I don't understand really.
FlagDiacriticOperator Operation(void) const;
//!
//! No clue
SymbolNumber Feature(void) const;
//!
//! Not a slightest idea
ValueNumber Value(void) const;
};
//! Internal class for alphabet processing.
//! Contains low-level processing stuff.
class TransducerAlphabet
{
private:
KeyTable kt;
OperationMap operations;
SymbolNumber unknown_symbol;
SymbolNumber identity_symbol;
SymbolNumber flag_state_size;
SymbolNumber orig_symbol_count;
StringSymbolMap string_to_symbol;
void process_symbol(char * line);
void read(FILE * f, SymbolNumber number_of_symbols);
void read(char ** raw, SymbolNumber number_of_symbols);
public:
//!
//! read alphabets from file @a f
TransducerAlphabet(FILE *f, SymbolNumber number_of_symbols);
//!
//! read alphabes from raw data @a raw
TransducerAlphabet(char ** raw, SymbolNumber number_of_symbols);
void add_symbol(std::string & sym);
void add_symbol(char * sym);
//!
//! get alphabet's keytable mapping
KeyTable * get_key_table(void);
//!
//! get flag operation map stuff
OperationMap * get_operation_map(void);
//!
//! get state's size
SymbolNumber get_state_size(void);
//!
//! get position of unknown symbol
SymbolNumber get_unknown(void) const;
SymbolNumber get_identity(void) const;
//! get orig symbol count
SymbolNumber get_orig_symbol_count(void) const;
//!
//! get mapping from strings to symbols
StringSymbolMap * get_string_to_symbol(void);
bool has_string(std::string const & s) const;
//!
//! get if given symbol is a flag
bool is_flag(SymbolNumber symbol);
};
class LetterTrie;
typedef std::vector<LetterTrie*> LetterTrieVector;
//! Internal class for alphabet processing.
//! Contains low-level processing stuff.
class LetterTrie
{
private:
LetterTrieVector letters;
SymbolVector symbols;
public:
LetterTrie(void):
letters(UCHAR_MAX+1, static_cast<LetterTrie*>(NULL)),
symbols(UCHAR_MAX+1,NO_SYMBOL)
{}
//!
//! add a string to alphabets with a key
void add_string(const char * p,SymbolNumber symbol_key);
//!
//! find a key for string or add it
SymbolNumber find_key(char ** p);
bool has_key_starting_with(const char c) const;
~LetterTrie();
};
//! Internal class for alphabet processing.
//! Contains low-level processing stuff.
class Encoder {
private:
LetterTrie letters;
SymbolVector ascii_symbols;
void read_input_symbols(KeyTable * kt, SymbolNumber number_of_input_symbols);
public:
//!
//! create encoder from keytable
Encoder(KeyTable * kt, SymbolNumber number_of_input_symbols);
SymbolNumber find_key(char ** p);
void read_input_symbol(const char * s, const int s_num);
void read_input_symbol(std::string const & s, const int s_num);
};
typedef std::vector<ValueNumber> FlagDiacriticState;
//! Internal class for transition data.
//! Contains low-level processing stuff.
class TransitionIndex
{
protected:
SymbolNumber input_symbol; //!< transition's input symbol
TransitionTableIndex first_transition_index; //!< first transition location
public:
//!
//! Each TransitionIndex has an input symbol and a target index.
static const size_t SIZE =
sizeof(SymbolNumber) + sizeof(TransitionTableIndex);
//!
//! Create transition index for symbol
TransitionIndex(const SymbolNumber input,
const TransitionTableIndex first_transition):
input_symbol(input),
first_transition_index(first_transition)
{}
//!
//! return target of transition
TransitionTableIndex target(void) const;
//!
//! whether it's final state
bool final(void) const;
//!
//! retrieve final weight
Weight final_weight(void) const;
//!
//! symbol number for transitions input
SymbolNumber get_input(void) const;
};
//! Internal class for transition processing.
//! Contains low-level processing stuff.
class Transition
{
protected:
SymbolNumber input_symbol; //!< input symbol
SymbolNumber output_symbol; //!< output symbol
TransitionTableIndex target_index; //!< location of target of transition
Weight transition_weight; //!< tranisition's weight
public:
//! Each transition has an input symbol, an output symbol and
//! a target index.
static const size_t SIZE =
2 * sizeof(SymbolNumber) + sizeof(TransitionTableIndex) + sizeof(Weight);
//!
//! Create transition with input, output, target and weight.
Transition(const SymbolNumber input,
const SymbolNumber output,
const TransitionTableIndex target,
const Weight w):
input_symbol(input),
output_symbol(output),
target_index(target),
transition_weight(w)
{}
Transition():
input_symbol(NO_SYMBOL),
output_symbol(NO_SYMBOL),
target_index(NO_TABLE_INDEX),
transition_weight(INFINITE_WEIGHT)
{}
//!
//! get transitions target
TransitionTableIndex target(void) const;
//!
//! get output symbol
SymbolNumber get_output(void) const;
//!
//! get input symbol
SymbolNumber get_input(void) const;
//!
//! get transition weight
Weight get_weight(void) const;
//!
//! whether transition is final
bool final(void) const;
};
//! Internal class for Transducer processing.
//! Contains low-level processing stuff.
class IndexTable
{
private:
char * indices;
TransitionTableIndex size;
void read(FILE * f,
TransitionTableIndex number_of_table_entries);
void read(char ** raw,
TransitionTableIndex number_of_table_entries);
void convert_to_big_endian(void);
public:
//!
//! read index table from file @a f.
IndexTable(FILE * f,
TransitionTableIndex number_of_table_entries);
//!
//! read index table from raw data @a raw.
IndexTable(char ** raw,
TransitionTableIndex number_of_table_entries);
~IndexTable(void);
//!
//! input symbol for the index
SymbolNumber input_symbol(TransitionTableIndex i) const;
//!
//! target state location for the index
TransitionTableIndex target(TransitionTableIndex i) const;
//!
//! whether it's final transition
bool final(TransitionTableIndex i) const;
//!
//! transition's weight
Weight final_weight(TransitionTableIndex i) const;
};
//! Internal class for transition processing.
//! Contains low-level processing stuff.
class TransitionTable
{
protected:
//!
//! raw transition data
char * transitions;
TransitionTableIndex size;
//!
//! read known amount of transitions from file @a f
void read(FILE * f,
TransitionTableIndex number_of_table_entries);
//! read known amount of transitions from raw dara @a data
void read(char ** raw,
TransitionTableIndex number_of_table_entries);
void convert_to_big_endian(void);
public:
//!
//! read transition table from file @a f
TransitionTable(FILE * f,
TransitionTableIndex transition_count);
//!
//! read transition table from raw data @a raw
TransitionTable(char ** raw,
TransitionTableIndex transition_count);
~TransitionTable(void);
//!
//! transition's input symbol
SymbolNumber input_symbol(TransitionTableIndex i) const;
//!
//! transition's output symbol
SymbolNumber output_symbol(TransitionTableIndex i) const;
//!
//! target node location
TransitionTableIndex target(TransitionTableIndex i) const;
//!
//! weight of transiton
Weight weight(TransitionTableIndex i) const;
//!
//! whether it's final
bool final(TransitionTableIndex i) const;
};
template <class printable>
void debug_print(printable p)
{
if (0) {
std::cerr << p;
}
}
} // namespace hfst_ospell
#endif // HFST_OSPELL_HFST_OL_H_