1 // © 2016 and later: Unicode, Inc. and others.
2 // License & terms of use: http://www.unicode.org/copyright.html
4 *******************************************************************************
5 * Copyright (C) 2006-2016, International Business Machines Corporation
6 * and others. All Rights Reserved.
7 *******************************************************************************
10 #include "unicode/utypes.h"
12 #if !UCONFIG_NO_BREAK_ITERATION
16 #include "unicode/uniset.h"
17 #include "unicode/chariter.h"
18 #include "unicode/ubrk.h"
22 #include "unicode/normlzr.h"
24 #include "dictionarydata.h"
29 ******************************************************************
32 DictionaryBreakEngine::DictionaryBreakEngine(uint32_t breakTypes
) {
36 DictionaryBreakEngine::~DictionaryBreakEngine() {
40 DictionaryBreakEngine::handles(UChar32 c
, int32_t breakType
) const {
41 return (breakType
>= 0 && breakType
< 32 && (((uint32_t)1 << breakType
) & fTypes
)
46 DictionaryBreakEngine::findBreaks( UText
*text
,
51 UStack
&foundBreaks
) const {
54 // Find the span of characters included in the set.
55 // The span to break begins at the current position in the text, and
56 // extends towards the start or end of the text, depending on 'reverse'.
58 int32_t start
= (int32_t)utext_getNativeIndex(text
);
62 UChar32 c
= utext_current32(text
);
64 UBool isDict
= fSet
.contains(c
);
65 while((current
= (int32_t)utext_getNativeIndex(text
)) > startPos
&& isDict
) {
66 c
= utext_previous32(text
);
67 isDict
= fSet
.contains(c
);
69 if (current
< startPos
) {
70 rangeStart
= startPos
;
75 rangeStart
= (int32_t)utext_getNativeIndex(text
);
78 // rangeEnd = start + 1;
79 utext_setNativeIndex(text
, start
);
81 rangeEnd
= (int32_t)utext_getNativeIndex(text
);
84 while((current
= (int32_t)utext_getNativeIndex(text
)) < endPos
&& fSet
.contains(c
)) {
85 utext_next32(text
); // TODO: recast loop for postincrement
86 c
= utext_current32(text
);
91 if (breakType
>= 0 && breakType
< 32 && (((uint32_t)1 << breakType
) & fTypes
)) {
92 result
= divideUpDictionaryRange(text
, rangeStart
, rangeEnd
, foundBreaks
);
93 utext_setNativeIndex(text
, current
);
100 DictionaryBreakEngine::setCharacters( const UnicodeSet
&set
) {
102 // Compact for caching
107 ******************************************************************
111 // Helper class for improving readability of the Thai/Lao/Khmer word break
112 // algorithm. The implementation is completely inline.
114 // List size, limited by the maximum number of words in the dictionary
115 // that form a nested sequence.
116 static const int32_t POSSIBLE_WORD_LIST_MAX
= 20;
120 // list of word candidate lengths, in increasing length order
121 // TODO: bytes would be sufficient for word lengths.
122 int32_t count
; // Count of candidates
123 int32_t prefix
; // The longest match with a dictionary word
124 int32_t offset
; // Offset in the text of these candidates
125 int32_t mark
; // The preferred candidate's offset
126 int32_t current
; // The candidate we're currently looking at
127 int32_t cuLengths
[POSSIBLE_WORD_LIST_MAX
]; // Word Lengths, in code units.
128 int32_t cpLengths
[POSSIBLE_WORD_LIST_MAX
]; // Word Lengths, in code points.
131 PossibleWord() : count(0), prefix(0), offset(-1), mark(0), current(0) {};
134 // Fill the list of candidates if needed, select the longest, and return the number found
135 int32_t candidates( UText
*text
, DictionaryMatcher
*dict
, int32_t rangeEnd
);
137 // Select the currently marked candidate, point after it in the text, and invalidate self
138 int32_t acceptMarked( UText
*text
);
140 // Back up from the current candidate to the next shorter one; return TRUE if that exists
141 // and point the text after it
142 UBool
backUp( UText
*text
);
144 // Return the longest prefix this candidate location shares with a dictionary word
145 // Return value is in code points.
146 int32_t longestPrefix() { return prefix
; };
148 // Mark the current candidate as the one we like
149 void markCurrent() { mark
= current
; };
151 // Get length in code points of the marked word.
152 int32_t markedCPLength() { return cpLengths
[mark
]; };
156 int32_t PossibleWord::candidates( UText
*text
, DictionaryMatcher
*dict
, int32_t rangeEnd
) {
157 // TODO: If getIndex is too slow, use offset < 0 and add discardAll()
158 int32_t start
= (int32_t)utext_getNativeIndex(text
);
159 if (start
!= offset
) {
161 count
= dict
->matches(text
, rangeEnd
-start
, UPRV_LENGTHOF(cuLengths
), cuLengths
, cpLengths
, NULL
, &prefix
);
162 // Dictionary leaves text after longest prefix, not longest word. Back up.
164 utext_setNativeIndex(text
, start
);
168 utext_setNativeIndex(text
, start
+cuLengths
[count
-1]);
176 PossibleWord::acceptMarked( UText
*text
) {
177 utext_setNativeIndex(text
, offset
+ cuLengths
[mark
]);
178 return cuLengths
[mark
];
183 PossibleWord::backUp( UText
*text
) {
185 utext_setNativeIndex(text
, offset
+ cuLengths
[--current
]);
192 ******************************************************************
196 // How many words in a row are "good enough"?
197 static const int32_t THAI_LOOKAHEAD
= 3;
199 // Will not combine a non-word with a preceding dictionary word longer than this
200 static const int32_t THAI_ROOT_COMBINE_THRESHOLD
= 3;
202 // Will not combine a non-word that shares at least this much prefix with a
203 // dictionary word, with a preceding word
204 static const int32_t THAI_PREFIX_COMBINE_THRESHOLD
= 3;
206 // Ellision character
207 static const int32_t THAI_PAIYANNOI
= 0x0E2F;
210 static const int32_t THAI_MAIYAMOK
= 0x0E46;
213 static const int32_t THAI_MIN_WORD
= 2;
215 // Minimum number of characters for two words
216 static const int32_t THAI_MIN_WORD_SPAN
= THAI_MIN_WORD
* 2;
218 ThaiBreakEngine::ThaiBreakEngine(DictionaryMatcher
*adoptDictionary
, UErrorCode
&status
)
219 : DictionaryBreakEngine((1<<UBRK_WORD
) | (1<<UBRK_LINE
)),
220 fDictionary(adoptDictionary
)
222 fThaiWordSet
.applyPattern(UNICODE_STRING_SIMPLE("[[:Thai:]&[:LineBreak=SA:]]"), status
);
223 if (U_SUCCESS(status
)) {
224 setCharacters(fThaiWordSet
);
226 fMarkSet
.applyPattern(UNICODE_STRING_SIMPLE("[[:Thai:]&[:LineBreak=SA:]&[:M:]]"), status
);
227 fMarkSet
.add(0x0020);
228 fEndWordSet
= fThaiWordSet
;
229 fEndWordSet
.remove(0x0E31); // MAI HAN-AKAT
230 fEndWordSet
.remove(0x0E40, 0x0E44); // SARA E through SARA AI MAIMALAI
231 fBeginWordSet
.add(0x0E01, 0x0E2E); // KO KAI through HO NOKHUK
232 fBeginWordSet
.add(0x0E40, 0x0E44); // SARA E through SARA AI MAIMALAI
233 fSuffixSet
.add(THAI_PAIYANNOI
);
234 fSuffixSet
.add(THAI_MAIYAMOK
);
236 // Compact for caching.
238 fEndWordSet
.compact();
239 fBeginWordSet
.compact();
240 fSuffixSet
.compact();
243 ThaiBreakEngine::~ThaiBreakEngine() {
248 ThaiBreakEngine::divideUpDictionaryRange( UText
*text
,
251 UStack
&foundBreaks
) const {
252 utext_setNativeIndex(text
, rangeStart
);
253 utext_moveIndex32(text
, THAI_MIN_WORD_SPAN
);
254 if (utext_getNativeIndex(text
) >= rangeEnd
) {
255 return 0; // Not enough characters for two words
257 utext_setNativeIndex(text
, rangeStart
);
260 uint32_t wordsFound
= 0;
261 int32_t cpWordLength
= 0; // Word Length in Code Points.
262 int32_t cuWordLength
= 0; // Word length in code units (UText native indexing)
264 UErrorCode status
= U_ZERO_ERROR
;
265 PossibleWord words
[THAI_LOOKAHEAD
];
267 utext_setNativeIndex(text
, rangeStart
);
269 while (U_SUCCESS(status
) && (current
= (int32_t)utext_getNativeIndex(text
)) < rangeEnd
) {
273 // Look for candidate words at the current position
274 int32_t candidates
= words
[wordsFound%THAI_LOOKAHEAD
].candidates(text
, fDictionary
, rangeEnd
);
276 // If we found exactly one, use that
277 if (candidates
== 1) {
278 cuWordLength
= words
[wordsFound
% THAI_LOOKAHEAD
].acceptMarked(text
);
279 cpWordLength
= words
[wordsFound
% THAI_LOOKAHEAD
].markedCPLength();
282 // If there was more than one, see which one can take us forward the most words
283 else if (candidates
> 1) {
284 // If we're already at the end of the range, we're done
285 if ((int32_t)utext_getNativeIndex(text
) >= rangeEnd
) {
289 int32_t wordsMatched
= 1;
290 if (words
[(wordsFound
+ 1) % THAI_LOOKAHEAD
].candidates(text
, fDictionary
, rangeEnd
) > 0) {
291 if (wordsMatched
< 2) {
292 // Followed by another dictionary word; mark first word as a good candidate
293 words
[wordsFound%THAI_LOOKAHEAD
].markCurrent();
297 // If we're already at the end of the range, we're done
298 if ((int32_t)utext_getNativeIndex(text
) >= rangeEnd
) {
302 // See if any of the possible second words is followed by a third word
304 // If we find a third word, stop right away
305 if (words
[(wordsFound
+ 2) % THAI_LOOKAHEAD
].candidates(text
, fDictionary
, rangeEnd
)) {
306 words
[wordsFound
% THAI_LOOKAHEAD
].markCurrent();
310 while (words
[(wordsFound
+ 1) % THAI_LOOKAHEAD
].backUp(text
));
313 while (words
[wordsFound
% THAI_LOOKAHEAD
].backUp(text
));
315 // Set UText position to after the accepted word.
316 cuWordLength
= words
[wordsFound
% THAI_LOOKAHEAD
].acceptMarked(text
);
317 cpWordLength
= words
[wordsFound
% THAI_LOOKAHEAD
].markedCPLength();
321 // We come here after having either found a word or not. We look ahead to the
322 // next word. If it's not a dictionary word, we will combine it with the word we
323 // just found (if there is one), but only if the preceding word does not exceed
325 // The text iterator should now be positioned at the end of the word we found.
328 if ((int32_t)utext_getNativeIndex(text
) < rangeEnd
&& cpWordLength
< THAI_ROOT_COMBINE_THRESHOLD
) {
329 // if it is a dictionary word, do nothing. If it isn't, then if there is
330 // no preceding word, or the non-word shares less than the minimum threshold
331 // of characters with a dictionary word, then scan to resynchronize
332 if (words
[wordsFound
% THAI_LOOKAHEAD
].candidates(text
, fDictionary
, rangeEnd
) <= 0
333 && (cuWordLength
== 0
334 || words
[wordsFound%THAI_LOOKAHEAD
].longestPrefix() < THAI_PREFIX_COMBINE_THRESHOLD
)) {
335 // Look for a plausible word boundary
336 int32_t remaining
= rangeEnd
- (current
+cuWordLength
);
340 int32_t pcIndex
= (int32_t)utext_getNativeIndex(text
);
341 pc
= utext_next32(text
);
342 int32_t pcSize
= (int32_t)utext_getNativeIndex(text
) - pcIndex
;
345 if (remaining
<= 0) {
348 uc
= utext_current32(text
);
349 if (fEndWordSet
.contains(pc
) && fBeginWordSet
.contains(uc
)) {
350 // Maybe. See if it's in the dictionary.
351 // NOTE: In the original Apple code, checked that the next
352 // two characters after uc were not 0x0E4C THANTHAKHAT before
353 // checking the dictionary. That is just a performance filter,
354 // but it's not clear it's faster than checking the trie.
355 int32_t candidates
= words
[(wordsFound
+ 1) % THAI_LOOKAHEAD
].candidates(text
, fDictionary
, rangeEnd
);
356 utext_setNativeIndex(text
, current
+ cuWordLength
+ chars
);
357 if (candidates
> 0) {
363 // Bump the word count if there wasn't already one
364 if (cuWordLength
<= 0) {
368 // Update the length with the passed-over characters
369 cuWordLength
+= chars
;
372 // Back up to where we were for next iteration
373 utext_setNativeIndex(text
, current
+cuWordLength
);
377 // Never stop before a combining mark.
379 while ((currPos
= (int32_t)utext_getNativeIndex(text
)) < rangeEnd
&& fMarkSet
.contains(utext_current32(text
))) {
381 cuWordLength
+= (int32_t)utext_getNativeIndex(text
) - currPos
;
384 // Look ahead for possible suffixes if a dictionary word does not follow.
385 // We do this in code rather than using a rule so that the heuristic
386 // resynch continues to function. For example, one of the suffix characters
387 // could be a typo in the middle of a word.
388 if ((int32_t)utext_getNativeIndex(text
) < rangeEnd
&& cuWordLength
> 0) {
389 if (words
[wordsFound%THAI_LOOKAHEAD
].candidates(text
, fDictionary
, rangeEnd
) <= 0
390 && fSuffixSet
.contains(uc
= utext_current32(text
))) {
391 if (uc
== THAI_PAIYANNOI
) {
392 if (!fSuffixSet
.contains(utext_previous32(text
))) {
393 // Skip over previous end and PAIYANNOI
395 int32_t paiyannoiIndex
= (int32_t)utext_getNativeIndex(text
);
397 cuWordLength
+= (int32_t)utext_getNativeIndex(text
) - paiyannoiIndex
; // Add PAIYANNOI to word
398 uc
= utext_current32(text
); // Fetch next character
401 // Restore prior position
405 if (uc
== THAI_MAIYAMOK
) {
406 if (utext_previous32(text
) != THAI_MAIYAMOK
) {
407 // Skip over previous end and MAIYAMOK
409 int32_t maiyamokIndex
= (int32_t)utext_getNativeIndex(text
);
411 cuWordLength
+= (int32_t)utext_getNativeIndex(text
) - maiyamokIndex
; // Add MAIYAMOK to word
414 // Restore prior position
420 utext_setNativeIndex(text
, current
+cuWordLength
);
424 // Did we find a word on this iteration? If so, push it on the break stack
425 if (cuWordLength
> 0) {
426 foundBreaks
.push((current
+cuWordLength
), status
);
430 // Don't return a break for the end of the dictionary range if there is one there.
431 if (foundBreaks
.peeki() >= rangeEnd
) {
432 (void) foundBreaks
.popi();
440 ******************************************************************
444 // How many words in a row are "good enough"?
445 static const int32_t LAO_LOOKAHEAD
= 3;
447 // Will not combine a non-word with a preceding dictionary word longer than this
448 static const int32_t LAO_ROOT_COMBINE_THRESHOLD
= 3;
450 // Will not combine a non-word that shares at least this much prefix with a
451 // dictionary word, with a preceding word
452 static const int32_t LAO_PREFIX_COMBINE_THRESHOLD
= 3;
455 static const int32_t LAO_MIN_WORD
= 2;
457 // Minimum number of characters for two words
458 static const int32_t LAO_MIN_WORD_SPAN
= LAO_MIN_WORD
* 2;
460 LaoBreakEngine::LaoBreakEngine(DictionaryMatcher
*adoptDictionary
, UErrorCode
&status
)
461 : DictionaryBreakEngine((1<<UBRK_WORD
) | (1<<UBRK_LINE
)),
462 fDictionary(adoptDictionary
)
464 fLaoWordSet
.applyPattern(UNICODE_STRING_SIMPLE("[[:Laoo:]&[:LineBreak=SA:]]"), status
);
465 if (U_SUCCESS(status
)) {
466 setCharacters(fLaoWordSet
);
468 fMarkSet
.applyPattern(UNICODE_STRING_SIMPLE("[[:Laoo:]&[:LineBreak=SA:]&[:M:]]"), status
);
469 fMarkSet
.add(0x0020);
470 fEndWordSet
= fLaoWordSet
;
471 fEndWordSet
.remove(0x0EC0, 0x0EC4); // prefix vowels
472 fBeginWordSet
.add(0x0E81, 0x0EAE); // basic consonants (including holes for corresponding Thai characters)
473 fBeginWordSet
.add(0x0EDC, 0x0EDD); // digraph consonants (no Thai equivalent)
474 fBeginWordSet
.add(0x0EC0, 0x0EC4); // prefix vowels
476 // Compact for caching.
478 fEndWordSet
.compact();
479 fBeginWordSet
.compact();
482 LaoBreakEngine::~LaoBreakEngine() {
487 LaoBreakEngine::divideUpDictionaryRange( UText
*text
,
490 UStack
&foundBreaks
) const {
491 if ((rangeEnd
- rangeStart
) < LAO_MIN_WORD_SPAN
) {
492 return 0; // Not enough characters for two words
495 uint32_t wordsFound
= 0;
496 int32_t cpWordLength
= 0;
497 int32_t cuWordLength
= 0;
499 UErrorCode status
= U_ZERO_ERROR
;
500 PossibleWord words
[LAO_LOOKAHEAD
];
502 utext_setNativeIndex(text
, rangeStart
);
504 while (U_SUCCESS(status
) && (current
= (int32_t)utext_getNativeIndex(text
)) < rangeEnd
) {
508 // Look for candidate words at the current position
509 int32_t candidates
= words
[wordsFound%LAO_LOOKAHEAD
].candidates(text
, fDictionary
, rangeEnd
);
511 // If we found exactly one, use that
512 if (candidates
== 1) {
513 cuWordLength
= words
[wordsFound
% LAO_LOOKAHEAD
].acceptMarked(text
);
514 cpWordLength
= words
[wordsFound
% LAO_LOOKAHEAD
].markedCPLength();
517 // If there was more than one, see which one can take us forward the most words
518 else if (candidates
> 1) {
519 // If we're already at the end of the range, we're done
520 if (utext_getNativeIndex(text
) >= rangeEnd
) {
524 int32_t wordsMatched
= 1;
525 if (words
[(wordsFound
+ 1) % LAO_LOOKAHEAD
].candidates(text
, fDictionary
, rangeEnd
) > 0) {
526 if (wordsMatched
< 2) {
527 // Followed by another dictionary word; mark first word as a good candidate
528 words
[wordsFound%LAO_LOOKAHEAD
].markCurrent();
532 // If we're already at the end of the range, we're done
533 if ((int32_t)utext_getNativeIndex(text
) >= rangeEnd
) {
537 // See if any of the possible second words is followed by a third word
539 // If we find a third word, stop right away
540 if (words
[(wordsFound
+ 2) % LAO_LOOKAHEAD
].candidates(text
, fDictionary
, rangeEnd
)) {
541 words
[wordsFound
% LAO_LOOKAHEAD
].markCurrent();
545 while (words
[(wordsFound
+ 1) % LAO_LOOKAHEAD
].backUp(text
));
548 while (words
[wordsFound
% LAO_LOOKAHEAD
].backUp(text
));
550 cuWordLength
= words
[wordsFound
% LAO_LOOKAHEAD
].acceptMarked(text
);
551 cpWordLength
= words
[wordsFound
% LAO_LOOKAHEAD
].markedCPLength();
555 // We come here after having either found a word or not. We look ahead to the
556 // next word. If it's not a dictionary word, we will combine it withe the word we
557 // just found (if there is one), but only if the preceding word does not exceed
559 // The text iterator should now be positioned at the end of the word we found.
560 if ((int32_t)utext_getNativeIndex(text
) < rangeEnd
&& cpWordLength
< LAO_ROOT_COMBINE_THRESHOLD
) {
561 // if it is a dictionary word, do nothing. If it isn't, then if there is
562 // no preceding word, or the non-word shares less than the minimum threshold
563 // of characters with a dictionary word, then scan to resynchronize
564 if (words
[wordsFound
% LAO_LOOKAHEAD
].candidates(text
, fDictionary
, rangeEnd
) <= 0
565 && (cuWordLength
== 0
566 || words
[wordsFound%LAO_LOOKAHEAD
].longestPrefix() < LAO_PREFIX_COMBINE_THRESHOLD
)) {
567 // Look for a plausible word boundary
568 int32_t remaining
= rangeEnd
- (current
+ cuWordLength
);
573 int32_t pcIndex
= (int32_t)utext_getNativeIndex(text
);
574 pc
= utext_next32(text
);
575 int32_t pcSize
= (int32_t)utext_getNativeIndex(text
) - pcIndex
;
578 if (remaining
<= 0) {
581 uc
= utext_current32(text
);
582 if (fEndWordSet
.contains(pc
) && fBeginWordSet
.contains(uc
)) {
583 // Maybe. See if it's in the dictionary.
584 // TODO: this looks iffy; compare with old code.
585 int32_t candidates
= words
[(wordsFound
+ 1) % LAO_LOOKAHEAD
].candidates(text
, fDictionary
, rangeEnd
);
586 utext_setNativeIndex(text
, current
+ cuWordLength
+ chars
);
587 if (candidates
> 0) {
593 // Bump the word count if there wasn't already one
594 if (cuWordLength
<= 0) {
598 // Update the length with the passed-over characters
599 cuWordLength
+= chars
;
602 // Back up to where we were for next iteration
603 utext_setNativeIndex(text
, current
+ cuWordLength
);
607 // Never stop before a combining mark.
609 while ((currPos
= (int32_t)utext_getNativeIndex(text
)) < rangeEnd
&& fMarkSet
.contains(utext_current32(text
))) {
611 cuWordLength
+= (int32_t)utext_getNativeIndex(text
) - currPos
;
614 // Look ahead for possible suffixes if a dictionary word does not follow.
615 // We do this in code rather than using a rule so that the heuristic
616 // resynch continues to function. For example, one of the suffix characters
617 // could be a typo in the middle of a word.
618 // NOT CURRENTLY APPLICABLE TO LAO
620 // Did we find a word on this iteration? If so, push it on the break stack
621 if (cuWordLength
> 0) {
622 foundBreaks
.push((current
+cuWordLength
), status
);
626 // Don't return a break for the end of the dictionary range if there is one there.
627 if (foundBreaks
.peeki() >= rangeEnd
) {
628 (void) foundBreaks
.popi();
636 ******************************************************************
640 // How many words in a row are "good enough"?
641 static const int32_t BURMESE_LOOKAHEAD
= 3;
643 // Will not combine a non-word with a preceding dictionary word longer than this
644 static const int32_t BURMESE_ROOT_COMBINE_THRESHOLD
= 3;
646 // Will not combine a non-word that shares at least this much prefix with a
647 // dictionary word, with a preceding word
648 static const int32_t BURMESE_PREFIX_COMBINE_THRESHOLD
= 3;
651 static const int32_t BURMESE_MIN_WORD
= 2;
653 // Minimum number of characters for two words
654 static const int32_t BURMESE_MIN_WORD_SPAN
= BURMESE_MIN_WORD
* 2;
656 BurmeseBreakEngine::BurmeseBreakEngine(DictionaryMatcher
*adoptDictionary
, UErrorCode
&status
)
657 : DictionaryBreakEngine((1<<UBRK_WORD
) | (1<<UBRK_LINE
)),
658 fDictionary(adoptDictionary
)
660 fBurmeseWordSet
.applyPattern(UNICODE_STRING_SIMPLE("[[:Mymr:]&[:LineBreak=SA:]]"), status
);
661 if (U_SUCCESS(status
)) {
662 setCharacters(fBurmeseWordSet
);
664 fMarkSet
.applyPattern(UNICODE_STRING_SIMPLE("[[:Mymr:]&[:LineBreak=SA:]&[:M:]]"), status
);
665 fMarkSet
.add(0x0020);
666 fEndWordSet
= fBurmeseWordSet
;
667 fBeginWordSet
.add(0x1000, 0x102A); // basic consonants and independent vowels
669 // Compact for caching.
671 fEndWordSet
.compact();
672 fBeginWordSet
.compact();
675 BurmeseBreakEngine::~BurmeseBreakEngine() {
680 BurmeseBreakEngine::divideUpDictionaryRange( UText
*text
,
683 UStack
&foundBreaks
) const {
684 if ((rangeEnd
- rangeStart
) < BURMESE_MIN_WORD_SPAN
) {
685 return 0; // Not enough characters for two words
688 uint32_t wordsFound
= 0;
689 int32_t cpWordLength
= 0;
690 int32_t cuWordLength
= 0;
692 UErrorCode status
= U_ZERO_ERROR
;
693 PossibleWord words
[BURMESE_LOOKAHEAD
];
695 utext_setNativeIndex(text
, rangeStart
);
697 while (U_SUCCESS(status
) && (current
= (int32_t)utext_getNativeIndex(text
)) < rangeEnd
) {
701 // Look for candidate words at the current position
702 int32_t candidates
= words
[wordsFound%BURMESE_LOOKAHEAD
].candidates(text
, fDictionary
, rangeEnd
);
704 // If we found exactly one, use that
705 if (candidates
== 1) {
706 cuWordLength
= words
[wordsFound
% BURMESE_LOOKAHEAD
].acceptMarked(text
);
707 cpWordLength
= words
[wordsFound
% BURMESE_LOOKAHEAD
].markedCPLength();
710 // If there was more than one, see which one can take us forward the most words
711 else if (candidates
> 1) {
712 // If we're already at the end of the range, we're done
713 if (utext_getNativeIndex(text
) >= rangeEnd
) {
717 int32_t wordsMatched
= 1;
718 if (words
[(wordsFound
+ 1) % BURMESE_LOOKAHEAD
].candidates(text
, fDictionary
, rangeEnd
) > 0) {
719 if (wordsMatched
< 2) {
720 // Followed by another dictionary word; mark first word as a good candidate
721 words
[wordsFound%BURMESE_LOOKAHEAD
].markCurrent();
725 // If we're already at the end of the range, we're done
726 if ((int32_t)utext_getNativeIndex(text
) >= rangeEnd
) {
730 // See if any of the possible second words is followed by a third word
732 // If we find a third word, stop right away
733 if (words
[(wordsFound
+ 2) % BURMESE_LOOKAHEAD
].candidates(text
, fDictionary
, rangeEnd
)) {
734 words
[wordsFound
% BURMESE_LOOKAHEAD
].markCurrent();
738 while (words
[(wordsFound
+ 1) % BURMESE_LOOKAHEAD
].backUp(text
));
741 while (words
[wordsFound
% BURMESE_LOOKAHEAD
].backUp(text
));
743 cuWordLength
= words
[wordsFound
% BURMESE_LOOKAHEAD
].acceptMarked(text
);
744 cpWordLength
= words
[wordsFound
% BURMESE_LOOKAHEAD
].markedCPLength();
748 // We come here after having either found a word or not. We look ahead to the
749 // next word. If it's not a dictionary word, we will combine it withe the word we
750 // just found (if there is one), but only if the preceding word does not exceed
752 // The text iterator should now be positioned at the end of the word we found.
753 if ((int32_t)utext_getNativeIndex(text
) < rangeEnd
&& cpWordLength
< BURMESE_ROOT_COMBINE_THRESHOLD
) {
754 // if it is a dictionary word, do nothing. If it isn't, then if there is
755 // no preceding word, or the non-word shares less than the minimum threshold
756 // of characters with a dictionary word, then scan to resynchronize
757 if (words
[wordsFound
% BURMESE_LOOKAHEAD
].candidates(text
, fDictionary
, rangeEnd
) <= 0
758 && (cuWordLength
== 0
759 || words
[wordsFound%BURMESE_LOOKAHEAD
].longestPrefix() < BURMESE_PREFIX_COMBINE_THRESHOLD
)) {
760 // Look for a plausible word boundary
761 int32_t remaining
= rangeEnd
- (current
+ cuWordLength
);
766 int32_t pcIndex
= (int32_t)utext_getNativeIndex(text
);
767 pc
= utext_next32(text
);
768 int32_t pcSize
= (int32_t)utext_getNativeIndex(text
) - pcIndex
;
771 if (remaining
<= 0) {
774 uc
= utext_current32(text
);
775 if (fEndWordSet
.contains(pc
) && fBeginWordSet
.contains(uc
)) {
776 // Maybe. See if it's in the dictionary.
777 // TODO: this looks iffy; compare with old code.
778 int32_t candidates
= words
[(wordsFound
+ 1) % BURMESE_LOOKAHEAD
].candidates(text
, fDictionary
, rangeEnd
);
779 utext_setNativeIndex(text
, current
+ cuWordLength
+ chars
);
780 if (candidates
> 0) {
786 // Bump the word count if there wasn't already one
787 if (cuWordLength
<= 0) {
791 // Update the length with the passed-over characters
792 cuWordLength
+= chars
;
795 // Back up to where we were for next iteration
796 utext_setNativeIndex(text
, current
+ cuWordLength
);
800 // Never stop before a combining mark.
802 while ((currPos
= (int32_t)utext_getNativeIndex(text
)) < rangeEnd
&& fMarkSet
.contains(utext_current32(text
))) {
804 cuWordLength
+= (int32_t)utext_getNativeIndex(text
) - currPos
;
807 // Look ahead for possible suffixes if a dictionary word does not follow.
808 // We do this in code rather than using a rule so that the heuristic
809 // resynch continues to function. For example, one of the suffix characters
810 // could be a typo in the middle of a word.
811 // NOT CURRENTLY APPLICABLE TO BURMESE
813 // Did we find a word on this iteration? If so, push it on the break stack
814 if (cuWordLength
> 0) {
815 foundBreaks
.push((current
+cuWordLength
), status
);
819 // Don't return a break for the end of the dictionary range if there is one there.
820 if (foundBreaks
.peeki() >= rangeEnd
) {
821 (void) foundBreaks
.popi();
829 ******************************************************************
833 // How many words in a row are "good enough"?
834 static const int32_t KHMER_LOOKAHEAD
= 3;
836 // Will not combine a non-word with a preceding dictionary word longer than this
837 static const int32_t KHMER_ROOT_COMBINE_THRESHOLD
= 3;
839 // Will not combine a non-word that shares at least this much prefix with a
840 // dictionary word, with a preceding word
841 static const int32_t KHMER_PREFIX_COMBINE_THRESHOLD
= 3;
844 static const int32_t KHMER_MIN_WORD
= 2;
846 // Minimum number of characters for two words
847 static const int32_t KHMER_MIN_WORD_SPAN
= KHMER_MIN_WORD
* 2;
849 KhmerBreakEngine::KhmerBreakEngine(DictionaryMatcher
*adoptDictionary
, UErrorCode
&status
)
850 : DictionaryBreakEngine((1 << UBRK_WORD
) | (1 << UBRK_LINE
)),
851 fDictionary(adoptDictionary
)
853 fKhmerWordSet
.applyPattern(UNICODE_STRING_SIMPLE("[[:Khmr:]&[:LineBreak=SA:]]"), status
);
854 if (U_SUCCESS(status
)) {
855 setCharacters(fKhmerWordSet
);
857 fMarkSet
.applyPattern(UNICODE_STRING_SIMPLE("[[:Khmr:]&[:LineBreak=SA:]&[:M:]]"), status
);
858 fMarkSet
.add(0x0020);
859 fEndWordSet
= fKhmerWordSet
;
860 fBeginWordSet
.add(0x1780, 0x17B3);
861 //fBeginWordSet.add(0x17A3, 0x17A4); // deprecated vowels
862 //fEndWordSet.remove(0x17A5, 0x17A9); // Khmer independent vowels that can't end a word
863 //fEndWordSet.remove(0x17B2); // Khmer independent vowel that can't end a word
864 fEndWordSet
.remove(0x17D2); // KHMER SIGN COENG that combines some following characters
865 //fEndWordSet.remove(0x17B6, 0x17C5); // Remove dependent vowels
866 // fEndWordSet.remove(0x0E31); // MAI HAN-AKAT
867 // fEndWordSet.remove(0x0E40, 0x0E44); // SARA E through SARA AI MAIMALAI
868 // fBeginWordSet.add(0x0E01, 0x0E2E); // KO KAI through HO NOKHUK
869 // fBeginWordSet.add(0x0E40, 0x0E44); // SARA E through SARA AI MAIMALAI
870 // fSuffixSet.add(THAI_PAIYANNOI);
871 // fSuffixSet.add(THAI_MAIYAMOK);
873 // Compact for caching.
875 fEndWordSet
.compact();
876 fBeginWordSet
.compact();
877 // fSuffixSet.compact();
880 KhmerBreakEngine::~KhmerBreakEngine() {
885 KhmerBreakEngine::divideUpDictionaryRange( UText
*text
,
888 UStack
&foundBreaks
) const {
889 if ((rangeEnd
- rangeStart
) < KHMER_MIN_WORD_SPAN
) {
890 return 0; // Not enough characters for two words
893 uint32_t wordsFound
= 0;
894 int32_t cpWordLength
= 0;
895 int32_t cuWordLength
= 0;
897 UErrorCode status
= U_ZERO_ERROR
;
898 PossibleWord words
[KHMER_LOOKAHEAD
];
900 utext_setNativeIndex(text
, rangeStart
);
902 while (U_SUCCESS(status
) && (current
= (int32_t)utext_getNativeIndex(text
)) < rangeEnd
) {
906 // Look for candidate words at the current position
907 int32_t candidates
= words
[wordsFound%KHMER_LOOKAHEAD
].candidates(text
, fDictionary
, rangeEnd
);
909 // If we found exactly one, use that
910 if (candidates
== 1) {
911 cuWordLength
= words
[wordsFound
% KHMER_LOOKAHEAD
].acceptMarked(text
);
912 cpWordLength
= words
[wordsFound
% KHMER_LOOKAHEAD
].markedCPLength();
916 // If there was more than one, see which one can take us forward the most words
917 else if (candidates
> 1) {
918 // If we're already at the end of the range, we're done
919 if ((int32_t)utext_getNativeIndex(text
) >= rangeEnd
) {
923 int32_t wordsMatched
= 1;
924 if (words
[(wordsFound
+ 1) % KHMER_LOOKAHEAD
].candidates(text
, fDictionary
, rangeEnd
) > 0) {
925 if (wordsMatched
< 2) {
926 // Followed by another dictionary word; mark first word as a good candidate
927 words
[wordsFound
% KHMER_LOOKAHEAD
].markCurrent();
931 // If we're already at the end of the range, we're done
932 if ((int32_t)utext_getNativeIndex(text
) >= rangeEnd
) {
936 // See if any of the possible second words is followed by a third word
938 // If we find a third word, stop right away
939 if (words
[(wordsFound
+ 2) % KHMER_LOOKAHEAD
].candidates(text
, fDictionary
, rangeEnd
)) {
940 words
[wordsFound
% KHMER_LOOKAHEAD
].markCurrent();
944 while (words
[(wordsFound
+ 1) % KHMER_LOOKAHEAD
].backUp(text
));
947 while (words
[wordsFound
% KHMER_LOOKAHEAD
].backUp(text
));
949 cuWordLength
= words
[wordsFound
% KHMER_LOOKAHEAD
].acceptMarked(text
);
950 cpWordLength
= words
[wordsFound
% KHMER_LOOKAHEAD
].markedCPLength();
954 // We come here after having either found a word or not. We look ahead to the
955 // next word. If it's not a dictionary word, we will combine it with the word we
956 // just found (if there is one), but only if the preceding word does not exceed
958 // The text iterator should now be positioned at the end of the word we found.
959 if ((int32_t)utext_getNativeIndex(text
) < rangeEnd
&& cpWordLength
< KHMER_ROOT_COMBINE_THRESHOLD
) {
960 // if it is a dictionary word, do nothing. If it isn't, then if there is
961 // no preceding word, or the non-word shares less than the minimum threshold
962 // of characters with a dictionary word, then scan to resynchronize
963 if (words
[wordsFound
% KHMER_LOOKAHEAD
].candidates(text
, fDictionary
, rangeEnd
) <= 0
964 && (cuWordLength
== 0
965 || words
[wordsFound
% KHMER_LOOKAHEAD
].longestPrefix() < KHMER_PREFIX_COMBINE_THRESHOLD
)) {
966 // Look for a plausible word boundary
967 int32_t remaining
= rangeEnd
- (current
+cuWordLength
);
972 int32_t pcIndex
= (int32_t)utext_getNativeIndex(text
);
973 pc
= utext_next32(text
);
974 int32_t pcSize
= (int32_t)utext_getNativeIndex(text
) - pcIndex
;
977 if (remaining
<= 0) {
980 uc
= utext_current32(text
);
981 if (fEndWordSet
.contains(pc
) && fBeginWordSet
.contains(uc
)) {
982 // Maybe. See if it's in the dictionary.
983 int32_t candidates
= words
[(wordsFound
+ 1) % KHMER_LOOKAHEAD
].candidates(text
, fDictionary
, rangeEnd
);
984 utext_setNativeIndex(text
, current
+cuWordLength
+chars
);
985 if (candidates
> 0) {
991 // Bump the word count if there wasn't already one
992 if (cuWordLength
<= 0) {
996 // Update the length with the passed-over characters
997 cuWordLength
+= chars
;
1000 // Back up to where we were for next iteration
1001 utext_setNativeIndex(text
, current
+cuWordLength
);
1005 // Never stop before a combining mark.
1007 while ((currPos
= (int32_t)utext_getNativeIndex(text
)) < rangeEnd
&& fMarkSet
.contains(utext_current32(text
))) {
1009 cuWordLength
+= (int32_t)utext_getNativeIndex(text
) - currPos
;
1012 // Look ahead for possible suffixes if a dictionary word does not follow.
1013 // We do this in code rather than using a rule so that the heuristic
1014 // resynch continues to function. For example, one of the suffix characters
1015 // could be a typo in the middle of a word.
1016 // if ((int32_t)utext_getNativeIndex(text) < rangeEnd && wordLength > 0) {
1017 // if (words[wordsFound%KHMER_LOOKAHEAD].candidates(text, fDictionary, rangeEnd) <= 0
1018 // && fSuffixSet.contains(uc = utext_current32(text))) {
1019 // if (uc == KHMER_PAIYANNOI) {
1020 // if (!fSuffixSet.contains(utext_previous32(text))) {
1021 // // Skip over previous end and PAIYANNOI
1022 // utext_next32(text);
1023 // utext_next32(text);
1024 // wordLength += 1; // Add PAIYANNOI to word
1025 // uc = utext_current32(text); // Fetch next character
1028 // // Restore prior position
1029 // utext_next32(text);
1032 // if (uc == KHMER_MAIYAMOK) {
1033 // if (utext_previous32(text) != KHMER_MAIYAMOK) {
1034 // // Skip over previous end and MAIYAMOK
1035 // utext_next32(text);
1036 // utext_next32(text);
1037 // wordLength += 1; // Add MAIYAMOK to word
1040 // // Restore prior position
1041 // utext_next32(text);
1046 // utext_setNativeIndex(text, current+wordLength);
1050 // Did we find a word on this iteration? If so, push it on the break stack
1051 if (cuWordLength
> 0) {
1052 foundBreaks
.push((current
+cuWordLength
), status
);
1056 // Don't return a break for the end of the dictionary range if there is one there.
1057 if (foundBreaks
.peeki() >= rangeEnd
) {
1058 (void) foundBreaks
.popi();
1065 #if !UCONFIG_NO_NORMALIZATION
1067 ******************************************************************
1070 static const uint32_t kuint32max
= 0xFFFFFFFF;
1071 CjkBreakEngine::CjkBreakEngine(DictionaryMatcher
*adoptDictionary
, LanguageType type
, UErrorCode
&status
)
1072 : DictionaryBreakEngine(1 << UBRK_WORD
), fDictionary(adoptDictionary
) {
1073 // Korean dictionary only includes Hangul syllables
1074 fHangulWordSet
.applyPattern(UNICODE_STRING_SIMPLE("[\\uac00-\\ud7a3]"), status
);
1075 fHanWordSet
.applyPattern(UNICODE_STRING_SIMPLE("[:Han:]"), status
);
1076 fKatakanaWordSet
.applyPattern(UNICODE_STRING_SIMPLE("[[:Katakana:]\\uff9e\\uff9f]"), status
);
1077 fHiraganaWordSet
.applyPattern(UNICODE_STRING_SIMPLE("[:Hiragana:]"), status
);
1078 nfkcNorm2
= Normalizer2::getNFKCInstance(status
);
1080 if (U_SUCCESS(status
)) {
1081 // handle Korean and Japanese/Chinese using different dictionaries
1082 if (type
== kKorean
) {
1083 setCharacters(fHangulWordSet
);
1084 } else { //Chinese and Japanese
1086 cjSet
.addAll(fHanWordSet
);
1087 cjSet
.addAll(fKatakanaWordSet
);
1088 cjSet
.addAll(fHiraganaWordSet
);
1089 cjSet
.add(0xFF70); // HALFWIDTH KATAKANA-HIRAGANA PROLONGED SOUND MARK
1090 cjSet
.add(0x30FC); // KATAKANA-HIRAGANA PROLONGED SOUND MARK
1091 setCharacters(cjSet
);
1096 CjkBreakEngine::~CjkBreakEngine(){
1100 // The katakanaCost values below are based on the length frequencies of all
1101 // katakana phrases in the dictionary
1102 static const int32_t kMaxKatakanaLength
= 8;
1103 static const int32_t kMaxKatakanaGroupLength
= 20;
1104 static const uint32_t maxSnlp
= 255;
1106 static inline uint32_t getKatakanaCost(int32_t wordLength
){
1107 //TODO: fill array with actual values from dictionary!
1108 static const uint32_t katakanaCost
[kMaxKatakanaLength
+ 1]
1109 = {8192, 984, 408, 240, 204, 252, 300, 372, 480};
1110 return (wordLength
> kMaxKatakanaLength
) ? 8192 : katakanaCost
[wordLength
];
1113 static inline bool isKatakana(uint16_t value
) {
1114 return (value
>= 0x30A1u
&& value
<= 0x30FEu
&& value
!= 0x30FBu
) ||
1115 (value
>= 0xFF66u
&& value
<= 0xFF9fu
);
1119 // Function for accessing internal utext flags.
1120 // Replicates an internal UText function.
1122 static inline int32_t utext_i32_flag(int32_t bitIndex
) {
1123 return (int32_t)1 << bitIndex
;
1128 * @param text A UText representing the text
1129 * @param rangeStart The start of the range of dictionary characters
1130 * @param rangeEnd The end of the range of dictionary characters
1131 * @param foundBreaks Output of C array of int32_t break positions, or 0
1132 * @return The number of breaks found
1135 CjkBreakEngine::divideUpDictionaryRange( UText
*inText
,
1138 UStack
&foundBreaks
) const {
1139 if (rangeStart
>= rangeEnd
) {
1143 // UnicodeString version of input UText, NFKC normalized if necessary.
1144 UnicodeString inString
;
1146 // inputMap[inStringIndex] = corresponding native index from UText inText.
1147 // If NULL then mapping is 1:1
1148 LocalPointer
<UVector32
> inputMap
;
1150 UErrorCode status
= U_ZERO_ERROR
;
1153 // if UText has the input string as one contiguous UTF-16 chunk
1154 if ((inText
->providerProperties
& utext_i32_flag(UTEXT_PROVIDER_STABLE_CHUNKS
)) &&
1155 inText
->chunkNativeStart
<= rangeStart
&&
1156 inText
->chunkNativeLimit
>= rangeEnd
&&
1157 inText
->nativeIndexingLimit
>= rangeEnd
- inText
->chunkNativeStart
) {
1159 // Input UText is in one contiguous UTF-16 chunk.
1160 // Use Read-only aliasing UnicodeString.
1161 inString
.setTo(FALSE
,
1162 inText
->chunkContents
+ rangeStart
- inText
->chunkNativeStart
,
1163 rangeEnd
- rangeStart
);
1165 // Copy the text from the original inText (UText) to inString (UnicodeString).
1166 // Create a map from UnicodeString indices -> UText offsets.
1167 utext_setNativeIndex(inText
, rangeStart
);
1168 int32_t limit
= rangeEnd
;
1169 U_ASSERT(limit
<= utext_nativeLength(inText
));
1170 if (limit
> utext_nativeLength(inText
)) {
1171 limit
= (int32_t)utext_nativeLength(inText
);
1173 inputMap
.adoptInsteadAndCheckErrorCode(new UVector32(status
), status
);
1174 if (U_FAILURE(status
)) {
1177 while (utext_getNativeIndex(inText
) < limit
) {
1178 int32_t nativePosition
= (int32_t)utext_getNativeIndex(inText
);
1179 UChar32 c
= utext_next32(inText
);
1180 U_ASSERT(c
!= U_SENTINEL
);
1182 while (inputMap
->size() < inString
.length()) {
1183 inputMap
->addElement(nativePosition
, status
);
1186 inputMap
->addElement(limit
, status
);
1190 if (!nfkcNorm2
->isNormalized(inString
, status
)) {
1191 UnicodeString normalizedInput
;
1192 // normalizedMap[normalizedInput position] == original UText position.
1193 LocalPointer
<UVector32
> normalizedMap(new UVector32(status
), status
);
1194 if (U_FAILURE(status
)) {
1198 UnicodeString fragment
;
1199 UnicodeString normalizedFragment
;
1200 for (int32_t srcI
= 0; srcI
< inString
.length();) { // Once per normalization chunk
1202 int32_t fragmentStartI
= srcI
;
1203 UChar32 c
= inString
.char32At(srcI
);
1206 srcI
= inString
.moveIndex32(srcI
, 1);
1207 if (srcI
== inString
.length()) {
1210 c
= inString
.char32At(srcI
);
1211 if (nfkcNorm2
->hasBoundaryBefore(c
)) {
1215 nfkcNorm2
->normalize(fragment
, normalizedFragment
, status
);
1216 normalizedInput
.append(normalizedFragment
);
1218 // Map every position in the normalized chunk to the start of the chunk
1219 // in the original input.
1220 int32_t fragmentOriginalStart
= inputMap
.isValid() ?
1221 inputMap
->elementAti(fragmentStartI
) : fragmentStartI
+rangeStart
;
1222 while (normalizedMap
->size() < normalizedInput
.length()) {
1223 normalizedMap
->addElement(fragmentOriginalStart
, status
);
1224 if (U_FAILURE(status
)) {
1229 U_ASSERT(normalizedMap
->size() == normalizedInput
.length());
1230 int32_t nativeEnd
= inputMap
.isValid() ?
1231 inputMap
->elementAti(inString
.length()) : inString
.length()+rangeStart
;
1232 normalizedMap
->addElement(nativeEnd
, status
);
1234 inputMap
.moveFrom(normalizedMap
);
1235 inString
.moveFrom(normalizedInput
);
1238 int32_t numCodePts
= inString
.countChar32();
1239 if (numCodePts
!= inString
.length()) {
1240 // There are supplementary characters in the input.
1241 // The dictionary will produce boundary positions in terms of code point indexes,
1242 // not in terms of code unit string indexes.
1243 // Use the inputMap mechanism to take care of this in addition to indexing differences
1244 // from normalization and/or UTF-8 input.
1245 UBool hadExistingMap
= inputMap
.isValid();
1246 if (!hadExistingMap
) {
1247 inputMap
.adoptInsteadAndCheckErrorCode(new UVector32(status
), status
);
1248 if (U_FAILURE(status
)) {
1253 for (int32_t cuIdx
= 0; ; cuIdx
= inString
.moveIndex32(cuIdx
, 1)) {
1254 U_ASSERT(cuIdx
>= cpIdx
);
1255 if (hadExistingMap
) {
1256 inputMap
->setElementAt(inputMap
->elementAti(cuIdx
), cpIdx
);
1258 inputMap
->addElement(cuIdx
+rangeStart
, status
);
1261 if (cuIdx
== inString
.length()) {
1267 // bestSnlp[i] is the snlp of the best segmentation of the first i
1268 // code points in the range to be matched.
1269 UVector32
bestSnlp(numCodePts
+ 1, status
);
1270 bestSnlp
.addElement(0, status
);
1271 for(int32_t i
= 1; i
<= numCodePts
; i
++) {
1272 bestSnlp
.addElement(kuint32max
, status
);
1276 // prev[i] is the index of the last CJK code point in the previous word in
1277 // the best segmentation of the first i characters.
1278 UVector32
prev(numCodePts
+ 1, status
);
1279 for(int32_t i
= 0; i
<= numCodePts
; i
++){
1280 prev
.addElement(-1, status
);
1283 const int32_t maxWordSize
= 20;
1284 UVector32
values(numCodePts
, status
);
1285 values
.setSize(numCodePts
);
1286 UVector32
lengths(numCodePts
, status
);
1287 lengths
.setSize(numCodePts
);
1289 UText fu
= UTEXT_INITIALIZER
;
1290 utext_openUnicodeString(&fu
, &inString
, &status
);
1292 // Dynamic programming to find the best segmentation.
1294 // In outer loop, i is the code point index,
1295 // ix is the corresponding string (code unit) index.
1296 // They differ when the string contains supplementary characters.
1298 bool is_prev_katakana
= false;
1299 for (int32_t i
= 0; i
< numCodePts
; ++i
, ix
= inString
.moveIndex32(ix
, 1)) {
1300 if ((uint32_t)bestSnlp
.elementAti(i
) == kuint32max
) {
1305 utext_setNativeIndex(&fu
, ix
);
1306 count
= fDictionary
->matches(&fu
, maxWordSize
, numCodePts
,
1307 NULL
, lengths
.getBuffer(), values
.getBuffer(), NULL
);
1308 // Note: lengths is filled with code point lengths
1309 // The NULL parameter is the ignored code unit lengths.
1311 // if there are no single character matches found in the dictionary
1312 // starting with this character, treat character as a 1-character word
1313 // with the highest value possible, i.e. the least likely to occur.
1314 // Exclude Korean characters from this treatment, as they should be left
1315 // together by default.
1316 if ((count
== 0 || lengths
.elementAti(0) != 1) &&
1317 !fHangulWordSet
.contains(inString
.char32At(ix
))) {
1318 values
.setElementAt(maxSnlp
, count
); // 255
1319 lengths
.setElementAt(1, count
++);
1322 for (int32_t j
= 0; j
< count
; j
++) {
1323 uint32_t newSnlp
= (uint32_t)bestSnlp
.elementAti(i
) + (uint32_t)values
.elementAti(j
);
1324 int32_t ln_j_i
= lengths
.elementAti(j
) + i
;
1325 if (newSnlp
< (uint32_t)bestSnlp
.elementAti(ln_j_i
)) {
1326 bestSnlp
.setElementAt(newSnlp
, ln_j_i
);
1327 prev
.setElementAt(i
, ln_j_i
);
1332 // Katakana word in single character is pretty rare. So we apply
1333 // the following heuristic to Katakana: any continuous run of Katakana
1334 // characters is considered a candidate word with a default cost
1335 // specified in the katakanaCost table according to its length.
1337 bool is_katakana
= isKatakana(inString
.char32At(ix
));
1338 int32_t katakanaRunLength
= 1;
1339 if (!is_prev_katakana
&& is_katakana
) {
1340 int32_t j
= inString
.moveIndex32(ix
, 1);
1341 // Find the end of the continuous run of Katakana characters
1342 while (j
< inString
.length() && katakanaRunLength
< kMaxKatakanaGroupLength
&&
1343 isKatakana(inString
.char32At(j
))) {
1344 j
= inString
.moveIndex32(j
, 1);
1345 katakanaRunLength
++;
1347 if (katakanaRunLength
< kMaxKatakanaGroupLength
) {
1348 uint32_t newSnlp
= bestSnlp
.elementAti(i
) + getKatakanaCost(katakanaRunLength
);
1349 if (newSnlp
< (uint32_t)bestSnlp
.elementAti(j
)) {
1350 bestSnlp
.setElementAt(newSnlp
, j
);
1351 prev
.setElementAt(i
, i
+katakanaRunLength
); // prev[j] = i;
1355 is_prev_katakana
= is_katakana
;
1359 // Start pushing the optimal offset index into t_boundary (t for tentative).
1360 // prev[numCodePts] is guaranteed to be meaningful.
1361 // We'll first push in the reverse order, i.e.,
1362 // t_boundary[0] = numCodePts, and afterwards do a swap.
1363 UVector32
t_boundary(numCodePts
+1, status
);
1365 int32_t numBreaks
= 0;
1366 // No segmentation found, set boundary to end of range
1367 if ((uint32_t)bestSnlp
.elementAti(numCodePts
) == kuint32max
) {
1368 t_boundary
.addElement(numCodePts
, status
);
1371 for (int32_t i
= numCodePts
; i
> 0; i
= prev
.elementAti(i
)) {
1372 t_boundary
.addElement(i
, status
);
1375 U_ASSERT(prev
.elementAti(t_boundary
.elementAti(numBreaks
- 1)) == 0);
1378 // Add a break for the start of the dictionary range if there is not one
1380 if (foundBreaks
.size() == 0 || foundBreaks
.peeki() < rangeStart
) {
1381 t_boundary
.addElement(0, status
);
1385 // Now that we're done, convert positions in t_boundary[] (indices in
1386 // the normalized input string) back to indices in the original input UText
1387 // while reversing t_boundary and pushing values to foundBreaks.
1388 int32_t prevCPPos
= -1;
1389 int32_t prevUTextPos
= -1;
1390 for (int32_t i
= numBreaks
-1; i
>= 0; i
--) {
1391 int32_t cpPos
= t_boundary
.elementAti(i
);
1392 U_ASSERT(cpPos
> prevCPPos
);
1393 int32_t utextPos
= inputMap
.isValid() ? inputMap
->elementAti(cpPos
) : cpPos
+ rangeStart
;
1394 U_ASSERT(utextPos
>= prevUTextPos
);
1395 if (utextPos
> prevUTextPos
) {
1396 // Boundaries are added to foundBreaks output in ascending order.
1397 U_ASSERT(foundBreaks
.size() == 0 || foundBreaks
.peeki() < utextPos
);
1398 foundBreaks
.push(utextPos
, status
);
1400 // Normalization expanded the input text, the dictionary found a boundary
1401 // within the expansion, giving two boundaries with the same index in the
1402 // original text. Ignore the second. See ticket #12918.
1406 prevUTextPos
= utextPos
;
1409 // inString goes out of scope
1410 // inputMap goes out of scope
1417 #endif /* #if !UCONFIG_NO_BREAK_ITERATION */