forked from tberg12/ocular
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Charset.java
584 lines (524 loc) · 25.8 KB
/
Charset.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
package edu.berkeley.cs.nlp.ocular.data.textreader;
import static edu.berkeley.cs.nlp.ocular.util.CollectionHelper.makeMap;
import static edu.berkeley.cs.nlp.ocular.util.CollectionHelper.makeSet;
import static edu.berkeley.cs.nlp.ocular.util.CollectionHelper.setUnion;
import static edu.berkeley.cs.nlp.ocular.util.Tuple2.Tuple2;
import static edu.berkeley.cs.nlp.ocular.util.Tuple3.Tuple3;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import edu.berkeley.cs.nlp.ocular.util.StringHelper;
import edu.berkeley.cs.nlp.ocular.util.Tuple2;
import edu.berkeley.cs.nlp.ocular.util.Tuple3;
import tberg.murphy.indexer.Indexer;
/**
* @author Dan Garrette ([email protected])
*/
public class Charset {
public static final String SPACE = " ";
public static final String HYPHEN = "-";
public static final Set<String> LOWERCASE_LATIN_LETTERS = makeSet("a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z", "\u03B4");
public static final Set<String> LOWERCASE_VOWELS = makeSet("a", "e", "i", "o", "u");
public static final Set<String> SPECIALS = makeSet("\uA75D", "\u204A", "\uA76F", "\uA749", "\uA753", "\uA770", "\uA751", "\uA757");
public static final Map<String,String> LIGATURES = makeMap(Tuple2("Æ","AE"), Tuple2("æ","ae"), Tuple2("Œ","OE"), Tuple2("œ","oe"), Tuple2("\uA75D", "rum"), Tuple2("\u204A", "et"), Tuple2("\uA76F", "con"), Tuple2("\uA749", "lis"), Tuple2("\uA753", "pro"), Tuple2("\uA770", "us"), Tuple2("\uA751", "per"), Tuple2("\uA757", "que"));
public static final String LONG_S = "\u017F"; // ſ
public static final Set<String> BANNED_CHARS = makeSet("@", "$", "%");
/**
* Punctuation symbols that should be made available for any language,
* regardless of whether they are seen in the language model training
* material.
*/
public static final Set<String> UNIV_PUNC = makeSet("&", ".", ",", "[", "]", HYPHEN, "*", "§", "¶");
private static boolean isPunctuation(char c) {
return !Character.isWhitespace(c) && !Character.isAlphabetic(c) && !Character.isDigit(c);
}
public static boolean isPunctuationChar(String s) {
for (char c: removeAnyDiacriticFromChar(s).toCharArray())
if (!isPunctuation(c)) return false;
return true;
}
public static final String GRAVE_COMBINING = "\u0300";
public static final String ACUTE_COMBINING = "\u0301";
public static final String CIRCUMFLEX_COMBINING = "\u0302";
public static final String TILDE_COMBINING = "\u0303";
public static final String MACRON_COMBINING = "\u0304"; // shorter overline
public static final String BREVE_COMBINING = "\u0306";
public static final String DIAERESIS_COMBINING = "\u0308"; // == umlaut
public static final String CEDILLA_COMBINING = "\u0327";
public static final String MACRON_BELOW_COMBINING = "\0331";
private static boolean isCombiningChar(String c) {
return (("\u0300".compareTo(c) <= 0 && c.compareTo("\u036F") <= 0) ||
("\u1AB0".compareTo(c) <= 0 && c.compareTo("\u1AFF") <= 0) ||
("\u1DC0".compareTo(c) <= 0 && c.compareTo("\u1DFF") <= 0) ||
("\u20D0".compareTo(c) <= 0 && c.compareTo("\u20FF") <= 0) ||
("\uFE20".compareTo(c) <= 0 && c.compareTo("\uFE2F") <= 0));
}
public static final String GRAVE_ESCAPE = "\\`";
public static final String ACUTE_ESCAPE = "\\'";
public static final String CIRCUMFLEX_ESCAPE = "\\^";
public static final String TILDE_ESCAPE = "\\~";
public static final String MACRON_ESCAPE = "\\-"; // shorter overline
public static final String BREVE_ESCAPE = "\\v";
public static final String DIAERESIS_ESCAPE = "\\\""; // == umlaut
public static final String CEDILLA_ESCAPE = "\\c";
public static final String MACRON_BELOW_ESCAPE = "\\_";
private static final HashMap<String,String> COMBINING_TO_ESCAPE_MAP = new HashMap<String,String>();
static {
COMBINING_TO_ESCAPE_MAP.put(GRAVE_COMBINING, GRAVE_ESCAPE);
COMBINING_TO_ESCAPE_MAP.put(ACUTE_COMBINING, ACUTE_ESCAPE);
COMBINING_TO_ESCAPE_MAP.put(CIRCUMFLEX_COMBINING, CIRCUMFLEX_ESCAPE);
COMBINING_TO_ESCAPE_MAP.put(TILDE_COMBINING, TILDE_ESCAPE);
COMBINING_TO_ESCAPE_MAP.put(MACRON_COMBINING, MACRON_ESCAPE);
COMBINING_TO_ESCAPE_MAP.put(BREVE_COMBINING, BREVE_ESCAPE);
COMBINING_TO_ESCAPE_MAP.put(DIAERESIS_COMBINING, DIAERESIS_ESCAPE);
COMBINING_TO_ESCAPE_MAP.put(CEDILLA_COMBINING, CEDILLA_ESCAPE);
COMBINING_TO_ESCAPE_MAP.put(MACRON_BELOW_COMBINING, MACRON_BELOW_ESCAPE);
}
// private static String combiningToEscape(String combiningChar) {
// String escape = COMBINING_TO_ESCAPE_MAP.get(combiningChar);
// if (escape != null)
// return escape;
// else
// throw new RuntimeException("Unrecognized combining char: [" + combiningChar + "] (" + StringHelper.toUnicode(combiningChar) + ")");
// }
private static String escapeToCombining(String escSeq) {
if (GRAVE_ESCAPE.equals(escSeq))
return GRAVE_COMBINING;
else if (ACUTE_ESCAPE.equals(escSeq))
return ACUTE_COMBINING;
else if (CIRCUMFLEX_ESCAPE.equals(escSeq))
return CIRCUMFLEX_COMBINING;
else if (TILDE_ESCAPE.equals(escSeq))
return TILDE_COMBINING;
else if (MACRON_ESCAPE.equals(escSeq))
return MACRON_COMBINING;
else if (BREVE_ESCAPE.equals(escSeq))
return BREVE_COMBINING;
else if (DIAERESIS_ESCAPE.equals(escSeq))
return DIAERESIS_COMBINING;
else if (CEDILLA_ESCAPE.equals(escSeq))
return CEDILLA_COMBINING;
else if (MACRON_BELOW_ESCAPE.equals(escSeq))
return MACRON_BELOW_COMBINING;
else
throw new RuntimeException("Unrecognized escape sequence: [" + escSeq + "]");
}
private static final Map<String, String> PRECOMPOSED_TO_ESCAPED_MAP = new HashMap<String, String>();
static {
PRECOMPOSED_TO_ESCAPED_MAP.put("à", "\\`a"); // \`a
PRECOMPOSED_TO_ESCAPED_MAP.put("á", "\\'a"); // \'a
PRECOMPOSED_TO_ESCAPED_MAP.put("â", "\\^a"); // \^a
PRECOMPOSED_TO_ESCAPED_MAP.put("ä", "\\\"a"); // \"a
PRECOMPOSED_TO_ESCAPED_MAP.put("ã", "\\~a"); // \~a
PRECOMPOSED_TO_ESCAPED_MAP.put("ā", "\\-a"); // \-a
PRECOMPOSED_TO_ESCAPED_MAP.put("ă", "\\va"); // \va
PRECOMPOSED_TO_ESCAPED_MAP.put("è", "\\`e"); // \`e
PRECOMPOSED_TO_ESCAPED_MAP.put("é", "\\'e"); // \'e
PRECOMPOSED_TO_ESCAPED_MAP.put("ê", "\\^e"); // \^e
PRECOMPOSED_TO_ESCAPED_MAP.put("ë", "\\\"e"); // \"e
PRECOMPOSED_TO_ESCAPED_MAP.put("ẽ", "\\~e"); // \~e
PRECOMPOSED_TO_ESCAPED_MAP.put("ē", "\\-e"); // \-e
PRECOMPOSED_TO_ESCAPED_MAP.put("ĕ", "\\ve"); // \ve
PRECOMPOSED_TO_ESCAPED_MAP.put("ì", "\\`i"); // \`i
PRECOMPOSED_TO_ESCAPED_MAP.put("í", "\\'i"); // \'i
PRECOMPOSED_TO_ESCAPED_MAP.put("î", "\\^i"); // \^i
PRECOMPOSED_TO_ESCAPED_MAP.put("ï", "\\\"i"); // \"i
PRECOMPOSED_TO_ESCAPED_MAP.put("ĩ", "\\~i"); // \~i
PRECOMPOSED_TO_ESCAPED_MAP.put("ī", "\\-i"); // \-i
PRECOMPOSED_TO_ESCAPED_MAP.put("ĭ", "\\vi"); // \vi
//PRECOMPOSED_TO_ESCAPED_MAP.put("ı", "\\ii"); // \ii
PRECOMPOSED_TO_ESCAPED_MAP.put("ò", "\\`o"); // \`o
PRECOMPOSED_TO_ESCAPED_MAP.put("ó", "\\'o"); // \'o
PRECOMPOSED_TO_ESCAPED_MAP.put("ô", "\\^o"); // \^o
PRECOMPOSED_TO_ESCAPED_MAP.put("ö", "\\\"o"); // \"o
PRECOMPOSED_TO_ESCAPED_MAP.put("õ", "\\~o"); // \~o
PRECOMPOSED_TO_ESCAPED_MAP.put("ō", "\\-o"); // \-o
PRECOMPOSED_TO_ESCAPED_MAP.put("ŏ", "\\vo"); // \vo
PRECOMPOSED_TO_ESCAPED_MAP.put("ù", "\\`u"); // \`u
PRECOMPOSED_TO_ESCAPED_MAP.put("ú", "\\'u"); // \'u
PRECOMPOSED_TO_ESCAPED_MAP.put("û", "\\^u"); // \^u
PRECOMPOSED_TO_ESCAPED_MAP.put("ü", "\\\"u"); // \"u
PRECOMPOSED_TO_ESCAPED_MAP.put("ũ", "\\~u"); // \~u
PRECOMPOSED_TO_ESCAPED_MAP.put("ū", "\\-u"); // \-u
PRECOMPOSED_TO_ESCAPED_MAP.put("ŭ", "\\vu"); // \vu
PRECOMPOSED_TO_ESCAPED_MAP.put("ñ", "\\~n"); // \~n
PRECOMPOSED_TO_ESCAPED_MAP.put("ç", "\\cc"); // \cc
PRECOMPOSED_TO_ESCAPED_MAP.put("À", "\\`A"); // \`A
PRECOMPOSED_TO_ESCAPED_MAP.put("Á", "\\'A"); // \'A
PRECOMPOSED_TO_ESCAPED_MAP.put("Â", "\\^A"); // \^A
PRECOMPOSED_TO_ESCAPED_MAP.put("Ä", "\\\"A"); // \"A
PRECOMPOSED_TO_ESCAPED_MAP.put("Ã", "\\~A"); // \~A
PRECOMPOSED_TO_ESCAPED_MAP.put("Ā", "\\-A"); // \-A
PRECOMPOSED_TO_ESCAPED_MAP.put("Ă", "\\vA"); // \vA
PRECOMPOSED_TO_ESCAPED_MAP.put("È", "\\`E"); // \`E
PRECOMPOSED_TO_ESCAPED_MAP.put("É", "\\'E"); // \'E
PRECOMPOSED_TO_ESCAPED_MAP.put("Ê", "\\^E"); // \^E
PRECOMPOSED_TO_ESCAPED_MAP.put("Ë", "\\\"E"); // \"E
PRECOMPOSED_TO_ESCAPED_MAP.put("Ẽ", "\\~E"); // \~E
PRECOMPOSED_TO_ESCAPED_MAP.put("Ē", "\\-E"); // \-E
PRECOMPOSED_TO_ESCAPED_MAP.put("Ĕ", "\\vE"); // \ve
PRECOMPOSED_TO_ESCAPED_MAP.put("Ì", "\\`I"); // \`I
PRECOMPOSED_TO_ESCAPED_MAP.put("Í", "\\'I"); // \'I
PRECOMPOSED_TO_ESCAPED_MAP.put("Î", "\\^I"); // \^I
PRECOMPOSED_TO_ESCAPED_MAP.put("Ï", "\\\"I"); // \"I
PRECOMPOSED_TO_ESCAPED_MAP.put("Ĩ", "\\~I"); // \~I
PRECOMPOSED_TO_ESCAPED_MAP.put("Ī", "\\-I"); // \-I
PRECOMPOSED_TO_ESCAPED_MAP.put("Ĭ", "\\vI"); // \vI
PRECOMPOSED_TO_ESCAPED_MAP.put("Ò", "\\`O"); // \`O
PRECOMPOSED_TO_ESCAPED_MAP.put("Ó", "\\'O"); // \'O
PRECOMPOSED_TO_ESCAPED_MAP.put("Ô", "\\^O"); // \^O
PRECOMPOSED_TO_ESCAPED_MAP.put("Ö", "\\\"O"); // \"O
PRECOMPOSED_TO_ESCAPED_MAP.put("Õ", "\\~O"); // \~O
PRECOMPOSED_TO_ESCAPED_MAP.put("Ō", "\\-O"); // \-O
PRECOMPOSED_TO_ESCAPED_MAP.put("Ŏ", "\\vO"); // \vO
PRECOMPOSED_TO_ESCAPED_MAP.put("Ù", "\\`U"); // \`U
PRECOMPOSED_TO_ESCAPED_MAP.put("Ú", "\\'U"); // \'U
PRECOMPOSED_TO_ESCAPED_MAP.put("Û", "\\^U"); // \^U
PRECOMPOSED_TO_ESCAPED_MAP.put("Ü", "\\\"U"); // \"U
PRECOMPOSED_TO_ESCAPED_MAP.put("Ũ", "\\~U"); // \~U
PRECOMPOSED_TO_ESCAPED_MAP.put("Ū", "\\-U"); // \-U
PRECOMPOSED_TO_ESCAPED_MAP.put("Ŭ", "\\vU"); // \vU
PRECOMPOSED_TO_ESCAPED_MAP.put("Ñ", "\\~N"); // \~N
PRECOMPOSED_TO_ESCAPED_MAP.put("Ç", "\\cC"); // \cC
// note: superscript is marked \s as in superscript o = \so and superscript r is \sr
//note for "breve" (u over letter) mark \va
}
private static final Map<String, String> PRECOMPOSED_TO_COMBINED_MAP = new HashMap<String, String>();
static {
for (Map.Entry<String, String> entry : PRECOMPOSED_TO_ESCAPED_MAP.entrySet()) {
String value = entry.getValue();
String baseChar = value.substring(value.length() - 1);
String escapeCodes = value.substring(0, value.length() - 1);
if (escapeCodes.length() % 2 != 0) throw new RuntimeException("problem with precomposed mapping: " + value);
StringBuilder baseWithCombining = new StringBuilder(baseChar);
for (int i = escapeCodes.length() - 2; i >= 0; i -= 2)
baseWithCombining.append(escapeToCombining(escapeCodes.substring(i, i + 2)));
PRECOMPOSED_TO_COMBINED_MAP.put(entry.getKey(), baseWithCombining.toString());
}
}
private static final Map<String, String> COMBINED_TO_PRECOMPOSED_MAP = new HashMap<String, String>();
static {
for (Map.Entry<String, String> entry : PRECOMPOSED_TO_COMBINED_MAP.entrySet()) {
COMBINED_TO_PRECOMPOSED_MAP.put(entry.getValue(), entry.getKey());
}
}
public static final Set<String> CHARS_THAT_CAN_BE_REPLACED = setUnion(LOWERCASE_LATIN_LETTERS, makeSet("ç")); // TODO: Change this?
public static final Set<String> VALID_CHAR_SUBSTITUTIONS = setUnion(LOWERCASE_LATIN_LETTERS, SPECIALS); // TODO: Change this?
public static final Set<String> CHARS_THAT_CAN_DOUBLED = LOWERCASE_LATIN_LETTERS; // TODO: Change this?
public static final Set<String> CHARS_THAT_CAN_BE_DECORATED_WITH_AN_ELISION_TILDE = LOWERCASE_LATIN_LETTERS; // TODO: Change this?
public static final Set<String> CHARS_THAT_CAN_BE_ELIDED = LOWERCASE_LATIN_LETTERS; // TODO: Change this?
private static final Set<String> COMBINING_DIACRITICS_THAT_CAN_BE_DISREGARDED = makeSet(GRAVE_COMBINING, ACUTE_COMBINING);
public static final Set<String> LETTERS_WITH_DISREGARDEDABLE_DIACRITICS = LOWERCASE_VOWELS;
public static Set<Integer> makePunctSet(Indexer<String> charIndexer) {
Set<Integer> punctSet = new HashSet<Integer>();
for (String c : charIndexer.getObjects()) {
if (isPunctuationChar(c))
punctSet.add(charIndexer.getIndex(c));
}
return punctSet;
}
public static Set<Integer> makeCanBeReplacedSet(Indexer<String> charIndexer) {
Set<Integer> canBeReplaced = new HashSet<Integer>();
for (String c : charIndexer.getObjects()) {
if (CHARS_THAT_CAN_BE_REPLACED.contains(c))
canBeReplaced.add(charIndexer.getIndex(c));
}
return canBeReplaced;
}
public static Set<Integer> makeValidSubstitutionCharsSet(Indexer<String> charIndexer) {
Set<Integer> validSubstitutionChars = new HashSet<Integer>();
for (String c : charIndexer.getObjects()) {
if (VALID_CHAR_SUBSTITUTIONS.contains(c))
validSubstitutionChars.add(charIndexer.getIndex(c));
}
return validSubstitutionChars;
}
public static Set<Integer> makeValidDoublableSet(Indexer<String> charIndexer) {
Set<Integer> validDoublableChars = new HashSet<Integer>();
for (String c : charIndexer.getObjects()) {
if (CHARS_THAT_CAN_DOUBLED.contains(c))
validDoublableChars.add(charIndexer.getIndex(c));
}
return validDoublableChars;
}
public static Set<Integer> makeCanBeElidedSet(Indexer<String> charIndexer) {
Set<Integer> canBeElided = new HashSet<Integer>();
for (String c : charIndexer.getObjects()) {
if (CHARS_THAT_CAN_BE_ELIDED.contains(c))
canBeElided.add(charIndexer.getIndex(c));
}
return canBeElided;
}
public static Map<Integer,Integer> makeAddTildeMap(Indexer<String> charIndexer) {
Map<Integer,Integer> m = new HashMap<Integer, Integer>();
for (String original : charIndexer.getObjects()) {
Tuple2<String,List<String>> originalLetterAndCombiningDiacritics = normalizeCharSeparateDiacritics(original);
String baseLetter = originalLetterAndCombiningDiacritics._1;
if (CHARS_THAT_CAN_BE_DECORATED_WITH_AN_ELISION_TILDE.contains(original)) {
m.put(charIndexer.getIndex(original), charIndexer.getIndex(addTilde(baseLetter)));
}
else if (LETTERS_WITH_DISREGARDEDABLE_DIACRITICS.contains(baseLetter)) {
for (String diacritic : originalLetterAndCombiningDiacritics._2) {
if (COMBINING_DIACRITICS_THAT_CAN_BE_DISREGARDED.contains(diacritic)) {
m.put(charIndexer.getIndex(original), charIndexer.getIndex(addTilde(baseLetter)));
break;
}
}
}
}
return m;
}
public static Map<Integer,List<Integer>> makeLigatureMap(Indexer<String> charIndexer) {
Map<Integer,List<Integer>> m = new HashMap<Integer, List<Integer>>();
for (Map.Entry<String,String> entry : LIGATURES.entrySet()) {
List<String> ligature = readNormalizeCharacters(entry.getKey());
if (ligature.size() > 1) throw new RuntimeException("Ligature ["+entry.getKey()+"] has more than one character: "+ligature);
List<Integer> l = new ArrayList<Integer>();
for (String c : readNormalizeCharacters(entry.getValue()))
l.add(charIndexer.getIndex(c));
m.put(charIndexer.getIndex(ligature.get(0)), l);
}
return m;
}
public static Map<Integer,Integer> makeDiacriticDisregardMap(Indexer<String> charIndexer) {
Map<Integer,Integer> m = new HashMap<Integer,Integer>();
for (String original : charIndexer.getObjects()) { // find accented letters
Tuple2<String,List<String>> originalLetterAndCombiningDiacritics = normalizeCharSeparateDiacritics(original);
String baseLetter = originalLetterAndCombiningDiacritics._1;
if (LETTERS_WITH_DISREGARDEDABLE_DIACRITICS.contains(baseLetter)) {
for (String diacritic : originalLetterAndCombiningDiacritics._2) {
if (COMBINING_DIACRITICS_THAT_CAN_BE_DISREGARDED.contains(diacritic)) {
m.put(charIndexer.getIndex(original), charIndexer.getIndex(baseLetter));
break;
}
}
}
}
return m;
}
public static String addTilde(String c) {
return normalizeChar(c + TILDE_COMBINING);
}
/**
* Get the character code including any escaped diacritics that precede
* the letter and any unicode "combining characters" that follow it.
*
* Precomposed accents are given the highest priority. Combining characters
* are interpreted as left-associative and high-priority, while escapes are
* right-associative and low-priority. So, for a letter x with precomposed
* diacritic 0, combining chars 1,2,3, and escapes 4,5,6, the input 654x123
* becomes encoded (with escapes) as 6543210x, and decoded (with precomposed
* and combining characters) as x01234656.
*
* @param c A single character, potentially with diacritics encoded in any
* form (composed, precomposed, escaped).
* @return A string representing a single fully-escaped character, with all
* diacritics (combining and precomposed) converted to their equivalent escape
* sequences.
* @throws RuntimeException if the parameter `s` does not represent a single
* (potentially composed or escaped) character.
*/
public static String normalizeChar(String c) {
Tuple2<String, List<String>> letterAndDiacritics = normalizeCharSeparateDiacritics(c);
return letterAndDiacritics._1 + StringHelper.join(letterAndDiacritics._2);
}
/**
* @see edu.berkeley.cs.nlp.ocular.data.textreader.textreader.Charset.normalizeChar
*
* @param c A single character, potentially with diacritics encoded in any
* form (composed, precomposed, escaped).
* @return A fully-normalized character, with all diacritics (combining and
* precomposed) converted to their equivalent normalized forms and placed in
* a list to be returned with the bare letter.
* @throws RuntimeException if the parameter `s` does not represent a single
* (potentially composed or escaped) character.
*/
public static Tuple2<String,List<String>> normalizeCharSeparateDiacritics(String c) {
Tuple3<String, List<String>, Integer> letterAndLength = readLetterAndNormalDiacriticsAt(c, 0);
int length = letterAndLength._3;
if (c.length() != length) throw new RuntimeException("Could not escape ["+c+"] because it contains more than one character ("+StringHelper.toUnicode(c)+")");
return Tuple2(letterAndLength._1, letterAndLength._2);
}
/**
* Read a single character from the line, starting at the given offset.
*
* @see edu.berkeley.cs.nlp.ocular.data.textreader.textreader.Charset.normalizeChar
*
* @param line A line of text possibly containing characters with diacritics
* composed, precomposed, or escaped.
* @param offset The offset point in `line` from which to start reading for a
* character.
* @return A fully-normalized character string, with all diacritics (combining
* and precomposed) converted to their equivalent combining forms. Also
* return the length in the ORIGINAL string of the span used to produce this
* normalized character (to use as an offset when scanning through the string).
*/
private static Tuple2<String, Integer> readNormalizeCharAt(String line, int offset) {
Tuple3<String, List<String>, Integer> result = readLetterAndNormalDiacriticsAt(line, offset);
String c = result._1 + StringHelper.join(result._2);
int length = result._3;
return Tuple2(c, length);
}
/**
* Read a single character from the line including a list of all its diacritics,
* starting at the given offset.
*
* @see edu.berkeley.cs.nlp.ocular.data.textreader.textreader.Charset.normalizeChar
*
* @param line A line of text possibly containing characters with diacritics
* composed, precomposed, or normalized.
* @param offset The offset point in `line` from which to start reading for a
* character.
* @return A fully-normalized character, with all diacritics (combining and
* precomposed) converted to their equivalent combining forms and put in a list,
* the base letter with all diacritics removed, and the length in the ORIGINAL
* string of the span used to produce this normalized character (to use as an
* offset when scanning through the string).
*/
private static Tuple3<String, List<String>, Integer> readLetterAndNormalDiacriticsAt(String line, int offset) {
int lineLen = line.length();
if (offset >= lineLen) throw new RuntimeException("offset must be less than the line length");
if (lineLen - offset >= 2 && line.substring(offset, offset + 2).equals("\\\\"))
return Tuple3("\\\\", (List<String>)new ArrayList<String>(), 2); // "\\" is its own character (for "\"), not an escaped diacritic
List<String> escapeDiacritics = new ArrayList<String>(); // in reversed order!
List<String> combiningDiacritics = new ArrayList<String>();
// get any escape prefixes characters
int i = offset;
while (i < lineLen && line.charAt(i) == '\\') {
if (i + 1 >= lineLen) throw new RuntimeException("expected more after escape symbol, but found nothing: " + i + "," + lineLen + " " + line.substring(Math.max(0, i - 10), i) + "[" + line.substring(i) + "]");
String escape = line.substring(i, i + 2);
escapeDiacritics.add(0, escape);
i += 2; // accept the 2-character escape sequence
}
if (i >= lineLen) throw new RuntimeException("expected a letter after escape code, but found nothing: " + i + "," + lineLen + " " + line.substring(Math.max(0, i - 50), i) + "[" + line.substring(i) + "]");
String letter = String.valueOf(line.charAt(i));
if (isCombiningChar(letter)) throw new RuntimeException("found unexpected combining char: " + i + "," + lineLen + " " + line.substring(Math.max(0, i - 50), i) + "[" + line.substring(i) + "]");
i += 1; // accept the letter itself
// get any combining characters
while (i < lineLen) {
String next = line.substring(i, i + 1);
if (!isCombiningChar(next)) break;
combiningDiacritics.add(next);
i++; // accept the combining character
}
String deprecomposedChar = PRECOMPOSED_TO_COMBINED_MAP.get(letter);
String letterOnly;
if (deprecomposedChar == null) {
letterOnly = letter;
}
else {
letterOnly = String.valueOf(deprecomposedChar.charAt(0));
for (int j = 1; j < deprecomposedChar.length(); ++j)
combiningDiacritics.add(0, String.valueOf(deprecomposedChar.charAt(j)));
}
for (String diacritic : escapeDiacritics) {
if (diacritic.equals("\\i")) {
if (!letterOnly.equals("i")) throw new RuntimeException("the \\i escape sequence can only be used on the character 'i' (to indicate a no-dot i)");
letterOnly = "ı";
}
else {
combiningDiacritics.add(escapeToCombining(diacritic));
}
}
if (letterOnly.length() != 1) throw new RuntimeException("base letter should be length 1, found: " + letterOnly);
if (!combiningDiacritics.isEmpty()) {
char letterChar = letterOnly.charAt(0);
if (!(Character.isAlphabetic(letterChar)))
throw new RuntimeException("because there were diacritics, letter is expected, but something else was found: " + i + "," + lineLen + " " + line.substring(Math.max(0, i - 50), i) + "[" + line.substring(i) + "]");
}
return Tuple3(letterOnly, combiningDiacritics, i - offset);
}
/**
* Convert a string into a sequence of diacritic-normalized characters.
*
* @see edu.berkeley.cs.nlp.ocular.data.textreader.textreader.Charset.normalizeChar
*
* @param line A line of text possibly containing characters with diacritics
* composed, precomposed, or escaped.
* @return A fully-normalized character string, with all diacritics (combining
* and precomposed) converted to their equivalent combining chars.
*/
public static List<String> readNormalizeCharacters(String line) {
List<String> normalizedChars = new ArrayList<String>();
int i = 0;
while (i < line.length()) {
Tuple2<String, Integer> normalizedCharAndLength = readNormalizeCharAt(line, i);
String c = normalizedCharAndLength._1;
int length = normalizedCharAndLength._2;
normalizedChars.add(c);
i += length; // advance to the next character
}
return normalizedChars;
}
/**
* Convert character into unicode precomposed and combining characters
*/
public static String unescapeChar(String c, boolean precomposedOnly) {
if (c.equals("\\\\")) return "\\";
Tuple2<String,List<String>> letterAndNormalDiacritics = normalizeCharSeparateDiacritics(c); // use combining chars only (and make sure it's a valid character)
String baseLetter = letterAndNormalDiacritics._1;
List<String> diacritics = letterAndNormalDiacritics._2;
if (diacritics.isEmpty()) return baseLetter;
StringBuilder b = new StringBuilder();
// Attempt to make a precomposed letter, falling back to composed otherwise
String firstDiacritic = diacritics.get(0);
String precomposed = COMBINED_TO_PRECOMPOSED_MAP.get(baseLetter + firstDiacritic);
if (precomposed != null)
b.append(precomposed);
else {
b.append(baseLetter);
if (!precomposedOnly) b.append(firstDiacritic);
}
if (precomposedOnly) {
// Handle the rest of the diacritics
for (int i = (precomposed != null ? 1 : 0); i < diacritics.size(); ++i) {
String escape = COMBINING_TO_ESCAPE_MAP.get(diacritics.get(i));
if (escape != null)
b.insert(0, escape);
else
b.append(StringHelper.toUnicode(diacritics.get(i)));
}
}
else {
// Handle the rest of the diacritics
for (int i = 1; i < diacritics.size(); ++i) {
b.append(diacritics.get(i));
}
}
return b.toString();
}
/**
* Convert character into unicode precomposed and combining characters
*/
public static String unescapeChar(String c) {
return unescapeChar(c, false);
}
/**
* Convert character into a base character and explicit escape sequences
*/
public static String fullyEscapeChar(String c) {
if (c.equals("\\\\")) return c;
Tuple2<String,List<String>> letterAndNormalDiacritics = normalizeCharSeparateDiacritics(c); // use combining chars only (and make sure it's a valid character)
String baseLetter = letterAndNormalDiacritics._1;
List<String> diacritics = letterAndNormalDiacritics._2;
if (baseLetter.equals("ı"))
baseLetter = "\\ii";
if (diacritics.isEmpty()) return baseLetter;
StringBuilder b = new StringBuilder(baseLetter);
// Handle the rest of the diacritics
for (int i = 0; i < diacritics.size(); ++i) {
String escape = COMBINING_TO_ESCAPE_MAP.get(diacritics.get(i));
if (escape != null)
b.insert(0, escape);
else
b.append(StringHelper.toUnicode(diacritics.get(i)));
}
return b.toString();
}
public static String removeAnyDiacriticFromChar(String c) {
return normalizeCharSeparateDiacritics(c)._1;
}
}