/usr/include/hfst/implementations/optimized-lookup/pmatch.h is in libhfst-dev 3.13.0~r3461-2.
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 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 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 | // Copyright (c) 2016 University of Helsinki
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 3 of the License, or (at your option) any later version.
// See the file COPYING included with this distribution for more
// information.
#ifndef _HFST_OL_TRANSDUCER_PMATCH_H_
#define _HFST_OL_TRANSDUCER_PMATCH_H_
#include <map>
#include <stack>
#include <sstream>
#include <algorithm>
#include <ctime>
#include "HfstTransducer.h"
#include "HfstExceptionDefs.h"
#include "transducer.h"
namespace hfst_ol {
class PmatchTransducer;
class PmatchContainer;
struct Location;
struct WeightedDoubleTape;
struct RtnStackFrame;
typedef std::vector<RtnStackFrame> RtnCallStack;
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,
Pmatch_input_mark,
SPECIALSYMBOL_NR_ITEMS};
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 PmatchAlphabet: public TransducerAlphabet {
protected:
RtnVector rtns;
SymbolNumberVector special_symbols;
std::map<SymbolNumber, std::string> end_tag_map;
std::map<std::string, SymbolNumber> capture_tag_map;
std::map<std::string, SymbolNumber> captured_tag_map;
SymbolNumberVector capture2captured;
SymbolNumberVector captured2capture;
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_list_members
SymbolNumberVector list2symbols;
// For each entry referring to entries in the symbol table, indicate
// "this symbol is an exclusionary list", ie. symbols not in it
// will match
SymbolNumberVector exclusionary_lists;
std::vector<SymbolNumberVector> symbol_lists;
std::vector<SymbolNumberVector> symbol_list_members;
std::vector<unsigned long> counters;
SymbolNumberVector guards;
std::vector<bool> global_flags;
std::vector<bool> printable_vector;
bool is_end_tag(const SymbolNumber symbol) const;
bool is_capture_tag(const SymbolNumber symbol) const;
bool is_captured_tag(const SymbolNumber symbol) const;
bool is_input_mark(const SymbolNumber symbol) const;
bool is_guard(const SymbolNumber symbol) const;
bool is_counter(const SymbolNumber symbol) const;
bool is_global_flag(const SymbolNumber symbol) const;
std::string end_tag(const SymbolNumber symbol);
std::string start_tag(const SymbolNumber symbol);
PmatchContainer * container;
public:
PmatchAlphabet(std::istream& is, SymbolNumber symbol_count, PmatchContainer * cont);
PmatchAlphabet(TransducerAlphabet const & a, PmatchContainer * cont);
PmatchAlphabet(void);
~PmatchAlphabet(void);
virtual void add_symbol(const std::string & symbol);
static bool is_end_tag(const std::string & symbol);
static bool is_capture_tag(const std::string & symbol);
static bool is_captured_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 bool is_printable(const std::string & symbol);
static bool is_global_flag(const std::string & symbol);
static std::string name_from_insertion(
const std::string & symbol);
bool is_printable(SymbolNumber symbol);
bool is_global_flag(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);
PmatchTransducer * get_rtn(std::string name);
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;
};
struct RtnStackFrame
{
PmatchTransducer * caller;
TransitionTableIndex caller_index;
};
struct Capture
{
unsigned int begin;
unsigned int end;
SymbolNumber name;
};
class PmatchContainer
{
protected:
PmatchAlphabet alphabet;
Encoder * encoder;
SymbolNumber orig_symbol_count;
SymbolNumber symbol_count;
PmatchTransducer * toplevel;
SymbolNumberVector input;
// This tracks the ENTRY and EXIT tags
PositionStack entry_stack;
RtnCallStack rtn_stack;
DoubleTape tape;
DoubleTape best_result;
DoubleTape result;
LocationVectorVector locations;
WeightedDoubleTapeVector tape_locations;
std::vector<Capture> captures;
std::vector<Capture> best_captures;
std::vector<Capture> old_captures;
std::vector<char> possible_first_symbols;
// The flag state for global flags
hfst::FdState<SymbolNumber> global_flag_state;
bool verbose;
bool count_patterns;
bool delete_patterns;
bool extract_patterns;
bool locate_mode;
bool mark_patterns;
size_t max_context_length;
size_t max_recursion;
bool need_separators;
bool xerox_composition;
unsigned long line_number;
std::map<std::string, size_t> pattern_counts;
bool profile_mode;
bool single_codepoint_tokenization;
unsigned int recursion_depth_left;
// An optional time limit for operations
double max_time;
// When we started work
clock_t start_clock;
// A counter to avoid checking the clock too often
unsigned long call_counter;
// A flag to set for when time has been overstepped
bool limit_reached;
// The global running weight
Weight running_weight;
// This is the depth of the stack from the point of view of the
// container. When it's 0, we're in the toplevel, even if the
// stack of variables is bigger due to having passed through a RTN.
unsigned int stack_depth;
// Where in the input the best candidate so far has gotten to
unsigned int best_input_pos;
Weight best_weight;
void collect_first_symbols(void);
public:
PmatchContainer(std::istream & is);
PmatchContainer(Transducer * toplevel);
PmatchContainer(std::vector<hfst::HfstTransducer> transducers);
PmatchContainer(void);
~PmatchContainer(void);
void set_properties(void);
void set_properties(std::map<std::string, std::string> & properties);
void initialize_input(const char * input);
bool has_unsatisfied_rtns(void) const;
std::string get_unsatisfied_rtn_name(void) const;
void add_rtn(Transducer * rtn, const std::string & name);
void process(const std::string & input);
std::string match(const std::string & input,
double time_cutoff = 0.0);
LocationVectorVector locate(const std::string & input,
double time_cutoff = 0.0);
void note_analysis(unsigned int input_pos, unsigned int tape_pos);
void grab_location(unsigned int input_pos, unsigned int tape_pos);
std::pair<SymbolNumberVector::iterator,
SymbolNumberVector::iterator>
get_longest_matching_capture(SymbolNumber key, unsigned int input_pos);
std::string get_profiling_info(void);
std::string get_pattern_count_info(void);
bool has_queued_input(unsigned int input_pos);
bool input_matches_at(unsigned int pos,
SymbolNumberVector::iterator begin,
SymbolNumberVector::iterator end);
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_result(const DoubleTape & best_result);
void copy_to_result(SymbolNumber input, SymbolNumber output);
static std::map<std::string, std::string> parse_hfst3_header(std::istream & f);
void set_verbose(bool b) { verbose = b; }
void set_locate_mode(bool b) { locate_mode = b; }
void set_extract_patterns(bool b)
{ extract_patterns = b; }
void set_single_codepoint_tokenization(bool b)
{ single_codepoint_tokenization = b; }
void set_count_patterns(bool b)
{ count_patterns = b; }
void set_delete_patterns(bool b)
{ delete_patterns = b; }
void set_mark_patterns(bool b)
{ mark_patterns = b; }
void set_max_recursion(size_t max)
{ max_recursion = max; }
void set_max_context(size_t max)
{ max_context_length = max; }
bool is_in_locate_mode(void) { return locate_mode; }
void set_profile(bool b) { profile_mode = b; }
void set_weight(Weight w) { running_weight = w; }
void increment_weight(Weight w) { running_weight += w; }
Weight get_weight(void) { return running_weight; }
void increase_stack_depth(void) { ++stack_depth; }
void decrease_stack_depth(void)
{
if (stack_depth == 0) {
HFST_THROW_MESSAGE(HfstException, "pmatch: negative stack depth");
}
--stack_depth;
}
void push_rtn_call(unsigned int return_index, PmatchTransducer * caller);
RtnStackFrame rtn_stack_top(void);
void rtn_stack_pop(void);
unsigned int get_stack_depth(void) { return stack_depth; }
bool candidate_found(void)
{
if (locate_mode) {
return tape_locations.size() != 0;
} else {
return best_result.size() != 0;
}
}
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 = (unsigned int)max_recursion; }
friend class PmatchTransducer;
friend class PmatchAlphabet;
};
struct Location
{
unsigned int start;
unsigned int length;
std::string input;
std::string output;
std::string tag;
Weight weight;
std::vector<size_t> input_parts; // indices in input_symbol_strings
std::vector<size_t> output_parts; // indices in output_symbol_strings
std::vector<std::string> input_symbol_strings;
std::vector<std::string> output_symbol_strings;
bool operator<(Location rhs) const
{ return this->weight < rhs.weight; }
};
struct ContextMatchedTrap
{
bool polarity;
ContextMatchedTrap(bool p): polarity(p) {}
};
class PmatchTransducer
{
protected:
std::string name;
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;
// Used for context checks
char tape_step;
size_t max_context_length_remaining;
unsigned int context_placeholder;
ContextChecking context;
bool default_symbol_trap;
bool negative_context_success;
bool pending_passthrough;
};
std::stack<LocalVariables> local_stack;
std::vector<TransitionW> transition_table;
std::vector<TransitionWIndex> index_table;
PmatchAlphabet & alphabet;
SymbolNumber orig_symbol_count;
PmatchContainer * container;
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();
}
}
Weight 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_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,
std::string name,
PmatchContainer * container);
PmatchTransducer(std::vector<TransitionW> transition_vector,
std::vector<TransitionWIndex> index_vector,
PmatchAlphabet & alphabet,
std::string name,
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; }
void match(unsigned int input_pos, unsigned int tape_pos);
void rtn_call(unsigned int input_pos, unsigned int tape_pos,
PmatchTransducer * caller, TransitionTableIndex caller_index);
void rtn_return(unsigned int input_pos, unsigned int tape_pos);
void handle_final_state(unsigned int input_pos, unsigned int tape_pos);
void collect_possible_first_symbols(void);
friend class PmatchContainer;
};
}
#endif //_HFST_OL_TRANSDUCER_PMATCH_H_
|