intl/icu/source/common/dictbe.cpp

Wed, 31 Dec 2014 07:22:50 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 07:22:50 +0100
branch
TOR_BUG_3246
changeset 4
fc2d59ddac77
permissions
-rw-r--r--

Correct previous dual key logic pending first delivery installment.

michael@0 1 /**
michael@0 2 *******************************************************************************
michael@0 3 * Copyright (C) 2006-2013, International Business Machines Corporation
michael@0 4 * and others. All Rights Reserved.
michael@0 5 *******************************************************************************
michael@0 6 */
michael@0 7
michael@0 8 #include "unicode/utypes.h"
michael@0 9
michael@0 10 #if !UCONFIG_NO_BREAK_ITERATION
michael@0 11
michael@0 12 #include "brkeng.h"
michael@0 13 #include "dictbe.h"
michael@0 14 #include "unicode/uniset.h"
michael@0 15 #include "unicode/chariter.h"
michael@0 16 #include "unicode/ubrk.h"
michael@0 17 #include "uvector.h"
michael@0 18 #include "uassert.h"
michael@0 19 #include "unicode/normlzr.h"
michael@0 20 #include "cmemory.h"
michael@0 21 #include "dictionarydata.h"
michael@0 22
michael@0 23 U_NAMESPACE_BEGIN
michael@0 24
michael@0 25 /*
michael@0 26 ******************************************************************
michael@0 27 */
michael@0 28
michael@0 29 DictionaryBreakEngine::DictionaryBreakEngine(uint32_t breakTypes) {
michael@0 30 fTypes = breakTypes;
michael@0 31 }
michael@0 32
michael@0 33 DictionaryBreakEngine::~DictionaryBreakEngine() {
michael@0 34 }
michael@0 35
michael@0 36 UBool
michael@0 37 DictionaryBreakEngine::handles(UChar32 c, int32_t breakType) const {
michael@0 38 return (breakType >= 0 && breakType < 32 && (((uint32_t)1 << breakType) & fTypes)
michael@0 39 && fSet.contains(c));
michael@0 40 }
michael@0 41
michael@0 42 int32_t
michael@0 43 DictionaryBreakEngine::findBreaks( UText *text,
michael@0 44 int32_t startPos,
michael@0 45 int32_t endPos,
michael@0 46 UBool reverse,
michael@0 47 int32_t breakType,
michael@0 48 UStack &foundBreaks ) const {
michael@0 49 int32_t result = 0;
michael@0 50
michael@0 51 // Find the span of characters included in the set.
michael@0 52 int32_t start = (int32_t)utext_getNativeIndex(text);
michael@0 53 int32_t current;
michael@0 54 int32_t rangeStart;
michael@0 55 int32_t rangeEnd;
michael@0 56 UChar32 c = utext_current32(text);
michael@0 57 if (reverse) {
michael@0 58 UBool isDict = fSet.contains(c);
michael@0 59 while((current = (int32_t)utext_getNativeIndex(text)) > startPos && isDict) {
michael@0 60 c = utext_previous32(text);
michael@0 61 isDict = fSet.contains(c);
michael@0 62 }
michael@0 63 rangeStart = (current < startPos) ? startPos : current+(isDict ? 0 : 1);
michael@0 64 rangeEnd = start + 1;
michael@0 65 }
michael@0 66 else {
michael@0 67 while((current = (int32_t)utext_getNativeIndex(text)) < endPos && fSet.contains(c)) {
michael@0 68 utext_next32(text); // TODO: recast loop for postincrement
michael@0 69 c = utext_current32(text);
michael@0 70 }
michael@0 71 rangeStart = start;
michael@0 72 rangeEnd = current;
michael@0 73 }
michael@0 74 if (breakType >= 0 && breakType < 32 && (((uint32_t)1 << breakType) & fTypes)) {
michael@0 75 result = divideUpDictionaryRange(text, rangeStart, rangeEnd, foundBreaks);
michael@0 76 utext_setNativeIndex(text, current);
michael@0 77 }
michael@0 78
michael@0 79 return result;
michael@0 80 }
michael@0 81
michael@0 82 void
michael@0 83 DictionaryBreakEngine::setCharacters( const UnicodeSet &set ) {
michael@0 84 fSet = set;
michael@0 85 // Compact for caching
michael@0 86 fSet.compact();
michael@0 87 }
michael@0 88
michael@0 89 /*
michael@0 90 ******************************************************************
michael@0 91 * PossibleWord
michael@0 92 */
michael@0 93
michael@0 94 // Helper class for improving readability of the Thai/Lao/Khmer word break
michael@0 95 // algorithm. The implementation is completely inline.
michael@0 96
michael@0 97 // List size, limited by the maximum number of words in the dictionary
michael@0 98 // that form a nested sequence.
michael@0 99 #define POSSIBLE_WORD_LIST_MAX 20
michael@0 100
michael@0 101 class PossibleWord {
michael@0 102 private:
michael@0 103 // list of word candidate lengths, in increasing length order
michael@0 104 int32_t lengths[POSSIBLE_WORD_LIST_MAX];
michael@0 105 int32_t count; // Count of candidates
michael@0 106 int32_t prefix; // The longest match with a dictionary word
michael@0 107 int32_t offset; // Offset in the text of these candidates
michael@0 108 int mark; // The preferred candidate's offset
michael@0 109 int current; // The candidate we're currently looking at
michael@0 110
michael@0 111 public:
michael@0 112 PossibleWord();
michael@0 113 ~PossibleWord();
michael@0 114
michael@0 115 // Fill the list of candidates if needed, select the longest, and return the number found
michael@0 116 int candidates( UText *text, DictionaryMatcher *dict, int32_t rangeEnd );
michael@0 117
michael@0 118 // Select the currently marked candidate, point after it in the text, and invalidate self
michael@0 119 int32_t acceptMarked( UText *text );
michael@0 120
michael@0 121 // Back up from the current candidate to the next shorter one; return TRUE if that exists
michael@0 122 // and point the text after it
michael@0 123 UBool backUp( UText *text );
michael@0 124
michael@0 125 // Return the longest prefix this candidate location shares with a dictionary word
michael@0 126 int32_t longestPrefix();
michael@0 127
michael@0 128 // Mark the current candidate as the one we like
michael@0 129 void markCurrent();
michael@0 130 };
michael@0 131
michael@0 132 inline
michael@0 133 PossibleWord::PossibleWord() {
michael@0 134 offset = -1;
michael@0 135 }
michael@0 136
michael@0 137 inline
michael@0 138 PossibleWord::~PossibleWord() {
michael@0 139 }
michael@0 140
michael@0 141 inline int
michael@0 142 PossibleWord::candidates( UText *text, DictionaryMatcher *dict, int32_t rangeEnd ) {
michael@0 143 // TODO: If getIndex is too slow, use offset < 0 and add discardAll()
michael@0 144 int32_t start = (int32_t)utext_getNativeIndex(text);
michael@0 145 if (start != offset) {
michael@0 146 offset = start;
michael@0 147 prefix = dict->matches(text, rangeEnd-start, lengths, count, sizeof(lengths)/sizeof(lengths[0]));
michael@0 148 // Dictionary leaves text after longest prefix, not longest word. Back up.
michael@0 149 if (count <= 0) {
michael@0 150 utext_setNativeIndex(text, start);
michael@0 151 }
michael@0 152 }
michael@0 153 if (count > 0) {
michael@0 154 utext_setNativeIndex(text, start+lengths[count-1]);
michael@0 155 }
michael@0 156 current = count-1;
michael@0 157 mark = current;
michael@0 158 return count;
michael@0 159 }
michael@0 160
michael@0 161 inline int32_t
michael@0 162 PossibleWord::acceptMarked( UText *text ) {
michael@0 163 utext_setNativeIndex(text, offset + lengths[mark]);
michael@0 164 return lengths[mark];
michael@0 165 }
michael@0 166
michael@0 167 inline UBool
michael@0 168 PossibleWord::backUp( UText *text ) {
michael@0 169 if (current > 0) {
michael@0 170 utext_setNativeIndex(text, offset + lengths[--current]);
michael@0 171 return TRUE;
michael@0 172 }
michael@0 173 return FALSE;
michael@0 174 }
michael@0 175
michael@0 176 inline int32_t
michael@0 177 PossibleWord::longestPrefix() {
michael@0 178 return prefix;
michael@0 179 }
michael@0 180
michael@0 181 inline void
michael@0 182 PossibleWord::markCurrent() {
michael@0 183 mark = current;
michael@0 184 }
michael@0 185
michael@0 186 /*
michael@0 187 ******************************************************************
michael@0 188 * ThaiBreakEngine
michael@0 189 */
michael@0 190
michael@0 191 // How many words in a row are "good enough"?
michael@0 192 #define THAI_LOOKAHEAD 3
michael@0 193
michael@0 194 // Will not combine a non-word with a preceding dictionary word longer than this
michael@0 195 #define THAI_ROOT_COMBINE_THRESHOLD 3
michael@0 196
michael@0 197 // Will not combine a non-word that shares at least this much prefix with a
michael@0 198 // dictionary word, with a preceding word
michael@0 199 #define THAI_PREFIX_COMBINE_THRESHOLD 3
michael@0 200
michael@0 201 // Ellision character
michael@0 202 #define THAI_PAIYANNOI 0x0E2F
michael@0 203
michael@0 204 // Repeat character
michael@0 205 #define THAI_MAIYAMOK 0x0E46
michael@0 206
michael@0 207 // Minimum word size
michael@0 208 #define THAI_MIN_WORD 2
michael@0 209
michael@0 210 // Minimum number of characters for two words
michael@0 211 #define THAI_MIN_WORD_SPAN (THAI_MIN_WORD * 2)
michael@0 212
michael@0 213 ThaiBreakEngine::ThaiBreakEngine(DictionaryMatcher *adoptDictionary, UErrorCode &status)
michael@0 214 : DictionaryBreakEngine((1<<UBRK_WORD) | (1<<UBRK_LINE)),
michael@0 215 fDictionary(adoptDictionary)
michael@0 216 {
michael@0 217 fThaiWordSet.applyPattern(UNICODE_STRING_SIMPLE("[[:Thai:]&[:LineBreak=SA:]]"), status);
michael@0 218 if (U_SUCCESS(status)) {
michael@0 219 setCharacters(fThaiWordSet);
michael@0 220 }
michael@0 221 fMarkSet.applyPattern(UNICODE_STRING_SIMPLE("[[:Thai:]&[:LineBreak=SA:]&[:M:]]"), status);
michael@0 222 fMarkSet.add(0x0020);
michael@0 223 fEndWordSet = fThaiWordSet;
michael@0 224 fEndWordSet.remove(0x0E31); // MAI HAN-AKAT
michael@0 225 fEndWordSet.remove(0x0E40, 0x0E44); // SARA E through SARA AI MAIMALAI
michael@0 226 fBeginWordSet.add(0x0E01, 0x0E2E); // KO KAI through HO NOKHUK
michael@0 227 fBeginWordSet.add(0x0E40, 0x0E44); // SARA E through SARA AI MAIMALAI
michael@0 228 fSuffixSet.add(THAI_PAIYANNOI);
michael@0 229 fSuffixSet.add(THAI_MAIYAMOK);
michael@0 230
michael@0 231 // Compact for caching.
michael@0 232 fMarkSet.compact();
michael@0 233 fEndWordSet.compact();
michael@0 234 fBeginWordSet.compact();
michael@0 235 fSuffixSet.compact();
michael@0 236 }
michael@0 237
michael@0 238 ThaiBreakEngine::~ThaiBreakEngine() {
michael@0 239 delete fDictionary;
michael@0 240 }
michael@0 241
michael@0 242 int32_t
michael@0 243 ThaiBreakEngine::divideUpDictionaryRange( UText *text,
michael@0 244 int32_t rangeStart,
michael@0 245 int32_t rangeEnd,
michael@0 246 UStack &foundBreaks ) const {
michael@0 247 if ((rangeEnd - rangeStart) < THAI_MIN_WORD_SPAN) {
michael@0 248 return 0; // Not enough characters for two words
michael@0 249 }
michael@0 250
michael@0 251 uint32_t wordsFound = 0;
michael@0 252 int32_t wordLength;
michael@0 253 int32_t current;
michael@0 254 UErrorCode status = U_ZERO_ERROR;
michael@0 255 PossibleWord words[THAI_LOOKAHEAD];
michael@0 256 UChar32 uc;
michael@0 257
michael@0 258 utext_setNativeIndex(text, rangeStart);
michael@0 259
michael@0 260 while (U_SUCCESS(status) && (current = (int32_t)utext_getNativeIndex(text)) < rangeEnd) {
michael@0 261 wordLength = 0;
michael@0 262
michael@0 263 // Look for candidate words at the current position
michael@0 264 int candidates = words[wordsFound%THAI_LOOKAHEAD].candidates(text, fDictionary, rangeEnd);
michael@0 265
michael@0 266 // If we found exactly one, use that
michael@0 267 if (candidates == 1) {
michael@0 268 wordLength = words[wordsFound % THAI_LOOKAHEAD].acceptMarked(text);
michael@0 269 wordsFound += 1;
michael@0 270 }
michael@0 271 // If there was more than one, see which one can take us forward the most words
michael@0 272 else if (candidates > 1) {
michael@0 273 // If we're already at the end of the range, we're done
michael@0 274 if ((int32_t)utext_getNativeIndex(text) >= rangeEnd) {
michael@0 275 goto foundBest;
michael@0 276 }
michael@0 277 do {
michael@0 278 int wordsMatched = 1;
michael@0 279 if (words[(wordsFound + 1) % THAI_LOOKAHEAD].candidates(text, fDictionary, rangeEnd) > 0) {
michael@0 280 if (wordsMatched < 2) {
michael@0 281 // Followed by another dictionary word; mark first word as a good candidate
michael@0 282 words[wordsFound%THAI_LOOKAHEAD].markCurrent();
michael@0 283 wordsMatched = 2;
michael@0 284 }
michael@0 285
michael@0 286 // If we're already at the end of the range, we're done
michael@0 287 if ((int32_t)utext_getNativeIndex(text) >= rangeEnd) {
michael@0 288 goto foundBest;
michael@0 289 }
michael@0 290
michael@0 291 // See if any of the possible second words is followed by a third word
michael@0 292 do {
michael@0 293 // If we find a third word, stop right away
michael@0 294 if (words[(wordsFound + 2) % THAI_LOOKAHEAD].candidates(text, fDictionary, rangeEnd)) {
michael@0 295 words[wordsFound % THAI_LOOKAHEAD].markCurrent();
michael@0 296 goto foundBest;
michael@0 297 }
michael@0 298 }
michael@0 299 while (words[(wordsFound + 1) % THAI_LOOKAHEAD].backUp(text));
michael@0 300 }
michael@0 301 }
michael@0 302 while (words[wordsFound % THAI_LOOKAHEAD].backUp(text));
michael@0 303 foundBest:
michael@0 304 wordLength = words[wordsFound % THAI_LOOKAHEAD].acceptMarked(text);
michael@0 305 wordsFound += 1;
michael@0 306 }
michael@0 307
michael@0 308 // We come here after having either found a word or not. We look ahead to the
michael@0 309 // next word. If it's not a dictionary word, we will combine it withe the word we
michael@0 310 // just found (if there is one), but only if the preceding word does not exceed
michael@0 311 // the threshold.
michael@0 312 // The text iterator should now be positioned at the end of the word we found.
michael@0 313 if ((int32_t)utext_getNativeIndex(text) < rangeEnd && wordLength < THAI_ROOT_COMBINE_THRESHOLD) {
michael@0 314 // if it is a dictionary word, do nothing. If it isn't, then if there is
michael@0 315 // no preceding word, or the non-word shares less than the minimum threshold
michael@0 316 // of characters with a dictionary word, then scan to resynchronize
michael@0 317 if (words[wordsFound % THAI_LOOKAHEAD].candidates(text, fDictionary, rangeEnd) <= 0
michael@0 318 && (wordLength == 0
michael@0 319 || words[wordsFound%THAI_LOOKAHEAD].longestPrefix() < THAI_PREFIX_COMBINE_THRESHOLD)) {
michael@0 320 // Look for a plausible word boundary
michael@0 321 //TODO: This section will need a rework for UText.
michael@0 322 int32_t remaining = rangeEnd - (current+wordLength);
michael@0 323 UChar32 pc = utext_current32(text);
michael@0 324 int32_t chars = 0;
michael@0 325 for (;;) {
michael@0 326 utext_next32(text);
michael@0 327 uc = utext_current32(text);
michael@0 328 // TODO: Here we're counting on the fact that the SA languages are all
michael@0 329 // in the BMP. This should get fixed with the UText rework.
michael@0 330 chars += 1;
michael@0 331 if (--remaining <= 0) {
michael@0 332 break;
michael@0 333 }
michael@0 334 if (fEndWordSet.contains(pc) && fBeginWordSet.contains(uc)) {
michael@0 335 // Maybe. See if it's in the dictionary.
michael@0 336 // NOTE: In the original Apple code, checked that the next
michael@0 337 // two characters after uc were not 0x0E4C THANTHAKHAT before
michael@0 338 // checking the dictionary. That is just a performance filter,
michael@0 339 // but it's not clear it's faster than checking the trie.
michael@0 340 int candidates = words[(wordsFound + 1) % THAI_LOOKAHEAD].candidates(text, fDictionary, rangeEnd);
michael@0 341 utext_setNativeIndex(text, current + wordLength + chars);
michael@0 342 if (candidates > 0) {
michael@0 343 break;
michael@0 344 }
michael@0 345 }
michael@0 346 pc = uc;
michael@0 347 }
michael@0 348
michael@0 349 // Bump the word count if there wasn't already one
michael@0 350 if (wordLength <= 0) {
michael@0 351 wordsFound += 1;
michael@0 352 }
michael@0 353
michael@0 354 // Update the length with the passed-over characters
michael@0 355 wordLength += chars;
michael@0 356 }
michael@0 357 else {
michael@0 358 // Back up to where we were for next iteration
michael@0 359 utext_setNativeIndex(text, current+wordLength);
michael@0 360 }
michael@0 361 }
michael@0 362
michael@0 363 // Never stop before a combining mark.
michael@0 364 int32_t currPos;
michael@0 365 while ((currPos = (int32_t)utext_getNativeIndex(text)) < rangeEnd && fMarkSet.contains(utext_current32(text))) {
michael@0 366 utext_next32(text);
michael@0 367 wordLength += (int32_t)utext_getNativeIndex(text) - currPos;
michael@0 368 }
michael@0 369
michael@0 370 // Look ahead for possible suffixes if a dictionary word does not follow.
michael@0 371 // We do this in code rather than using a rule so that the heuristic
michael@0 372 // resynch continues to function. For example, one of the suffix characters
michael@0 373 // could be a typo in the middle of a word.
michael@0 374 if ((int32_t)utext_getNativeIndex(text) < rangeEnd && wordLength > 0) {
michael@0 375 if (words[wordsFound%THAI_LOOKAHEAD].candidates(text, fDictionary, rangeEnd) <= 0
michael@0 376 && fSuffixSet.contains(uc = utext_current32(text))) {
michael@0 377 if (uc == THAI_PAIYANNOI) {
michael@0 378 if (!fSuffixSet.contains(utext_previous32(text))) {
michael@0 379 // Skip over previous end and PAIYANNOI
michael@0 380 utext_next32(text);
michael@0 381 utext_next32(text);
michael@0 382 wordLength += 1; // Add PAIYANNOI to word
michael@0 383 uc = utext_current32(text); // Fetch next character
michael@0 384 }
michael@0 385 else {
michael@0 386 // Restore prior position
michael@0 387 utext_next32(text);
michael@0 388 }
michael@0 389 }
michael@0 390 if (uc == THAI_MAIYAMOK) {
michael@0 391 if (utext_previous32(text) != THAI_MAIYAMOK) {
michael@0 392 // Skip over previous end and MAIYAMOK
michael@0 393 utext_next32(text);
michael@0 394 utext_next32(text);
michael@0 395 wordLength += 1; // Add MAIYAMOK to word
michael@0 396 }
michael@0 397 else {
michael@0 398 // Restore prior position
michael@0 399 utext_next32(text);
michael@0 400 }
michael@0 401 }
michael@0 402 }
michael@0 403 else {
michael@0 404 utext_setNativeIndex(text, current+wordLength);
michael@0 405 }
michael@0 406 }
michael@0 407
michael@0 408 // Did we find a word on this iteration? If so, push it on the break stack
michael@0 409 if (wordLength > 0) {
michael@0 410 foundBreaks.push((current+wordLength), status);
michael@0 411 }
michael@0 412 }
michael@0 413
michael@0 414 // Don't return a break for the end of the dictionary range if there is one there.
michael@0 415 if (foundBreaks.peeki() >= rangeEnd) {
michael@0 416 (void) foundBreaks.popi();
michael@0 417 wordsFound -= 1;
michael@0 418 }
michael@0 419
michael@0 420 return wordsFound;
michael@0 421 }
michael@0 422
michael@0 423 /*
michael@0 424 ******************************************************************
michael@0 425 * LaoBreakEngine
michael@0 426 */
michael@0 427
michael@0 428 // How many words in a row are "good enough"?
michael@0 429 #define LAO_LOOKAHEAD 3
michael@0 430
michael@0 431 // Will not combine a non-word with a preceding dictionary word longer than this
michael@0 432 #define LAO_ROOT_COMBINE_THRESHOLD 3
michael@0 433
michael@0 434 // Will not combine a non-word that shares at least this much prefix with a
michael@0 435 // dictionary word, with a preceding word
michael@0 436 #define LAO_PREFIX_COMBINE_THRESHOLD 3
michael@0 437
michael@0 438 // Minimum word size
michael@0 439 #define LAO_MIN_WORD 2
michael@0 440
michael@0 441 // Minimum number of characters for two words
michael@0 442 #define LAO_MIN_WORD_SPAN (LAO_MIN_WORD * 2)
michael@0 443
michael@0 444 LaoBreakEngine::LaoBreakEngine(DictionaryMatcher *adoptDictionary, UErrorCode &status)
michael@0 445 : DictionaryBreakEngine((1<<UBRK_WORD) | (1<<UBRK_LINE)),
michael@0 446 fDictionary(adoptDictionary)
michael@0 447 {
michael@0 448 fLaoWordSet.applyPattern(UNICODE_STRING_SIMPLE("[[:Laoo:]&[:LineBreak=SA:]]"), status);
michael@0 449 if (U_SUCCESS(status)) {
michael@0 450 setCharacters(fLaoWordSet);
michael@0 451 }
michael@0 452 fMarkSet.applyPattern(UNICODE_STRING_SIMPLE("[[:Laoo:]&[:LineBreak=SA:]&[:M:]]"), status);
michael@0 453 fMarkSet.add(0x0020);
michael@0 454 fEndWordSet = fLaoWordSet;
michael@0 455 fEndWordSet.remove(0x0EC0, 0x0EC4); // prefix vowels
michael@0 456 fBeginWordSet.add(0x0E81, 0x0EAE); // basic consonants (including holes for corresponding Thai characters)
michael@0 457 fBeginWordSet.add(0x0EDC, 0x0EDD); // digraph consonants (no Thai equivalent)
michael@0 458 fBeginWordSet.add(0x0EC0, 0x0EC4); // prefix vowels
michael@0 459
michael@0 460 // Compact for caching.
michael@0 461 fMarkSet.compact();
michael@0 462 fEndWordSet.compact();
michael@0 463 fBeginWordSet.compact();
michael@0 464 }
michael@0 465
michael@0 466 LaoBreakEngine::~LaoBreakEngine() {
michael@0 467 delete fDictionary;
michael@0 468 }
michael@0 469
michael@0 470 int32_t
michael@0 471 LaoBreakEngine::divideUpDictionaryRange( UText *text,
michael@0 472 int32_t rangeStart,
michael@0 473 int32_t rangeEnd,
michael@0 474 UStack &foundBreaks ) const {
michael@0 475 if ((rangeEnd - rangeStart) < LAO_MIN_WORD_SPAN) {
michael@0 476 return 0; // Not enough characters for two words
michael@0 477 }
michael@0 478
michael@0 479 uint32_t wordsFound = 0;
michael@0 480 int32_t wordLength;
michael@0 481 int32_t current;
michael@0 482 UErrorCode status = U_ZERO_ERROR;
michael@0 483 PossibleWord words[LAO_LOOKAHEAD];
michael@0 484 UChar32 uc;
michael@0 485
michael@0 486 utext_setNativeIndex(text, rangeStart);
michael@0 487
michael@0 488 while (U_SUCCESS(status) && (current = (int32_t)utext_getNativeIndex(text)) < rangeEnd) {
michael@0 489 wordLength = 0;
michael@0 490
michael@0 491 // Look for candidate words at the current position
michael@0 492 int candidates = words[wordsFound%LAO_LOOKAHEAD].candidates(text, fDictionary, rangeEnd);
michael@0 493
michael@0 494 // If we found exactly one, use that
michael@0 495 if (candidates == 1) {
michael@0 496 wordLength = words[wordsFound % LAO_LOOKAHEAD].acceptMarked(text);
michael@0 497 wordsFound += 1;
michael@0 498 }
michael@0 499 // If there was more than one, see which one can take us forward the most words
michael@0 500 else if (candidates > 1) {
michael@0 501 // If we're already at the end of the range, we're done
michael@0 502 if ((int32_t)utext_getNativeIndex(text) >= rangeEnd) {
michael@0 503 goto foundBest;
michael@0 504 }
michael@0 505 do {
michael@0 506 int wordsMatched = 1;
michael@0 507 if (words[(wordsFound + 1) % LAO_LOOKAHEAD].candidates(text, fDictionary, rangeEnd) > 0) {
michael@0 508 if (wordsMatched < 2) {
michael@0 509 // Followed by another dictionary word; mark first word as a good candidate
michael@0 510 words[wordsFound%LAO_LOOKAHEAD].markCurrent();
michael@0 511 wordsMatched = 2;
michael@0 512 }
michael@0 513
michael@0 514 // If we're already at the end of the range, we're done
michael@0 515 if ((int32_t)utext_getNativeIndex(text) >= rangeEnd) {
michael@0 516 goto foundBest;
michael@0 517 }
michael@0 518
michael@0 519 // See if any of the possible second words is followed by a third word
michael@0 520 do {
michael@0 521 // If we find a third word, stop right away
michael@0 522 if (words[(wordsFound + 2) % LAO_LOOKAHEAD].candidates(text, fDictionary, rangeEnd)) {
michael@0 523 words[wordsFound % LAO_LOOKAHEAD].markCurrent();
michael@0 524 goto foundBest;
michael@0 525 }
michael@0 526 }
michael@0 527 while (words[(wordsFound + 1) % LAO_LOOKAHEAD].backUp(text));
michael@0 528 }
michael@0 529 }
michael@0 530 while (words[wordsFound % LAO_LOOKAHEAD].backUp(text));
michael@0 531 foundBest:
michael@0 532 wordLength = words[wordsFound % LAO_LOOKAHEAD].acceptMarked(text);
michael@0 533 wordsFound += 1;
michael@0 534 }
michael@0 535
michael@0 536 // We come here after having either found a word or not. We look ahead to the
michael@0 537 // next word. If it's not a dictionary word, we will combine it withe the word we
michael@0 538 // just found (if there is one), but only if the preceding word does not exceed
michael@0 539 // the threshold.
michael@0 540 // The text iterator should now be positioned at the end of the word we found.
michael@0 541 if ((int32_t)utext_getNativeIndex(text) < rangeEnd && wordLength < LAO_ROOT_COMBINE_THRESHOLD) {
michael@0 542 // if it is a dictionary word, do nothing. If it isn't, then if there is
michael@0 543 // no preceding word, or the non-word shares less than the minimum threshold
michael@0 544 // of characters with a dictionary word, then scan to resynchronize
michael@0 545 if (words[wordsFound % LAO_LOOKAHEAD].candidates(text, fDictionary, rangeEnd) <= 0
michael@0 546 && (wordLength == 0
michael@0 547 || words[wordsFound%LAO_LOOKAHEAD].longestPrefix() < LAO_PREFIX_COMBINE_THRESHOLD)) {
michael@0 548 // Look for a plausible word boundary
michael@0 549 //TODO: This section will need a rework for UText.
michael@0 550 int32_t remaining = rangeEnd - (current+wordLength);
michael@0 551 UChar32 pc = utext_current32(text);
michael@0 552 int32_t chars = 0;
michael@0 553 for (;;) {
michael@0 554 utext_next32(text);
michael@0 555 uc = utext_current32(text);
michael@0 556 // TODO: Here we're counting on the fact that the SA languages are all
michael@0 557 // in the BMP. This should get fixed with the UText rework.
michael@0 558 chars += 1;
michael@0 559 if (--remaining <= 0) {
michael@0 560 break;
michael@0 561 }
michael@0 562 if (fEndWordSet.contains(pc) && fBeginWordSet.contains(uc)) {
michael@0 563 // Maybe. See if it's in the dictionary.
michael@0 564 int candidates = words[(wordsFound + 1) % LAO_LOOKAHEAD].candidates(text, fDictionary, rangeEnd);
michael@0 565 utext_setNativeIndex(text, current + wordLength + chars);
michael@0 566 if (candidates > 0) {
michael@0 567 break;
michael@0 568 }
michael@0 569 }
michael@0 570 pc = uc;
michael@0 571 }
michael@0 572
michael@0 573 // Bump the word count if there wasn't already one
michael@0 574 if (wordLength <= 0) {
michael@0 575 wordsFound += 1;
michael@0 576 }
michael@0 577
michael@0 578 // Update the length with the passed-over characters
michael@0 579 wordLength += chars;
michael@0 580 }
michael@0 581 else {
michael@0 582 // Back up to where we were for next iteration
michael@0 583 utext_setNativeIndex(text, current+wordLength);
michael@0 584 }
michael@0 585 }
michael@0 586
michael@0 587 // Never stop before a combining mark.
michael@0 588 int32_t currPos;
michael@0 589 while ((currPos = (int32_t)utext_getNativeIndex(text)) < rangeEnd && fMarkSet.contains(utext_current32(text))) {
michael@0 590 utext_next32(text);
michael@0 591 wordLength += (int32_t)utext_getNativeIndex(text) - currPos;
michael@0 592 }
michael@0 593
michael@0 594 // Look ahead for possible suffixes if a dictionary word does not follow.
michael@0 595 // We do this in code rather than using a rule so that the heuristic
michael@0 596 // resynch continues to function. For example, one of the suffix characters
michael@0 597 // could be a typo in the middle of a word.
michael@0 598 // NOT CURRENTLY APPLICABLE TO LAO
michael@0 599
michael@0 600 // Did we find a word on this iteration? If so, push it on the break stack
michael@0 601 if (wordLength > 0) {
michael@0 602 foundBreaks.push((current+wordLength), status);
michael@0 603 }
michael@0 604 }
michael@0 605
michael@0 606 // Don't return a break for the end of the dictionary range if there is one there.
michael@0 607 if (foundBreaks.peeki() >= rangeEnd) {
michael@0 608 (void) foundBreaks.popi();
michael@0 609 wordsFound -= 1;
michael@0 610 }
michael@0 611
michael@0 612 return wordsFound;
michael@0 613 }
michael@0 614
michael@0 615 /*
michael@0 616 ******************************************************************
michael@0 617 * KhmerBreakEngine
michael@0 618 */
michael@0 619
michael@0 620 // How many words in a row are "good enough"?
michael@0 621 #define KHMER_LOOKAHEAD 3
michael@0 622
michael@0 623 // Will not combine a non-word with a preceding dictionary word longer than this
michael@0 624 #define KHMER_ROOT_COMBINE_THRESHOLD 3
michael@0 625
michael@0 626 // Will not combine a non-word that shares at least this much prefix with a
michael@0 627 // dictionary word, with a preceding word
michael@0 628 #define KHMER_PREFIX_COMBINE_THRESHOLD 3
michael@0 629
michael@0 630 // Minimum word size
michael@0 631 #define KHMER_MIN_WORD 2
michael@0 632
michael@0 633 // Minimum number of characters for two words
michael@0 634 #define KHMER_MIN_WORD_SPAN (KHMER_MIN_WORD * 2)
michael@0 635
michael@0 636 KhmerBreakEngine::KhmerBreakEngine(DictionaryMatcher *adoptDictionary, UErrorCode &status)
michael@0 637 : DictionaryBreakEngine((1 << UBRK_WORD) | (1 << UBRK_LINE)),
michael@0 638 fDictionary(adoptDictionary)
michael@0 639 {
michael@0 640 fKhmerWordSet.applyPattern(UNICODE_STRING_SIMPLE("[[:Khmr:]&[:LineBreak=SA:]]"), status);
michael@0 641 if (U_SUCCESS(status)) {
michael@0 642 setCharacters(fKhmerWordSet);
michael@0 643 }
michael@0 644 fMarkSet.applyPattern(UNICODE_STRING_SIMPLE("[[:Khmr:]&[:LineBreak=SA:]&[:M:]]"), status);
michael@0 645 fMarkSet.add(0x0020);
michael@0 646 fEndWordSet = fKhmerWordSet;
michael@0 647 fBeginWordSet.add(0x1780, 0x17B3);
michael@0 648 //fBeginWordSet.add(0x17A3, 0x17A4); // deprecated vowels
michael@0 649 //fEndWordSet.remove(0x17A5, 0x17A9); // Khmer independent vowels that can't end a word
michael@0 650 //fEndWordSet.remove(0x17B2); // Khmer independent vowel that can't end a word
michael@0 651 fEndWordSet.remove(0x17D2); // KHMER SIGN COENG that combines some following characters
michael@0 652 //fEndWordSet.remove(0x17B6, 0x17C5); // Remove dependent vowels
michael@0 653 // fEndWordSet.remove(0x0E31); // MAI HAN-AKAT
michael@0 654 // fEndWordSet.remove(0x0E40, 0x0E44); // SARA E through SARA AI MAIMALAI
michael@0 655 // fBeginWordSet.add(0x0E01, 0x0E2E); // KO KAI through HO NOKHUK
michael@0 656 // fBeginWordSet.add(0x0E40, 0x0E44); // SARA E through SARA AI MAIMALAI
michael@0 657 // fSuffixSet.add(THAI_PAIYANNOI);
michael@0 658 // fSuffixSet.add(THAI_MAIYAMOK);
michael@0 659
michael@0 660 // Compact for caching.
michael@0 661 fMarkSet.compact();
michael@0 662 fEndWordSet.compact();
michael@0 663 fBeginWordSet.compact();
michael@0 664 // fSuffixSet.compact();
michael@0 665 }
michael@0 666
michael@0 667 KhmerBreakEngine::~KhmerBreakEngine() {
michael@0 668 delete fDictionary;
michael@0 669 }
michael@0 670
michael@0 671 int32_t
michael@0 672 KhmerBreakEngine::divideUpDictionaryRange( UText *text,
michael@0 673 int32_t rangeStart,
michael@0 674 int32_t rangeEnd,
michael@0 675 UStack &foundBreaks ) const {
michael@0 676 if ((rangeEnd - rangeStart) < KHMER_MIN_WORD_SPAN) {
michael@0 677 return 0; // Not enough characters for two words
michael@0 678 }
michael@0 679
michael@0 680 uint32_t wordsFound = 0;
michael@0 681 int32_t wordLength;
michael@0 682 int32_t current;
michael@0 683 UErrorCode status = U_ZERO_ERROR;
michael@0 684 PossibleWord words[KHMER_LOOKAHEAD];
michael@0 685 UChar32 uc;
michael@0 686
michael@0 687 utext_setNativeIndex(text, rangeStart);
michael@0 688
michael@0 689 while (U_SUCCESS(status) && (current = (int32_t)utext_getNativeIndex(text)) < rangeEnd) {
michael@0 690 wordLength = 0;
michael@0 691
michael@0 692 // Look for candidate words at the current position
michael@0 693 int candidates = words[wordsFound%KHMER_LOOKAHEAD].candidates(text, fDictionary, rangeEnd);
michael@0 694
michael@0 695 // If we found exactly one, use that
michael@0 696 if (candidates == 1) {
michael@0 697 wordLength = words[wordsFound%KHMER_LOOKAHEAD].acceptMarked(text);
michael@0 698 wordsFound += 1;
michael@0 699 }
michael@0 700
michael@0 701 // If there was more than one, see which one can take us forward the most words
michael@0 702 else if (candidates > 1) {
michael@0 703 // If we're already at the end of the range, we're done
michael@0 704 if ((int32_t)utext_getNativeIndex(text) >= rangeEnd) {
michael@0 705 goto foundBest;
michael@0 706 }
michael@0 707 do {
michael@0 708 int wordsMatched = 1;
michael@0 709 if (words[(wordsFound + 1) % KHMER_LOOKAHEAD].candidates(text, fDictionary, rangeEnd) > 0) {
michael@0 710 if (wordsMatched < 2) {
michael@0 711 // Followed by another dictionary word; mark first word as a good candidate
michael@0 712 words[wordsFound % KHMER_LOOKAHEAD].markCurrent();
michael@0 713 wordsMatched = 2;
michael@0 714 }
michael@0 715
michael@0 716 // If we're already at the end of the range, we're done
michael@0 717 if ((int32_t)utext_getNativeIndex(text) >= rangeEnd) {
michael@0 718 goto foundBest;
michael@0 719 }
michael@0 720
michael@0 721 // See if any of the possible second words is followed by a third word
michael@0 722 do {
michael@0 723 // If we find a third word, stop right away
michael@0 724 if (words[(wordsFound + 2) % KHMER_LOOKAHEAD].candidates(text, fDictionary, rangeEnd)) {
michael@0 725 words[wordsFound % KHMER_LOOKAHEAD].markCurrent();
michael@0 726 goto foundBest;
michael@0 727 }
michael@0 728 }
michael@0 729 while (words[(wordsFound + 1) % KHMER_LOOKAHEAD].backUp(text));
michael@0 730 }
michael@0 731 }
michael@0 732 while (words[wordsFound % KHMER_LOOKAHEAD].backUp(text));
michael@0 733 foundBest:
michael@0 734 wordLength = words[wordsFound % KHMER_LOOKAHEAD].acceptMarked(text);
michael@0 735 wordsFound += 1;
michael@0 736 }
michael@0 737
michael@0 738 // We come here after having either found a word or not. We look ahead to the
michael@0 739 // next word. If it's not a dictionary word, we will combine it with the word we
michael@0 740 // just found (if there is one), but only if the preceding word does not exceed
michael@0 741 // the threshold.
michael@0 742 // The text iterator should now be positioned at the end of the word we found.
michael@0 743 if ((int32_t)utext_getNativeIndex(text) < rangeEnd && wordLength < KHMER_ROOT_COMBINE_THRESHOLD) {
michael@0 744 // if it is a dictionary word, do nothing. If it isn't, then if there is
michael@0 745 // no preceding word, or the non-word shares less than the minimum threshold
michael@0 746 // of characters with a dictionary word, then scan to resynchronize
michael@0 747 if (words[wordsFound % KHMER_LOOKAHEAD].candidates(text, fDictionary, rangeEnd) <= 0
michael@0 748 && (wordLength == 0
michael@0 749 || words[wordsFound % KHMER_LOOKAHEAD].longestPrefix() < KHMER_PREFIX_COMBINE_THRESHOLD)) {
michael@0 750 // Look for a plausible word boundary
michael@0 751 //TODO: This section will need a rework for UText.
michael@0 752 int32_t remaining = rangeEnd - (current+wordLength);
michael@0 753 UChar32 pc = utext_current32(text);
michael@0 754 int32_t chars = 0;
michael@0 755 for (;;) {
michael@0 756 utext_next32(text);
michael@0 757 uc = utext_current32(text);
michael@0 758 // TODO: Here we're counting on the fact that the SA languages are all
michael@0 759 // in the BMP. This should get fixed with the UText rework.
michael@0 760 chars += 1;
michael@0 761 if (--remaining <= 0) {
michael@0 762 break;
michael@0 763 }
michael@0 764 if (fEndWordSet.contains(pc) && fBeginWordSet.contains(uc)) {
michael@0 765 // Maybe. See if it's in the dictionary.
michael@0 766 int candidates = words[(wordsFound + 1) % KHMER_LOOKAHEAD].candidates(text, fDictionary, rangeEnd);
michael@0 767 utext_setNativeIndex(text, current+wordLength+chars);
michael@0 768 if (candidates > 0) {
michael@0 769 break;
michael@0 770 }
michael@0 771 }
michael@0 772 pc = uc;
michael@0 773 }
michael@0 774
michael@0 775 // Bump the word count if there wasn't already one
michael@0 776 if (wordLength <= 0) {
michael@0 777 wordsFound += 1;
michael@0 778 }
michael@0 779
michael@0 780 // Update the length with the passed-over characters
michael@0 781 wordLength += chars;
michael@0 782 }
michael@0 783 else {
michael@0 784 // Back up to where we were for next iteration
michael@0 785 utext_setNativeIndex(text, current+wordLength);
michael@0 786 }
michael@0 787 }
michael@0 788
michael@0 789 // Never stop before a combining mark.
michael@0 790 int32_t currPos;
michael@0 791 while ((currPos = (int32_t)utext_getNativeIndex(text)) < rangeEnd && fMarkSet.contains(utext_current32(text))) {
michael@0 792 utext_next32(text);
michael@0 793 wordLength += (int32_t)utext_getNativeIndex(text) - currPos;
michael@0 794 }
michael@0 795
michael@0 796 // Look ahead for possible suffixes if a dictionary word does not follow.
michael@0 797 // We do this in code rather than using a rule so that the heuristic
michael@0 798 // resynch continues to function. For example, one of the suffix characters
michael@0 799 // could be a typo in the middle of a word.
michael@0 800 // if ((int32_t)utext_getNativeIndex(text) < rangeEnd && wordLength > 0) {
michael@0 801 // if (words[wordsFound%KHMER_LOOKAHEAD].candidates(text, fDictionary, rangeEnd) <= 0
michael@0 802 // && fSuffixSet.contains(uc = utext_current32(text))) {
michael@0 803 // if (uc == KHMER_PAIYANNOI) {
michael@0 804 // if (!fSuffixSet.contains(utext_previous32(text))) {
michael@0 805 // // Skip over previous end and PAIYANNOI
michael@0 806 // utext_next32(text);
michael@0 807 // utext_next32(text);
michael@0 808 // wordLength += 1; // Add PAIYANNOI to word
michael@0 809 // uc = utext_current32(text); // Fetch next character
michael@0 810 // }
michael@0 811 // else {
michael@0 812 // // Restore prior position
michael@0 813 // utext_next32(text);
michael@0 814 // }
michael@0 815 // }
michael@0 816 // if (uc == KHMER_MAIYAMOK) {
michael@0 817 // if (utext_previous32(text) != KHMER_MAIYAMOK) {
michael@0 818 // // Skip over previous end and MAIYAMOK
michael@0 819 // utext_next32(text);
michael@0 820 // utext_next32(text);
michael@0 821 // wordLength += 1; // Add MAIYAMOK to word
michael@0 822 // }
michael@0 823 // else {
michael@0 824 // // Restore prior position
michael@0 825 // utext_next32(text);
michael@0 826 // }
michael@0 827 // }
michael@0 828 // }
michael@0 829 // else {
michael@0 830 // utext_setNativeIndex(text, current+wordLength);
michael@0 831 // }
michael@0 832 // }
michael@0 833
michael@0 834 // Did we find a word on this iteration? If so, push it on the break stack
michael@0 835 if (wordLength > 0) {
michael@0 836 foundBreaks.push((current+wordLength), status);
michael@0 837 }
michael@0 838 }
michael@0 839
michael@0 840 // Don't return a break for the end of the dictionary range if there is one there.
michael@0 841 if (foundBreaks.peeki() >= rangeEnd) {
michael@0 842 (void) foundBreaks.popi();
michael@0 843 wordsFound -= 1;
michael@0 844 }
michael@0 845
michael@0 846 return wordsFound;
michael@0 847 }
michael@0 848
michael@0 849 #if !UCONFIG_NO_NORMALIZATION
michael@0 850 /*
michael@0 851 ******************************************************************
michael@0 852 * CjkBreakEngine
michael@0 853 */
michael@0 854 static const uint32_t kuint32max = 0xFFFFFFFF;
michael@0 855 CjkBreakEngine::CjkBreakEngine(DictionaryMatcher *adoptDictionary, LanguageType type, UErrorCode &status)
michael@0 856 : DictionaryBreakEngine(1 << UBRK_WORD), fDictionary(adoptDictionary) {
michael@0 857 // Korean dictionary only includes Hangul syllables
michael@0 858 fHangulWordSet.applyPattern(UNICODE_STRING_SIMPLE("[\\uac00-\\ud7a3]"), status);
michael@0 859 fHanWordSet.applyPattern(UNICODE_STRING_SIMPLE("[:Han:]"), status);
michael@0 860 fKatakanaWordSet.applyPattern(UNICODE_STRING_SIMPLE("[[:Katakana:]\\uff9e\\uff9f]"), status);
michael@0 861 fHiraganaWordSet.applyPattern(UNICODE_STRING_SIMPLE("[:Hiragana:]"), status);
michael@0 862
michael@0 863 if (U_SUCCESS(status)) {
michael@0 864 // handle Korean and Japanese/Chinese using different dictionaries
michael@0 865 if (type == kKorean) {
michael@0 866 setCharacters(fHangulWordSet);
michael@0 867 } else { //Chinese and Japanese
michael@0 868 UnicodeSet cjSet;
michael@0 869 cjSet.addAll(fHanWordSet);
michael@0 870 cjSet.addAll(fKatakanaWordSet);
michael@0 871 cjSet.addAll(fHiraganaWordSet);
michael@0 872 cjSet.add(0xFF70); // HALFWIDTH KATAKANA-HIRAGANA PROLONGED SOUND MARK
michael@0 873 cjSet.add(0x30FC); // KATAKANA-HIRAGANA PROLONGED SOUND MARK
michael@0 874 setCharacters(cjSet);
michael@0 875 }
michael@0 876 }
michael@0 877 }
michael@0 878
michael@0 879 CjkBreakEngine::~CjkBreakEngine(){
michael@0 880 delete fDictionary;
michael@0 881 }
michael@0 882
michael@0 883 // The katakanaCost values below are based on the length frequencies of all
michael@0 884 // katakana phrases in the dictionary
michael@0 885 static const int kMaxKatakanaLength = 8;
michael@0 886 static const int kMaxKatakanaGroupLength = 20;
michael@0 887 static const uint32_t maxSnlp = 255;
michael@0 888
michael@0 889 static inline uint32_t getKatakanaCost(int wordLength){
michael@0 890 //TODO: fill array with actual values from dictionary!
michael@0 891 static const uint32_t katakanaCost[kMaxKatakanaLength + 1]
michael@0 892 = {8192, 984, 408, 240, 204, 252, 300, 372, 480};
michael@0 893 return (wordLength > kMaxKatakanaLength) ? 8192 : katakanaCost[wordLength];
michael@0 894 }
michael@0 895
michael@0 896 static inline bool isKatakana(uint16_t value) {
michael@0 897 return (value >= 0x30A1u && value <= 0x30FEu && value != 0x30FBu) ||
michael@0 898 (value >= 0xFF66u && value <= 0xFF9fu);
michael@0 899 }
michael@0 900
michael@0 901 // A very simple helper class to streamline the buffer handling in
michael@0 902 // divideUpDictionaryRange.
michael@0 903 template<class T, size_t N>
michael@0 904 class AutoBuffer {
michael@0 905 public:
michael@0 906 AutoBuffer(size_t size) : buffer(stackBuffer), capacity(N) {
michael@0 907 if (size > N) {
michael@0 908 buffer = reinterpret_cast<T*>(uprv_malloc(sizeof(T)*size));
michael@0 909 capacity = size;
michael@0 910 }
michael@0 911 }
michael@0 912 ~AutoBuffer() {
michael@0 913 if (buffer != stackBuffer)
michael@0 914 uprv_free(buffer);
michael@0 915 }
michael@0 916
michael@0 917 T* elems() {
michael@0 918 return buffer;
michael@0 919 }
michael@0 920
michael@0 921 const T& operator[] (size_t i) const {
michael@0 922 return buffer[i];
michael@0 923 }
michael@0 924
michael@0 925 T& operator[] (size_t i) {
michael@0 926 return buffer[i];
michael@0 927 }
michael@0 928
michael@0 929 // resize without copy
michael@0 930 void resize(size_t size) {
michael@0 931 if (size <= capacity)
michael@0 932 return;
michael@0 933 if (buffer != stackBuffer)
michael@0 934 uprv_free(buffer);
michael@0 935 buffer = reinterpret_cast<T*>(uprv_malloc(sizeof(T)*size));
michael@0 936 capacity = size;
michael@0 937 }
michael@0 938
michael@0 939 private:
michael@0 940 T stackBuffer[N];
michael@0 941 T* buffer;
michael@0 942 AutoBuffer();
michael@0 943 size_t capacity;
michael@0 944 };
michael@0 945
michael@0 946
michael@0 947 /*
michael@0 948 * @param text A UText representing the text
michael@0 949 * @param rangeStart The start of the range of dictionary characters
michael@0 950 * @param rangeEnd The end of the range of dictionary characters
michael@0 951 * @param foundBreaks Output of C array of int32_t break positions, or 0
michael@0 952 * @return The number of breaks found
michael@0 953 */
michael@0 954 int32_t
michael@0 955 CjkBreakEngine::divideUpDictionaryRange( UText *text,
michael@0 956 int32_t rangeStart,
michael@0 957 int32_t rangeEnd,
michael@0 958 UStack &foundBreaks ) const {
michael@0 959 if (rangeStart >= rangeEnd) {
michael@0 960 return 0;
michael@0 961 }
michael@0 962
michael@0 963 const size_t defaultInputLength = 80;
michael@0 964 size_t inputLength = rangeEnd - rangeStart;
michael@0 965 // TODO: Replace by UnicodeString.
michael@0 966 AutoBuffer<UChar, defaultInputLength> charString(inputLength);
michael@0 967
michael@0 968 // Normalize the input string and put it in normalizedText.
michael@0 969 // The map from the indices of the normalized input to the raw
michael@0 970 // input is kept in charPositions.
michael@0 971 UErrorCode status = U_ZERO_ERROR;
michael@0 972 utext_extract(text, rangeStart, rangeEnd, charString.elems(), inputLength, &status);
michael@0 973 if (U_FAILURE(status)) {
michael@0 974 return 0;
michael@0 975 }
michael@0 976
michael@0 977 UnicodeString inputString(charString.elems(), inputLength);
michael@0 978 // TODO: Use Normalizer2.
michael@0 979 UNormalizationMode norm_mode = UNORM_NFKC;
michael@0 980 UBool isNormalized =
michael@0 981 Normalizer::quickCheck(inputString, norm_mode, status) == UNORM_YES ||
michael@0 982 Normalizer::isNormalized(inputString, norm_mode, status);
michael@0 983
michael@0 984 // TODO: Replace by UVector32.
michael@0 985 AutoBuffer<int32_t, defaultInputLength> charPositions(inputLength + 1);
michael@0 986 int numChars = 0;
michael@0 987 UText normalizedText = UTEXT_INITIALIZER;
michael@0 988 // Needs to be declared here because normalizedText holds onto its buffer.
michael@0 989 UnicodeString normalizedString;
michael@0 990 if (isNormalized) {
michael@0 991 int32_t index = 0;
michael@0 992 charPositions[0] = 0;
michael@0 993 while(index < inputString.length()) {
michael@0 994 index = inputString.moveIndex32(index, 1);
michael@0 995 charPositions[++numChars] = index;
michael@0 996 }
michael@0 997 utext_openUnicodeString(&normalizedText, &inputString, &status);
michael@0 998 }
michael@0 999 else {
michael@0 1000 Normalizer::normalize(inputString, norm_mode, 0, normalizedString, status);
michael@0 1001 if (U_FAILURE(status)) {
michael@0 1002 return 0;
michael@0 1003 }
michael@0 1004 charPositions.resize(normalizedString.length() + 1);
michael@0 1005 Normalizer normalizer(charString.elems(), inputLength, norm_mode);
michael@0 1006 int32_t index = 0;
michael@0 1007 charPositions[0] = 0;
michael@0 1008 while(index < normalizer.endIndex()){
michael@0 1009 /* UChar32 uc = */ normalizer.next();
michael@0 1010 charPositions[++numChars] = index = normalizer.getIndex();
michael@0 1011 }
michael@0 1012 utext_openUnicodeString(&normalizedText, &normalizedString, &status);
michael@0 1013 }
michael@0 1014
michael@0 1015 if (U_FAILURE(status)) {
michael@0 1016 return 0;
michael@0 1017 }
michael@0 1018
michael@0 1019 // From this point on, all the indices refer to the indices of
michael@0 1020 // the normalized input string.
michael@0 1021
michael@0 1022 // bestSnlp[i] is the snlp of the best segmentation of the first i
michael@0 1023 // characters in the range to be matched.
michael@0 1024 // TODO: Replace by UVector32.
michael@0 1025 AutoBuffer<uint32_t, defaultInputLength> bestSnlp(numChars + 1);
michael@0 1026 bestSnlp[0] = 0;
michael@0 1027 for(int i = 1; i <= numChars; i++) {
michael@0 1028 bestSnlp[i] = kuint32max;
michael@0 1029 }
michael@0 1030
michael@0 1031 // prev[i] is the index of the last CJK character in the previous word in
michael@0 1032 // the best segmentation of the first i characters.
michael@0 1033 // TODO: Replace by UVector32.
michael@0 1034 AutoBuffer<int, defaultInputLength> prev(numChars + 1);
michael@0 1035 for(int i = 0; i <= numChars; i++){
michael@0 1036 prev[i] = -1;
michael@0 1037 }
michael@0 1038
michael@0 1039 const size_t maxWordSize = 20;
michael@0 1040 // TODO: Replace both with UVector32.
michael@0 1041 AutoBuffer<int32_t, maxWordSize> values(numChars);
michael@0 1042 AutoBuffer<int32_t, maxWordSize> lengths(numChars);
michael@0 1043
michael@0 1044 // Dynamic programming to find the best segmentation.
michael@0 1045 bool is_prev_katakana = false;
michael@0 1046 for (int32_t i = 0; i < numChars; ++i) {
michael@0 1047 //utext_setNativeIndex(text, rangeStart + i);
michael@0 1048 utext_setNativeIndex(&normalizedText, i);
michael@0 1049 if (bestSnlp[i] == kuint32max)
michael@0 1050 continue;
michael@0 1051
michael@0 1052 int32_t count;
michael@0 1053 // limit maximum word length matched to size of current substring
michael@0 1054 int32_t maxSearchLength = (i + maxWordSize < (size_t) numChars)? maxWordSize : (numChars - i);
michael@0 1055
michael@0 1056 fDictionary->matches(&normalizedText, maxSearchLength, lengths.elems(), count, maxSearchLength, values.elems());
michael@0 1057
michael@0 1058 // if there are no single character matches found in the dictionary
michael@0 1059 // starting with this charcter, treat character as a 1-character word
michael@0 1060 // with the highest value possible, i.e. the least likely to occur.
michael@0 1061 // Exclude Korean characters from this treatment, as they should be left
michael@0 1062 // together by default.
michael@0 1063 if((count == 0 || lengths[0] != 1) &&
michael@0 1064 !fHangulWordSet.contains(utext_current32(&normalizedText))) {
michael@0 1065 values[count] = maxSnlp;
michael@0 1066 lengths[count++] = 1;
michael@0 1067 }
michael@0 1068
michael@0 1069 for (int j = 0; j < count; j++) {
michael@0 1070 uint32_t newSnlp = bestSnlp[i] + values[j];
michael@0 1071 if (newSnlp < bestSnlp[lengths[j] + i]) {
michael@0 1072 bestSnlp[lengths[j] + i] = newSnlp;
michael@0 1073 prev[lengths[j] + i] = i;
michael@0 1074 }
michael@0 1075 }
michael@0 1076
michael@0 1077 // In Japanese,
michael@0 1078 // Katakana word in single character is pretty rare. So we apply
michael@0 1079 // the following heuristic to Katakana: any continuous run of Katakana
michael@0 1080 // characters is considered a candidate word with a default cost
michael@0 1081 // specified in the katakanaCost table according to its length.
michael@0 1082 //utext_setNativeIndex(text, rangeStart + i);
michael@0 1083 utext_setNativeIndex(&normalizedText, i);
michael@0 1084 bool is_katakana = isKatakana(utext_current32(&normalizedText));
michael@0 1085 if (!is_prev_katakana && is_katakana) {
michael@0 1086 int j = i + 1;
michael@0 1087 utext_next32(&normalizedText);
michael@0 1088 // Find the end of the continuous run of Katakana characters
michael@0 1089 while (j < numChars && (j - i) < kMaxKatakanaGroupLength &&
michael@0 1090 isKatakana(utext_current32(&normalizedText))) {
michael@0 1091 utext_next32(&normalizedText);
michael@0 1092 ++j;
michael@0 1093 }
michael@0 1094 if ((j - i) < kMaxKatakanaGroupLength) {
michael@0 1095 uint32_t newSnlp = bestSnlp[i] + getKatakanaCost(j - i);
michael@0 1096 if (newSnlp < bestSnlp[j]) {
michael@0 1097 bestSnlp[j] = newSnlp;
michael@0 1098 prev[j] = i;
michael@0 1099 }
michael@0 1100 }
michael@0 1101 }
michael@0 1102 is_prev_katakana = is_katakana;
michael@0 1103 }
michael@0 1104
michael@0 1105 // Start pushing the optimal offset index into t_boundary (t for tentative).
michael@0 1106 // prev[numChars] is guaranteed to be meaningful.
michael@0 1107 // We'll first push in the reverse order, i.e.,
michael@0 1108 // t_boundary[0] = numChars, and afterwards do a swap.
michael@0 1109 // TODO: Replace by UVector32.
michael@0 1110 AutoBuffer<int, maxWordSize> t_boundary(numChars + 1);
michael@0 1111
michael@0 1112 int numBreaks = 0;
michael@0 1113 // No segmentation found, set boundary to end of range
michael@0 1114 if (bestSnlp[numChars] == kuint32max) {
michael@0 1115 t_boundary[numBreaks++] = numChars;
michael@0 1116 } else {
michael@0 1117 for (int i = numChars; i > 0; i = prev[i]) {
michael@0 1118 t_boundary[numBreaks++] = i;
michael@0 1119 }
michael@0 1120 U_ASSERT(prev[t_boundary[numBreaks - 1]] == 0);
michael@0 1121 }
michael@0 1122
michael@0 1123 // Reverse offset index in t_boundary.
michael@0 1124 // Don't add a break for the start of the dictionary range if there is one
michael@0 1125 // there already.
michael@0 1126 if (foundBreaks.size() == 0 || foundBreaks.peeki() < rangeStart) {
michael@0 1127 t_boundary[numBreaks++] = 0;
michael@0 1128 }
michael@0 1129
michael@0 1130 // Now that we're done, convert positions in t_bdry[] (indices in
michael@0 1131 // the normalized input string) back to indices in the raw input string
michael@0 1132 // while reversing t_bdry and pushing values to foundBreaks.
michael@0 1133 for (int i = numBreaks-1; i >= 0; i--) {
michael@0 1134 foundBreaks.push(charPositions[t_boundary[i]] + rangeStart, status);
michael@0 1135 }
michael@0 1136
michael@0 1137 utext_close(&normalizedText);
michael@0 1138 return numBreaks;
michael@0 1139 }
michael@0 1140 #endif
michael@0 1141
michael@0 1142 U_NAMESPACE_END
michael@0 1143
michael@0 1144 #endif /* #if !UCONFIG_NO_BREAK_ITERATION */
michael@0 1145

mercurial