/usr/include/hfst/implementations/optimized-lookup/pmatch.h is in libhfst42-dev 3.9.0~r4595-3.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 | #ifndef _HFST_OL_TRANSDUCER_PMATCH_H_
#define _HFST_OL_TRANSDUCER_PMATCH_H_
#include <map>
#include <stack>
#include <sstream>
#include <algorithm>
#include "transducer.h"
namespace hfst_ol {
class PmatchTransducer;
class PmatchContainer;
struct Location;
class WeightedDoubleTape;
const unsigned int PMATCH_MAX_RECURSION_DEPTH = 5000;
typedef std::vector<PmatchTransducer *> RtnVector;
typedef std::map<std::string, SymbolNumber> RtnNameMap;
typedef std::vector<Location> LocationVector;
typedef std::vector<LocationVector> LocationVectorVector;
typedef std::vector<WeightedDoubleTape> WeightedDoubleTapeVector;
enum SpecialSymbol{entry,
exit,
LC_entry,
LC_exit,
RC_entry,
RC_exit,
NLC_entry,
NLC_exit,
NRC_entry,
NRC_exit,
Pmatch_passthrough,
boundary};
struct SymbolPair
{
SymbolNumber input;
SymbolNumber output;
SymbolPair(void): input(0), output(0) {}
SymbolPair(SymbolNumber i, SymbolNumber o): input(i), output(o) {}
};
class DoubleTape: public std::vector<SymbolPair>
{
public:
void write(unsigned int pos, SymbolNumber in, SymbolNumber out)
{
while (pos >= this->size()) {
this->push_back(SymbolPair());
}
this->operator[](pos) = SymbolPair(in, out);
}
DoubleTape extract_slice(unsigned int start, unsigned int stop)
{
DoubleTape retval;
while(start < stop) {
retval.push_back(this->at(start));
++start;
}
return retval;
}
};
class PositionStack: public std::vector<unsigned int>
{
unsigned int tmp;
public:
void push(unsigned int val) { push_back(val); }
void pop(void) { tmp = back(); pop_back(); }
void unpop(void) { push_back(tmp); }
unsigned int top(void) { return back(); }
};
class WeightedDoubleTape: public DoubleTape
{
public:
Weight weight;
WeightedDoubleTape(DoubleTape dt, Weight w): DoubleTape(dt), weight(w) {}
};
class PmatchAlphabet: public TransducerAlphabet {
protected:
RtnVector rtns;
SymbolNumberVector special_symbols;
std::map<SymbolNumber, std::string> end_tag_map;
RtnNameMap rtn_names;
// For each symbol, either NO_SYMBOL for "no corresponding list" or an index into symbol_lists
SymbolNumberVector symbol2lists;
// For each a symbol, either NO_SYMBOL for "this is not a list" or an index into symbol_lists_members
SymbolNumberVector list2symbols;
std::vector<SymbolNumberVector> symbol_lists;
std::vector<SymbolNumberVector> symbol_list_members;
std::vector<unsigned long int> counters;
SymbolNumberVector guards;
std::vector<bool> printable_vector;
bool is_end_tag(const SymbolNumber symbol) const;
bool is_guard(const SymbolNumber symbol) const;
bool is_counter(const SymbolNumber symbol) const;
std::string end_tag(const SymbolNumber symbol);
std::string start_tag(const SymbolNumber symbol);
bool extract_tags;
public:
PmatchAlphabet(std::istream& is, SymbolNumber symbol_count);
PmatchAlphabet(void);
~PmatchAlphabet(void);
virtual void add_symbol(const std::string & symbol);
static bool is_end_tag(const std::string & symbol);
static bool is_insertion(const std::string & symbol);
static bool is_guard(const std::string & symbol);
static bool is_list(const std::string & symbol);
static bool is_counter(const std::string & symbol);
static bool is_special(const std::string & symbol);
static std::string name_from_insertion(
const std::string & symbol);
bool is_printable(SymbolNumber symbol);
void add_special_symbol(const std::string & str, SymbolNumber symbol_number);
void process_symbol_list(std::string str, SymbolNumber sym);
void process_counter(std::string str, SymbolNumber sym);
void count(SymbolNumber sym);
void add_rtn(PmatchTransducer * rtn, std::string const & name);
bool has_rtn(std::string const & name) const;
bool has_rtn(SymbolNumber symbol) const;
PmatchTransducer * get_rtn(SymbolNumber symbol);
std::string get_counter_name(SymbolNumber symbol);
SymbolNumber get_special(SpecialSymbol special) const;
SymbolNumberVector get_specials(void) const;
std::string stringify(const DoubleTape & str);
Location locatefy(unsigned int input_offset,
const WeightedDoubleTape & str);
friend class PmatchTransducer;
friend class PmatchContainer;
};
class PmatchContainer
{
protected:
PmatchAlphabet alphabet;
Encoder * encoder;
SymbolNumber orig_symbol_count;
SymbolNumber symbol_count;
PmatchTransducer * toplevel;
size_t io_size;
SymbolNumberVector input;
PositionStack entry_stack;
DoubleTape tape;
DoubleTape output;
LocationVectorVector locations;
std::vector<char> possible_first_symbols;
bool verbose;
bool locate_mode;
bool profile_mode;
bool single_codepoint_tokenization;
unsigned int recursion_depth_left;
public:
PmatchContainer(std::istream & is);
PmatchContainer(void);
~PmatchContainer(void);
unsigned long line_number;
void initialize_input(const char * input);
bool has_unsatisfied_rtns(void) const;
std::string get_unsatisfied_rtn_name(void) const;
void process(std::string & input);
std::string match(std::string & input);
LocationVectorVector locate(std::string & input);
std::string get_profiling_info(void);
bool has_queued_input(unsigned int input_pos);
bool not_possible_first_symbol(SymbolNumber sym)
{
if (possible_first_symbols.size() == 0) {
return false;
}
return sym >= possible_first_symbols.size() ||
possible_first_symbols[sym] == 0;
}
void copy_to_output(const DoubleTape & best_result);
void copy_to_output(SymbolNumber input, SymbolNumber output);
std::string stringify_output(void);
// LocationVector locatefy_output(void);
static std::string parse_name_from_hfst3_header(std::istream & f);
void set_verbose(bool b) { verbose = b; }
void set_extract_tags_mode(bool b)
{ alphabet.extract_tags = b; }
void set_single_codepoint_tokenization(bool b)
{ single_codepoint_tokenization = b; }
void set_profile(bool b) { profile_mode = b; }
bool try_recurse(void)
{
if (recursion_depth_left > 0) {
--recursion_depth_left;
return true;
} else {
return false;
}
}
void unrecurse(void) { ++recursion_depth_left; }
void reset_recursion(void) { recursion_depth_left = PMATCH_MAX_RECURSION_DEPTH; }
friend class PmatchTransducer;
};
struct Location
{
unsigned int start;
unsigned int length;
std::string input;
std::string output;
std::string tag;
Weight weight;
bool operator<(Location rhs) const
{ return this->weight < rhs.weight; }
};
struct ContextMatchedTrap
{
bool polarity;
ContextMatchedTrap(bool p): polarity(p) {}
};
class PmatchTransducer
{
protected:
enum ContextChecking{none, LC, NLC, RC, NRC};
// Transducers have static data, ie. tables for describing the states and
// transitions, and dynamic data, which is altered during lookup.
// In pmatch several instances of the same transducer may be operating
// in a stack, so this dynamic data is put in a class of its own.
struct LocalVariables
{
hfst::FdState<SymbolNumber> flag_state;
char tape_step;
unsigned int context_placeholder;
ContextChecking context;
bool default_symbol_trap;
bool negative_context_success;
bool pending_passthrough;
Weight running_weight;
};
struct RtnVariables
{
unsigned int candidate_input_pos;
unsigned int candidate_tape_pos;
unsigned int input_tape_entry;
unsigned int tape_entry;
DoubleTape best_result;
Weight best_weight;
};
std::stack<LocalVariables> local_stack;
std::stack<RtnVariables> rtn_stack;
std::vector<TransitionW> transition_table;
std::vector<TransitionWIndex> index_table;
PmatchAlphabet & alphabet;
SymbolNumber orig_symbol_count;
PmatchContainer * container;
WeightedDoubleTapeVector * locations;
bool is_final(TransitionTableIndex i)
{
if (indexes_transition_table(i)) {
return transition_table[i - TRANSITION_TARGET_TABLE_START].final();
} else {
return index_table[i].final();
}
}
bool get_weight(TransitionTableIndex i)
{
if (indexes_transition_table(i)) {
return transition_table[i - TRANSITION_TARGET_TABLE_START].get_weight();
} else {
return index_table[i].final_weight();
}
}
TransitionTableIndex make_transition_table_index(
TransitionTableIndex i, SymbolNumber input) {
if (indexes_transition_table(i)) {
return i - TRANSITION_TARGET_TABLE_START;
} else {
if (index_table[i + input].get_input_symbol() == input) {
return index_table[i + input].get_target() - TRANSITION_TARGET_TABLE_START;
} else {
return TRANSITION_TARGET_TABLE_START;
}
}
}
// The mutually recursive lookup-handling functions
void take_epsilons(unsigned int input_pos,
unsigned int tape_pos,
TransitionTableIndex i);
void check_context(unsigned int input_pos,
unsigned int tape_pos,
TransitionTableIndex i);
void take_rtn(SymbolNumber input,
unsigned int input_pos,
unsigned int tape_pos,
TransitionTableIndex i);
void take_flag(SymbolNumber input,
unsigned int input_pos,
unsigned int tape_pos,
TransitionTableIndex i);
void take_transitions(SymbolNumber input,
unsigned int input_pos,
unsigned int tape_pos,
TransitionTableIndex i);
void get_analyses(unsigned int input_pos,
unsigned int tape_pos,
TransitionTableIndex index);
bool checking_context(void) const;
bool try_entering_context(SymbolNumber symbol);
bool try_exiting_context(SymbolNumber symbol);
void exit_context(void);
void collect_first_epsilon(TransitionTableIndex i,
SymbolNumberVector const& input_symbols,
std::set<TransitionTableIndex> & seen_indices);
void collect_first_epsilon_index(TransitionTableIndex i,
SymbolNumberVector const& input_symbols,
std::set<TransitionTableIndex> & seen_indices);
void collect_first_transition(TransitionTableIndex i,
SymbolNumberVector const& input_symbols,
std::set<TransitionTableIndex> & seen_indices);
void collect_first_index(TransitionTableIndex i,
SymbolNumberVector const& input_symbols,
std::set<TransitionTableIndex> & seen_indices);
void collect_first(TransitionTableIndex i,
SymbolNumberVector const& input_symbols,
std::set<TransitionTableIndex> & seen_indices);
public:
PmatchTransducer(std::istream& is,
TransitionTableIndex index_table_size,
TransitionTableIndex transition_table_size,
PmatchAlphabet & alphabet,
PmatchContainer * container);
std::set<SymbolNumber> possible_first_symbols;
bool final_index(TransitionTableIndex i) const
{
if (indexes_transition_table(i)) {
return transition_table[i].final();
} else {
return index_table[i].final();
}
}
static bool indexes_transition_table(TransitionTableIndex i)
{ return i >= TRANSITION_TARGET_TABLE_START; }
static bool is_good(TransitionTableIndex i)
{ return i < TRANSITION_TARGET_TABLE_START; }
const DoubleTape & get_best_result(void) const
{ return rtn_stack.top().best_result; }
unsigned int get_candidate_input_pos(void) const
{ return rtn_stack.top().candidate_input_pos; }
Weight get_best_weight(void) const
{ return rtn_stack.top().best_weight; }
void match(unsigned int & input_pos, unsigned int & tape_pos);
void rtn_call(unsigned int & input_pos, unsigned int & tape_pos);
void rtn_exit(void);
void note_analysis(unsigned int input_pos, unsigned int tape_pos);
void grab_location(unsigned int input_pos, unsigned int tape_pos);
void collect_possible_first_symbols(void);
friend class PmatchContainer;
};
}
#endif //_HFST_OL_TRANSDUCER_PMATCH_H_
|