Debian Codegraph @

extra/src/uk/me/parabola/util/CollationRules.java

raw symbols SCIP
1 /*
2 * Copyright (C) 2014.
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 3 or
6 * version 2 as published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
12 */
13 package uk.me.parabola.util;
14
15
16 import java.io.BufferedReader;
17 import java.io.FileReader;
18 import java.io.IOException;
19 import java.nio.ByteBuffer;
20 import java.nio.CharBuffer;
21 import java.nio.charset.CharacterCodingException;
22 import java.nio.charset.Charset;
23 import java.nio.charset.CharsetDecoder;
24 import java.util.Formatter;
25 import java.util.HashMap;
26 import java.util.Map;
27 import java.util.NavigableSet;
28 import java.util.TreeSet;
29 import java.util.regex.Matcher;
30 import java.util.regex.Pattern;
31
32 import com.ibm.icu.text.CollationElementIterator;
33 import com.ibm.icu.text.Collator;
34 import com.ibm.icu.text.RuleBasedCollator;
35
36 //import java.text.CollationElementIterator;
37 //import java.text.Collator;
38 //import java.text.RuleBasedCollator;
39
40 /**
41 * Create a set of rules for a given code page.
42 *
43 * Should be usable, perhaps with a few tweaks.
44 * Works with unicode too, need to choose which blocks to take for unicode.
45 *
46 * @author Steve Ratcliffe
47 */
48 public class CollationRules {
49
50 private CharsetDecoder decoder;
51 private final NavigableSet<CharPosition> positionMap = new TreeSet<>();
52 private final NavigableSet<CharPosition> basePositionMap = new TreeSet<>();
53 private final Map<Character, CharPosition> charMap = new HashMap<>();
54 private boolean isUnicode;
55 private Charset charset;
56
57 public static void main(String[] args) {
58 String charsetName = args[0];
59 CollationRules main = new CollationRules();
60 main.go(charsetName);
61 }
62
63 private void go(String charsetName) {
64 RuleBasedCollator col = (RuleBasedCollator) Collator.getInstance();
65
66 charset = Charset.forName(charsetName);
67 if ("utf-8".equalsIgnoreCase(charsetName))
68 isUnicode = true;
69 decoder = charset.newDecoder();
70
71 if (isUnicode)
72 addUnicode();
73 else
74 addBlock(col, 0);
75
76 printCharMap();
77 printExpansions();
78 }
79
80 private void addBlock(RuleBasedCollator col, int block) {
81 for (int i = 0; i < 0x100; i++) {
82 int ch = (block << 8) + i;
83 String testString = getString(ch);
84 char conv = testString.charAt(0);
85 if (Character.getType(conv) == Character.UNASSIGNED || conv == 0xfffd)
86 continue;
87 CollationElementIterator it = col.getCollationElementIterator(testString);
88
89 System.out.printf("# %s ", fmtChar(testString.charAt(0)));
90 int next;
91 int index = 0;
92 CharPosition cp = new CharPosition(0);
93 while ((next = it.next()) != CollationElementIterator.NULLORDER) {
94 if (index == 0) {
95 cp = new CharPosition(ch);
96 cp.setOrder(next);
97 } else {
98 assert index < 3;
99 if ((next & 0xffff0000) == 0) {
100 cp.addOrder(next, index);
101 } else {
102 cp.addChar(new CharPosition(ch));
103 cp.setOrder(next);
104 }
105 }
106
107 index++;
108 }
109 System.out.printf(" %s %d", cp, Character.getType(cp.getUnicode()));
110 System.out.println();
111
112 tweak(cp);
113 if (ch > 0)
114 positionMap.add(cp);
115 if (cp.nextChar == null) {
116 basePositionMap.add(cp);
117 charMap.put(conv, cp);
118 }
119 }
120 }
121
122 private void addUnicode() {
123 Pattern pat = Pattern.compile("([0-9A-F]{4,5}) ? ; \\[[.*](.*)\\] #.*");
124 try (FileReader r = new FileReader("allkeys.txt")) {
125 try (BufferedReader br = new BufferedReader(r)) {
126 String line;
127 while ((line = br.readLine()) != null) {
128 Matcher matcher = pat.matcher(line);
129 if (matcher.matches()) {
130 String weights = matcher.group(2);
131 int ch = Integer.parseInt(matcher.group(1), 16);
132 if (ch > 0xffff)
133 continue;
134
135 System.out.printf("# %04x %s ", ch, fmtChar(ch));
136
137 String[] split = weights.split("]\\[[.*]");
138
139 int index = 0;
140 CharPosition cp = new CharPosition(0);
141
142 for (String s : split) {
143 String[] ws = s.split("\\.");
144 int next = Integer.parseInt(ws[0], 16) << 16
145 | ((Integer.parseInt(ws[1], 16) << 8) & 0xff00)
146 | ((Integer.parseInt(ws[2], 16)) & 0xff);
147
148 if (index == 0) {
149 cp = new CharPosition(ch);
150 cp.setOrder(next);
151 } else {
152 if ((next & 0xffff0000) == 0) {
153 cp.addOrder(next, index);
154 } else {
155 cp.addChar(new CharPosition(ch));
156 cp.setOrder(next);
157 }
158 }
159 index++;
160 }
161
162 System.out.printf(" %s %d\n", cp, Character.getType(cp.getUnicode()));
163
164 tweak(cp);
165 if (ch > 0)
166 positionMap.add(cp);
167 if (cp.nextChar == null) {
168 basePositionMap.add(cp);
169 charMap.put((char) ch, cp);
170 }
171 } else {
172 System.out.println("# NOMATCH: " + line);
173 }
174 }
175
176 }
177 } catch (IOException e) {
178 e.printStackTrace();
179 }
180 }
181
182 /**
183 * Fix up a few characters that we always want to be in well known places.
184 *
185 * @param cp The position to change.
186 */
187 private void tweak(CharPosition cp) {
188 if (cp.val < 8)
189 cp.third = cp.val + 7;
190
191 if (!isUnicode) {
192 switch (cp.getUnicode()) {
193 case '¼':
194 cp.nextChar = charMap.get('/').copy();
195 cp.nextChar.nextChar = charMap.get('4');
196 break;
197 case '½':
198 cp.nextChar = charMap.get('/').copy();
199 cp.nextChar.nextChar = charMap.get('2');
200 break;
201 case '¾':
202 cp.nextChar = charMap.get('/').copy();
203 cp.nextChar.nextChar = charMap.get('4');
204 break;
205 }
206 }
207
208 switch (cp.getUnicode()) {
209 case '˜':
210 CharPosition tilde = charMap.get('~');
211 cp.first = tilde.first;
212 cp.second = tilde.second + 1;
213 cp.third = tilde.third + 1;
214 cp.nextChar = null;
215 break;
216 }
217 }
218
219 private String getString(int i) {
220 if (isUnicode)
221 return new String(new char[]{(char) i});
222 else {
223 byte[] b = {(byte) i};
224 return new String(b, 0, 1, charset);
225 }
226 }
227
228 private void printCharMap() {
229
230 Formatter chars = new Formatter();
231 chars.format("\n");
232
233 CharPosition last = new CharPosition(0);
234 last.first = 0;
235 for (CharPosition cp : positionMap) {
236 if (cp.isExpansion())
237 continue;
238
239 if (cp.first != last.first) {
240 chars.format("\n < ");
241 } else if (cp.second != last.second) {
242 chars.format(" ; ");
243 } else if (cp.third != last.third) {
244 chars.format(",");
245 } else {
246 chars.format("=");
247 }
248 last = cp;
249 int uni = toUnicode(cp.val);
250 chars.format("%s", fmtChar(uni));
251 }
252
253 System.out.println(chars);
254 }
255
256 private void printExpansions() {
257 for (CharPosition cp : positionMap) {
258 if (!cp.isExpansion())
259 continue;
260
261 Formatter fmt = new Formatter();
262
263 //noinspection MalformedFormatString
264 fmt.format("expand %c to", cp.getUnicode());
265
266 boolean ok = true;
267 for (CharPosition cp2 = cp; cp2 != null; cp2 = cp2.nextChar) {
268 cp2.second = 0x50000;
269 int top = (cp2.third >> 16) & 0xff;
270 cp2.third = (top == 0x9e || top == 0xa2 || top == 0x2b) ? 0x9b0000 : 0;
271
272 CharPosition floor = basePositionMap.ceiling(cp2);
273 if (floor == null || floor.getUnicode() == 0xfffd) {
274 fmt.format(" NF");
275 ok = false;
276 } else {
277 fmt.format(" %s", fmtChar(floor.getUnicode()));
278 }
279 }
280
281 System.out.println((ok ? "" : "# ") + fmt.toString());
282
283 // Print comments to help find problems.
284 for (CharPosition cp2 = cp; cp2 != null; cp2 = cp2.nextChar) {
285 CharPosition floor = basePositionMap.ceiling(cp2);
286 if (floor == null) {
287 System.out.println("#FIX: NF ref=" + cp2);
288 } else {
289 System.out.println("#floor is " + fmtChar(toUnicode(floor.val)) + ", " + floor + ", ref is " + cp2);
290 }
291 }
292 }
293 }
294
295 private String fmtChar(int val) {
296 boolean asChar = true;
297 switch (val) {
298 case '<':
299 case ';':
300 case ',':
301 case '=':
302 case '#':
303 asChar = false;
304 break;
305 default:
306
307 switch (Character.getType(val)) {
308 case Character.UNASSIGNED:
309 case Character.NON_SPACING_MARK:
310 case Character.FORMAT:
311 case Character.CONTROL:
312 case Character.SPACE_SEPARATOR:
313 case Character.LINE_SEPARATOR:
314 case Character.PARAGRAPH_SEPARATOR:
315 asChar = false;
316 }
317 }
318
319 if (asChar) {
320 //noinspection MalformedFormatString
321 return String.format("%c", val);
322 } else {
323 return String.format("%04x", val);
324 }
325 }
326
327 private int toUnicode(int c) {
328 if (isUnicode)
329 return c;
330 ByteBuffer b = ByteBuffer.allocate(1);
331 b.put((byte) c);
332 b.flip();
333 try {
334 CharBuffer chars = decoder.decode(b);
335 return chars.charAt(0);
336 } catch (CharacterCodingException e) {
337 return '?';
338 }
339 }
340
341
342 class CharPosition implements Comparable<CharPosition> {
343 private final int val;
344 private int first;
345 private int second;
346 private int third;
347 private CharPosition nextChar;
348
349 public CharPosition(int charValue) {
350 this.val = charValue;
351 }
352
353 public int compareTo(CharPosition other) {
354 if (other.first == first)
355 return compareSecond(other);
356 else if (first < other.first)
357 return -1;
358 else
359 return 1;
360 }
361
362 private int compareSecond(CharPosition c2) {
363 if (c2.second == second)
364 return compareThird(c2);
365 else if (second < c2.second)
366 return -1;
367 else
368 return 1;
369 }
370
371 private int compareThird(CharPosition c2) {
372 if (third == c2.third)
373 return new Integer(val).compareTo(c2.val);
374 else if (third < c2.third)
375 return -1;
376 else
377 return 1;
378 }
379
380 public String toString() {
381 Formatter fmt = new Formatter();
382 toString(fmt);
383
384 return fmt.toString();
385 }
386
387 private void toString(Formatter fmt) {
388 fmt.format("[%04x %02x %02x]", first, second, third);
389 if (nextChar != null)
390 nextChar.toString(fmt);
391 }
392
393 public void setOrder(int next) {
394 if (nextChar != null) {
395 nextChar.setOrder(next);
396 return;
397 }
398 first = (next >> 16) & 0xffff;
399 second = (next << 8) & 0xff0000;
400 third = (next << 16) & 0xff0000;
401 }
402
403 public void addOrder(int next, int count) {
404 assert ((next >>> 16) & 0xffff) == 0;
405 if (this.nextChar != null) {
406 this.nextChar.addOrder(next, count);
407 return;
408 }
409 second += ((next >> 8) & 0xff) << (2-count)*8;
410 third += ((next) & 0xff) << (2-count)*8;
411 }
412
413 public boolean isExpansion() {
414 return nextChar != null;
415 }
416
417 public void addChar(CharPosition pos) {
418 if (nextChar != null) {
419 nextChar.addChar(pos);
420 return;
421 }
422 nextChar = pos;
423 }
424
425 public int getUnicode() {
426 return toUnicode(val);
427 }
428
429 public CharPosition copy() {
430 CharPosition cp = new CharPosition(this.val);
431 cp.first = this.first;
432 cp.second = this.second;
433 cp.third = this.third;
434 return cp;
435 }
436 }
437 }