1 7 package com.ibm.icu.text; 8 9 import java.io.InputStream ; 10 import java.io.DataInputStream ; 11 import java.io.FileNotFoundException ; 12 import java.io.UnsupportedEncodingException ; 13 import java.io.IOException ; 14 import java.io.FileInputStream ; 15 import java.io.OutputStreamWriter ; 16 import java.io.PrintWriter ; 17 import java.io.FileOutputStream ; 18 19 import com.ibm.icu.util.CompactByteArray; 20 21 34 public class BreakDictionary { 35 42 public static void main(String args[]) 43 throws FileNotFoundException , UnsupportedEncodingException , IOException { 44 String filename = args[0]; 45 46 BreakDictionary dictionary = new BreakDictionary(new FileInputStream (filename)); 47 48 PrintWriter out = null; 49 50 if(args.length >= 2) { 51 out = new PrintWriter (new OutputStreamWriter (new FileOutputStream (args[1]), "UnicodeLittle")); 52 } 53 54 dictionary.printWordList("", 0, out); 55 56 if (out != null) { 57 out.close(); 58 } 59 } 60 61 65 public void printWordList(String partialWord, int state, PrintWriter out) 66 throws IOException { 67 if (state == 0xFFFF) { 68 System.out.println(partialWord); 69 if (out != null) { 70 out.println(partialWord); 71 } 72 } 73 else { 74 for (int i = 0; i < numCols; i++) { 75 int newState = (at(state, i)) & 0xFFFF; 76 77 if (newState != 0) { 78 char newChar = reverseColumnMap[i]; 79 String newPartialWord = partialWord; 80 81 if (newChar != 0) { 82 newPartialWord += newChar; 83 } 84 85 printWordList(newPartialWord, newState, out); 86 } 87 } 88 } 89 } 90 91 95 private char[] reverseColumnMap = null; 96 97 101 105 private CompactByteArray columnMap = null; 106 107 110 private int numCols; 111 112 117 private int numColGroups; 118 119 130 private short[] table = null; 131 132 135 private short[] rowIndex = null; 136 137 143 private int[] rowIndexFlags = null; 144 145 153 private short[] rowIndexFlagsIndex = null; 154 155 159 private byte[] rowIndexShifts = null; 160 161 165 169 public BreakDictionary(InputStream dictionaryStream) throws IOException { 170 readDictionaryFile(new DataInputStream (dictionaryStream)); 171 } 172 173 177 public void readDictionaryFile(DataInputStream in) throws IOException { 178 int l; 179 180 in.readInt(); 182 183 l = in.readInt(); 186 char[] temp = new char[l]; 187 for (int i = 0; i < temp.length; i++) 188 temp[i] = (char)in.readShort(); 189 l = in.readInt(); 190 byte[] temp2 = new byte[l]; 191 for (int i = 0; i < temp2.length; i++) 192 temp2[i] = in.readByte(); 193 columnMap = new CompactByteArray(temp, temp2); 194 195 numCols = in.readInt(); 197 numColGroups = in.readInt(); 198 199 l = in.readInt(); 201 rowIndex = new short[l]; 202 for (int i = 0; i < rowIndex.length; i++) 203 rowIndex[i] = in.readShort(); 204 205 l = in.readInt(); 207 rowIndexFlagsIndex = new short[l]; 208 for (int i = 0; i < rowIndexFlagsIndex.length; i++) 209 rowIndexFlagsIndex[i] = in.readShort(); 210 l = in.readInt(); 211 rowIndexFlags = new int[l]; 212 for (int i = 0; i < rowIndexFlags.length; i++) 213 rowIndexFlags[i] = in.readInt(); 214 215 l = in.readInt(); 217 rowIndexShifts = new byte[l]; 218 for (int i = 0; i < rowIndexShifts.length; i++) 219 rowIndexShifts[i] = in.readByte(); 220 221 l = in.readInt(); 223 table = new short[l]; 224 for (int i = 0; i < table.length; i++) 225 table[i] = in.readShort(); 226 227 reverseColumnMap = new char[numCols]; 229 for (char c = 0; c < 0xffff; c++) { 230 int col = columnMap.elementAt(c); 231 if (col != 0) { 232 reverseColumnMap[col] = c; 233 } 234 } 235 236 in.close(); 238 } 239 240 244 253 public final short at(int row, char ch) { 254 int col = columnMap.elementAt(ch); 255 return at(row, col); 256 } 257 258 271 public final short at(int row, int col) { 272 if (cellIsPopulated(row, col)) { 273 return internalAt(rowIndex[row], col + rowIndexShifts[row]); 280 } 281 else { 282 return 0; 283 } 284 } 285 286 290 private final boolean cellIsPopulated(int row, int col) { 291 if (rowIndexFlagsIndex[row] < 0) { 295 return col == -rowIndexFlagsIndex[row]; 296 } 297 298 else { 305 int flags = rowIndexFlags[rowIndexFlagsIndex[row] + (col >> 5)]; 306 return (flags & (1 << (col & 0x1f))) != 0; 307 } 308 } 309 310 316 private final short internalAt(int row, int col) { 317 return table[row * numCols + col]; 321 } 322 } 323 324 | Popular Tags |