27 #if defined(_WIN32) && !defined(__GNUC__) 28 #define strtok_r(str, delim, saveptr) strtok_s(str, delim, saveptr) 50 bool use_ambigs_for_adaption) {
51 for (
int i = 0; i < unicharset.size(); ++i) {
52 replace_ambigs_.push_back(
nullptr);
53 dang_ambigs_.push_back(
nullptr);
54 one_to_one_definite_ambigs_.push_back(
nullptr);
55 if (use_ambigs_for_adaption) {
56 ambigs_for_adaption_.push_back(
nullptr);
57 reverse_ambigs_for_adaption_.push_back(
nullptr);
73 bool use_ambigs_for_adaption,
77 if (debug_level)
tprintf(
"Reading ambiguities\n");
79 int test_ambig_part_size;
80 int replacement_ambig_part_size;
84 char *buffer =
new char[kBufferSize];
95 version =
static_cast<int>(strtol(buffer+1,
nullptr, 10));
100 while (ambig_file->
FGets(buffer, kBufferSize) !=
nullptr) {
102 if (debug_level > 2)
tprintf(
"read line %s\n", buffer);
104 if (!ParseAmbiguityLine(line_num, version, debug_level, encoder_set,
105 buffer, &test_ambig_part_size, test_unichar_ids,
106 &replacement_ambig_part_size,
107 replacement_string, &type))
continue;
110 if (!InsertIntoTable((type ==
REPLACE_AMBIG) ? replace_ambigs_
112 test_ambig_part_size, test_unichar_ids,
113 replacement_ambig_part_size, replacement_string, type,
114 ambig_spec, unicharset))
118 if (test_ambig_part_size == 1 &&
120 if (one_to_one_definite_ambigs_[test_unichar_ids[0]] ==
nullptr) {
121 one_to_one_definite_ambigs_[test_unichar_ids[0]] =
new UnicharIdVector();
123 one_to_one_definite_ambigs_[test_unichar_ids[0]]->push_back(
127 if (use_ambigs_for_adaption) {
131 if (unicharset->
encode_string(replacement_string,
true, &encoding,
133 for (i = 0; i < test_ambig_part_size; ++i) {
134 if (ambigs_for_adaption_[test_unichar_ids[i]] ==
nullptr) {
137 adaption_ambigs_entry = ambigs_for_adaption_[test_unichar_ids[i]];
138 for (
int r = 0; r < encoding.
size(); ++r) {
143 for (j = 0; j < adaption_ambigs_entry->
size() &&
144 (*adaption_ambigs_entry)[j] > id_to_insert; ++j);
145 if (j < adaption_ambigs_entry->size()) {
146 if ((*adaption_ambigs_entry)[j] != id_to_insert) {
147 adaption_ambigs_entry->
insert(id_to_insert, j);
150 adaption_ambigs_entry->
push_back(id_to_insert);
160 if (use_ambigs_for_adaption) {
161 for (i = 0; i < ambigs_for_adaption_.size(); ++i) {
162 adaption_ambigs_entry = ambigs_for_adaption_[i];
163 if (adaption_ambigs_entry ==
nullptr)
continue;
164 for (j = 0; j < adaption_ambigs_entry->
size(); ++j) {
165 UNICHAR_ID ambig_id = (*adaption_ambigs_entry)[j];
166 if (reverse_ambigs_for_adaption_[ambig_id] ==
nullptr) {
169 reverse_ambigs_for_adaption_[ambig_id]->push_back(i);
175 if (debug_level > 1) {
176 for (
int tbl = 0; tbl < 2; ++tbl) {
178 (tbl == 0) ? replace_ambigs_ : dang_ambigs_;
179 for (i = 0; i < print_table.
size(); ++i) {
180 AmbigSpec_LIST *lst = print_table[i];
181 if (lst ==
nullptr)
continue;
183 tprintf(
"%s Ambiguities for %s:\n",
184 (tbl == 0) ?
"Replaceable" :
"Dangerous",
187 AmbigSpec_IT lst_it(lst);
188 for (lst_it.mark_cycle_pt(); !lst_it.cycled_list(); lst_it.forward()) {
197 if (use_ambigs_for_adaption) {
198 for (
int vec_id = 0; vec_id < 2; ++vec_id) {
200 ambigs_for_adaption_ : reverse_ambigs_for_adaption_;
201 for (i = 0; i < vec.
size(); ++i) {
202 adaption_ambigs_entry = vec[i];
203 if (adaption_ambigs_entry !=
nullptr) {
204 tprintf(
"%sAmbigs for adaption for %s:\n",
205 (vec_id == 0) ?
"" :
"Reverse ",
207 for (j = 0; j < adaption_ambigs_entry->
size(); ++j) {
209 (*adaption_ambigs_entry)[j]).
string());
219 bool UnicharAmbigs::ParseAmbiguityLine(
220 int line_num,
int version,
int debug_level,
const UNICHARSET &unicharset,
221 char *buffer,
int *test_ambig_part_size,
UNICHAR_ID *test_unichar_ids,
222 int *replacement_ambig_part_size,
char *replacement_string,
int *type) {
227 input.split(
' ', &fields);
228 if (fields.
size() != 3) {
229 if (debug_level)
tprintf(kIllegalMsg, line_num);
234 if (!unicharset.
encode_string(fields[0].string(),
true, &unichars,
nullptr,
238 *test_ambig_part_size = unichars.
size();
241 tprintf(
"Too many unichars in ambiguity on line %d\n", line_num);
245 for (
int i = 0; i < unichars.
size(); ++i)
246 test_unichar_ids[i] = unichars[i];
247 test_unichar_ids[unichars.
size()] = INVALID_UNICHAR_ID;
249 if (!unicharset.
encode_string(fields[1].string(),
true, &unichars,
nullptr,
253 *replacement_ambig_part_size = unichars.
size();
256 tprintf(
"Too many unichars in ambiguity on line %d\n", line_num);
259 if (sscanf(fields[2].
string(),
"%d", type) != 1) {
260 if (debug_level)
tprintf(kIllegalMsg, line_num);
269 if (!(token = strtok_r(buffer, kAmbigDelimiters, &next_token)) ||
270 !sscanf(token,
"%d", test_ambig_part_size) ||
271 *test_ambig_part_size <= 0) {
272 if (debug_level)
tprintf(kIllegalMsg, line_num);
277 tprintf(
"Too many unichars in ambiguity on line %d\n", line_num);
280 for (i = 0; i < *test_ambig_part_size; ++i) {
281 if (!(token = strtok_r(
nullptr, kAmbigDelimiters, &next_token)))
break;
283 if (debug_level)
tprintf(kIllegalUnicharMsg, token);
288 test_unichar_ids[i] = INVALID_UNICHAR_ID;
290 if (i != *test_ambig_part_size ||
291 !(token = strtok_r(
nullptr, kAmbigDelimiters, &next_token)) ||
292 !sscanf(token,
"%d", replacement_ambig_part_size) ||
293 *replacement_ambig_part_size <= 0) {
294 if (debug_level)
tprintf(kIllegalMsg, line_num);
299 tprintf(
"Too many unichars in ambiguity on line %d\n", line_num);
302 replacement_string[0] =
'\0';
303 for (i = 0; i < *replacement_ambig_part_size; ++i) {
304 if (!(token = strtok_r(
nullptr, kAmbigDelimiters, &next_token)))
break;
305 strcat(replacement_string, token);
307 if (debug_level)
tprintf(kIllegalUnicharMsg, token);
311 if (i != *replacement_ambig_part_size) {
312 if (debug_level)
tprintf(kIllegalMsg, line_num);
325 if (!(token = strtok_r(
nullptr, kAmbigDelimiters, &next_token)) ||
326 !sscanf(token,
"%d", type)) {
327 if (debug_level)
tprintf(kIllegalMsg, line_num);
334 bool UnicharAmbigs::InsertIntoTable(
336 UNICHAR_ID *test_unichar_ids,
int replacement_ambig_part_size,
337 const char *replacement_string,
int type,
338 AmbigSpec *ambig_spec,
UNICHARSET *unicharset) {
339 ambig_spec->type =
static_cast<AmbigType>(type);
340 if (test_ambig_part_size == 1 && replacement_ambig_part_size == 1 &&
341 unicharset->
to_lower(test_unichar_ids[0]) ==
346 ambig_spec->wrong_ngram_size =
361 ambig_spec->correct_ngram_id =
363 if (replacement_ambig_part_size > 1) {
364 unicharset->
set_isngram(ambig_spec->correct_ngram_id,
true);
368 for (i = 0; i < test_ambig_part_size; ++i) {
370 if (test_ambig_part_size == 1) {
371 unichar_id = ambig_spec->correct_ngram_id;
374 replacement_string, i, test_ambig_part_size,
false);
378 ambig_spec->correct_fragments[i] = unichar_id;
380 ambig_spec->correct_fragments[i] = INVALID_UNICHAR_ID;
384 if (table[test_unichar_ids[0]] ==
nullptr) {
385 table[test_unichar_ids[0]] =
new AmbigSpec_LIST();
387 if (table[test_unichar_ids[0]]->add_sorted(
const int kMaxAmbigStringSize
bool encode_string(const char *str, bool give_up_on_failure, GenericVector< UNICHAR_ID > *encoding, GenericVector< char > *lengths, int *encoded_length) const
const char kUniversalAmbigsFile[]
const char * string() const
static int compare_ambig_specs(const void *spec1, const void *spec2)
char * FGets(char *buffer, int buffer_size)
UNICHAR_ID unichar_to_id(const char *const unichar_repr) const
void set_isngram(UNICHAR_ID unichar_id, bool value)
void LoadUniversal(const UNICHARSET &encoder_set, UNICHARSET *unicharset)
GenericVector< UNICHAR_ID > UnicharIdVector
void unichar_insert(const char *const unichar_repr, OldUncleanUnichars old_style)
void insert(const T &t, int index)
void chomp_string(char *str)
static void print(const UNICHAR_ID array[], const UNICHARSET &unicharset)
bool contains_unichar(const char *const unichar_repr) const
STRING debug_str(UNICHAR_ID id) const
static int copy(const UNICHAR_ID src[], UNICHAR_ID dst[])
#define ELISTIZE(CLASSNAME)
DLLSYM void tprintf(const char *format,...)
bool Open(const STRING &filename, FileReader reader)
UNICHAR_ID correct_ngram_id
UNICHAR_ID correct_fragments[MAX_AMBIG_SIZE+1]
const int ksizeofUniversalAmbigsFile
void LoadUnicharAmbigs(const UNICHARSET &encoder_set, TFile *ambigs_file, int debug_level, bool use_ambigs_for_adaption, UNICHARSET *unicharset)
GenericVector< AmbigSpec_LIST * > UnicharAmbigsVector
UNICHAR_ID to_lower(UNICHAR_ID unichar_id) const
UNICHAR_ID wrong_ngram[MAX_AMBIG_SIZE+1]