1 /* GENERATED SOURCE. DO NOT MODIFY. */ 2 // © 2016 and later: Unicode, Inc. and others. 3 // License & terms of use: http://www.unicode.org/copyright.html#License 4 /* 5 ******************************************************************************* 6 * Copyright (C) 2014, International Business Machines Corporation and * 7 * others. All Rights Reserved. * 8 ******************************************************************************* 9 */ 10 package android.icu.text; 11 12 import java.io.IOException; 13 import java.text.CharacterIterator; 14 15 import android.icu.lang.UCharacter; 16 import android.icu.lang.UProperty; 17 import android.icu.lang.UScript; 18 19 class BurmeseBreakEngine extends DictionaryBreakEngine { 20 21 // Constants for BurmeseBreakIterator 22 // How many words in a row are "good enough"? 23 private static final byte BURMESE_LOOKAHEAD = 3; 24 // Will not combine a non-word with a preceding dictionary word longer than this 25 private static final byte BURMESE_ROOT_COMBINE_THRESHOLD = 3; 26 // Will not combine a non-word that shares at least this much prefix with a 27 // dictionary word with a preceding word 28 private static final byte BURMESE_PREFIX_COMBINE_THRESHOLD = 3; 29 // Minimum word size 30 private static final byte BURMESE_MIN_WORD = 2; 31 32 private DictionaryMatcher fDictionary; 33 private static UnicodeSet fBurmeseWordSet; 34 private static UnicodeSet fEndWordSet; 35 private static UnicodeSet fBeginWordSet; 36 private static UnicodeSet fMarkSet; 37 38 static { 39 // Initialize UnicodeSets 40 fBurmeseWordSet = new UnicodeSet(); 41 fMarkSet = new UnicodeSet(); 42 fBeginWordSet = new UnicodeSet(); 43 44 fBurmeseWordSet.applyPattern("[[:Mymr:]&[:LineBreak=SA:]]"); fBurmeseWordSet.compact()45 fBurmeseWordSet.compact(); 46 47 fMarkSet.applyPattern("[[:Mymr:]&[:LineBreak=SA:]&[:M:]]"); 48 fMarkSet.add(0x0020); 49 fEndWordSet = new UnicodeSet(fBurmeseWordSet); 50 fBeginWordSet.add(0x1000, 0x102A); // basic consonants and independent vowels 51 52 // Compact for caching fMarkSet.compact()53 fMarkSet.compact(); fEndWordSet.compact()54 fEndWordSet.compact(); fBeginWordSet.compact()55 fBeginWordSet.compact(); 56 57 // Freeze the static UnicodeSet fBurmeseWordSet.freeze()58 fBurmeseWordSet.freeze(); fMarkSet.freeze()59 fMarkSet.freeze(); fEndWordSet.freeze()60 fEndWordSet.freeze(); fBeginWordSet.freeze()61 fBeginWordSet.freeze(); 62 } 63 BurmeseBreakEngine()64 public BurmeseBreakEngine() throws IOException { 65 super(BreakIterator.KIND_WORD, BreakIterator.KIND_LINE); 66 setCharacters(fBurmeseWordSet); 67 // Initialize dictionary 68 fDictionary = DictionaryData.loadDictionaryFor("Mymr"); 69 } 70 71 @Override equals(Object obj)72 public boolean equals(Object obj) { 73 // Normally is a singleton, but it's possible to have duplicates 74 // during initialization. All are equivalent. 75 return obj instanceof BurmeseBreakEngine; 76 } 77 78 @Override hashCode()79 public int hashCode() { 80 return getClass().hashCode(); 81 } 82 83 @Override handles(int c, int breakType)84 public boolean handles(int c, int breakType) { 85 if (breakType == BreakIterator.KIND_WORD || breakType == BreakIterator.KIND_LINE) { 86 int script = UCharacter.getIntPropertyValue(c, UProperty.SCRIPT); 87 return (script == UScript.MYANMAR); 88 } 89 return false; 90 } 91 92 @Override divideUpDictionaryRange(CharacterIterator fIter, int rangeStart, int rangeEnd, DequeI foundBreaks)93 public int divideUpDictionaryRange(CharacterIterator fIter, int rangeStart, int rangeEnd, 94 DequeI foundBreaks) { 95 96 97 if ((rangeEnd - rangeStart) < BURMESE_MIN_WORD) { 98 return 0; // Not enough characters for word 99 } 100 int wordsFound = 0; 101 int wordLength; 102 int current; 103 PossibleWord words[] = new PossibleWord[BURMESE_LOOKAHEAD]; 104 for (int i = 0; i < BURMESE_LOOKAHEAD; i++) { 105 words[i] = new PossibleWord(); 106 } 107 int uc; 108 109 fIter.setIndex(rangeStart); 110 while ((current = fIter.getIndex()) < rangeEnd) { 111 wordLength = 0; 112 113 //Look for candidate words at the current position 114 int candidates = words[wordsFound%BURMESE_LOOKAHEAD].candidates(fIter, fDictionary, rangeEnd); 115 116 // If we found exactly one, use that 117 if (candidates == 1) { 118 wordLength = words[wordsFound%BURMESE_LOOKAHEAD].acceptMarked(fIter); 119 wordsFound += 1; 120 } 121 122 // If there was more than one, see which one can take us forward the most words 123 else if (candidates > 1) { 124 boolean foundBest = false; 125 // If we're already at the end of the range, we're done 126 if (fIter.getIndex() < rangeEnd) { 127 do { 128 int wordsMatched = 1; 129 if (words[(wordsFound+1)%BURMESE_LOOKAHEAD].candidates(fIter, fDictionary, rangeEnd) > 0) { 130 if (wordsMatched < 2) { 131 // Followed by another dictionary word; mark first word as a good candidate 132 words[wordsFound%BURMESE_LOOKAHEAD].markCurrent(); 133 wordsMatched = 2; 134 } 135 136 // If we're already at the end of the range, we're done 137 if (fIter.getIndex() >= rangeEnd) { 138 break; 139 } 140 141 // See if any of the possible second words is followed by a third word 142 do { 143 // If we find a third word, stop right away 144 if (words[(wordsFound+2)%BURMESE_LOOKAHEAD].candidates(fIter, fDictionary, rangeEnd) > 0) { 145 words[wordsFound%BURMESE_LOOKAHEAD].markCurrent(); 146 foundBest = true; 147 break; 148 } 149 } while (words[(wordsFound+1)%BURMESE_LOOKAHEAD].backUp(fIter)); 150 } 151 } while (words[wordsFound%BURMESE_LOOKAHEAD].backUp(fIter) && !foundBest); 152 } 153 wordLength = words[wordsFound%BURMESE_LOOKAHEAD].acceptMarked(fIter); 154 wordsFound += 1; 155 } 156 157 // We come here after having either found a word or not. We look ahead to the 158 // next word. If it's not a dictionary word, we will combine it with the word we 159 // just found (if there is one), but only if the preceding word does not exceed 160 // the threshold. 161 // The text iterator should now be positioned at the end of the word we found. 162 if (fIter.getIndex() < rangeEnd && wordLength < BURMESE_ROOT_COMBINE_THRESHOLD) { 163 // If it is a dictionary word, do nothing. If it isn't, then if there is 164 // no preceding word, or the non-word shares less than the minimum threshold 165 // of characters with a dictionary word, then scan to resynchronize 166 if (words[wordsFound%BURMESE_LOOKAHEAD].candidates(fIter, fDictionary, rangeEnd) <= 0 && 167 (wordLength == 0 || 168 words[wordsFound%BURMESE_LOOKAHEAD].longestPrefix() < BURMESE_PREFIX_COMBINE_THRESHOLD)) { 169 // Look for a plausible word boundary 170 int remaining = rangeEnd - (current + wordLength); 171 int pc = fIter.current(); 172 int chars = 0; 173 for (;;) { 174 fIter.next(); 175 uc = fIter.current(); 176 chars += 1; 177 if (--remaining <= 0) { 178 break; 179 } 180 if (fEndWordSet.contains(pc) && fBeginWordSet.contains(uc)) { 181 // Maybe. See if it's in the dictionary. 182 int candidate = words[(wordsFound + 1) %BURMESE_LOOKAHEAD].candidates(fIter, fDictionary, rangeEnd); 183 fIter.setIndex(current + wordLength + chars); 184 if (candidate > 0) { 185 break; 186 } 187 } 188 pc = uc; 189 } 190 191 // Bump the word count if there wasn't already one 192 if (wordLength <= 0) { 193 wordsFound += 1; 194 } 195 196 // Update the length with the passed-over characters 197 wordLength += chars; 198 } else { 199 // Backup to where we were for next iteration 200 fIter.setIndex(current+wordLength); 201 } 202 } 203 204 // Never stop before a combining mark. 205 int currPos; 206 while ((currPos = fIter.getIndex()) < rangeEnd && fMarkSet.contains(fIter.current())) { 207 fIter.next(); 208 wordLength += fIter.getIndex() - currPos; 209 } 210 211 // Look ahead for possible suffixes if a dictionary word does not follow. 212 // We do this in code rather than using a rule so that the heuristic 213 // resynch continues to function. For example, one of the suffix characters 214 // could be a typo in the middle of a word. 215 // NOT CURRENTLY APPLICABLE TO BURMESE 216 217 // Did we find a word on this iteration? If so, push it on the break stack 218 if (wordLength > 0) { 219 foundBreaks.push(Integer.valueOf(current + wordLength)); 220 } 221 } 222 223 // Don't return a break for the end of the dictionary range if there is one there 224 if (foundBreaks.peek() >= rangeEnd) { 225 foundBreaks.pop(); 226 wordsFound -= 1; 227 } 228 229 return wordsFound; 230 } 231 232 } 233