-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNode.kt
More file actions
469 lines (430 loc) · 13.5 KB
/
Node.kt
File metadata and controls
469 lines (430 loc) · 13.5 KB
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
import java.lang.Character.*
import java.lang.Character.UnicodeScript.*
import java.text.Normalizer.*
import java.util.Collections.*
open class Node(var id:Char=MIN_VALUE,map:MutableMap<Char, Node> =mutableMapOf()):MutableMap<Char, Node>by map{
operator fun invoke(f: Node.()->Unit): Node {f();return this}
override fun hashCode() = id.toInt()
override fun toString():String{
val str = hex
return if(str[0].isLetter()||str[0].isDigit()&&str[1].isDigit()&&str[2].isDigit()&&str[3].isDigit())str else '"'+str+'"'
}
val leaf: Leaf?get(){
var node: Node? = this
while(node!is Leaf && node!=null) node = node.values.firstOrNull()
return node as Leaf?
}
open fun recursivelySetIDs(ids:MutableSet<Char> =((1.toChar()..0xFFFF.toChar())-id).toMutableSet()){
for((char,node)in this) if(node.id == MIN_VALUE){
node.id = if(char in ids) char else ids.last()
ids -= node.id
node.recursivelySetIDs(ids)
}
}
open fun toDeadTableString(visited:MutableSet<Node> =mutableSetOf()):String{
var result = "DEADKEY "+hex+"\n"
for((char,node)in this)result+=char.hex+' '+node.hex+if(node!is Leaf && node!is Companion)"@\n" else "\n"
result += "\r\n"
for(node in values)if(node !in visited){
visited += node
result += node.toDeadTableString(visited)
}
return result
}
infix operator fun StringBuilder.rangeTo(char:Char): Node {//maps all permutations to char
fun permute(i:Int){
if(i<=0)this as CharSequence..char
else for(j in 0..i){
val c = this[j]
this[j] = this[0]
this[0] = c
permute(i-1)
}
}
permute(length-1)
return this@Node
}
infix operator fun CharSequence.rangeTo(char:Char): Node {
var node = this@Node
for(i in 0..length-2){
if(node is Leaf)throw IllegalStateException("$this -> $char attempted to overwrite ${substring(0,i)} -> $node")
node = node.getOrPut(this[i]){Node()}
}
val leaf = Leaf(char)
val removed = try{
node.put(this[length-1],leaf)
}catch(e:IllegalStateException){
throw IllegalStateException(substring(0,lastIndex)+e.message,e)
}
if(null!=removed && leaf!=removed)throw IllegalStateException("$this -> $char attempted to overwrite $this -> ${removed.leaf}")
leaves.put(char,this.toString())
return this@Node
}
infix operator fun CharSequence.rangeTo(chars:Pair<Char,Char>): Node {
if(chars.first in leaves.keys && chars.second in leaves.keys)return this@Node
var nodes = setOf(this@Node)
for(i in 0..length-2){
val set:MutableSet<Node> = mutableSetOf()
val n = Node()
for(node in nodes)if(node !is Leaf){
set += node.getOrPut(this[i].uppercaseChar()){n}
set += node.getOrPut(this[i].lowercaseChar()){n}
}
if(set.isEmpty())throw IllegalStateException("$this -> ${chars.first} attempted to overwrite ${substring(0,1)} -> ${nodes.first()}")
nodes = set
}
val leaf1 = Leaf(chars.first)
val leaf2 = if(chars.first == chars.second) leaf1 else Leaf(chars.second)
for(node in nodes)if(node !is Leaf){
nodes = EMPTY_SET as Set<Node>
val removed1 = node.put(this[length-1].uppercaseChar(),leaf1)
val removed2 = node.put(this[length-1].lowercaseChar(),leaf2)
if(null!=removed1 && leaf1!=removed1)throw IllegalStateException("$this -> $leaf1 attempted to overwrite $this -> $removed1")
if(null!=removed2 && leaf2!=removed2)throw IllegalStateException("$this -> $leaf2 attempted to overwrite $this -> $removed2")
}
if(nodes !== EMPTY_SET)throw IllegalStateException("$this -> ${chars.first} attempted to overwrite $this -> ${nodes.firstOrNull()}")
leaves.put(chars.first,toString().uppercase())
leaves.put(chars.second,toString().lowercase())
return this@Node
}
class Leaf(val char:Char): Node(char,EMPTY_MAP as MutableMap<Char, Node>){
override fun toString() = if(char.isLetterOrDigit())""+char else "\""+char+"\""
override fun equals(other:Any?) = if(other is Leaf) char == other.char else false
override fun toDeadTableString(visited:MutableSet<Node>) = ""
override fun put(key:Char,value: Node): Node?=try{
super.put(key,value)
}catch(e:UnsupportedOperationException){
throw IllegalStateException("$key -> $value attempted to overwrite ${leaves[char]} -> $this",e)
}
}
companion object: Node('⎄'){
override fun toString() = "⎄"
override fun toDeadTableString(visited:MutableSet<Node>) = "DEADTABLE\n"+super.toDeadTableString(visited)+"ENDDEADTABLE\n"
val leaves = mutableMapOf<Char,String>()//maps each character to a key sequence for visual display
val KEYBOARD=((' '..'~')+('Α'..'Ω')+('α'..'ω')+('←'..'↙')-'').toSet()
val CHARS=((0xA0.toChar()..MAX_VALUE).filter{
it.script in setOf(COMMON,LATIN,GREEK) && "CJK" !in it.block.toString() && "ANA" !in it.block.toString()
}- KEYBOARD).toSet()
fun Any?.toTitleString() = toString().lowercase().split('_').map{s->if(s.length>2)s.capitalize()else s}.joinToString(" ")
val Any.hex:String get() = String.format("%04X",hashCode())
val Char.name get() = getName(code)?:""
val Char.block get() = UnicodeBlock.of(this)
val Char.script get() = UnicodeScript.of(this.toInt())
val Char.nfd get() = normalize(""+this,Form.NFD)
val Char.nfkd get() = normalize(""+this,Form.NFKD)
val Char.direction get() = try{directionality}catch(e:IllegalArgumentException){null}
operator infix fun Char.plus(c:Char) = this to c
operator fun Char.unaryPlus() = this + this
operator fun CharSequence.unaryPlus() = StringBuilder(this)
fun String.containsWord(word:String,ignoreCase:Boolean=false,vararg delimiters:Char) = word.isBlank()||equals(word,ignoreCase)||delimiters.any{startsWith(word+it,ignoreCase)||endsWith(it+word,ignoreCase)||contains(it+word+it,ignoreCase)}
fun CharSequence.isPrintableASCII():Boolean{
for(c in this) if(c <' ' || c >'~') return false
return true
}
fun CharSequence.hasLetterOrDigit():Boolean{
for(c in this) if(c.isLetterOrDigit()) return true
return false
}
fun CharSequence.isKeyboardLettersAndNotEntirelyDigits():Boolean{
var letter = false
for(c in this){
if(!c.isLetterOrDigit()||c!in KEYBOARD) return false
if(c.isLetter()) letter = true
}
return letter
}
val CharSequence.sub get() = StringBuilder(this).apply{
for(i in 0 until length) this[i] = this[i].sub
}as CharSequence
val Char.sub get() = when(this){
'̈'->'"'//diaeresis
'̄'->'-'//macron
//breve
'̨'->','//ogonek
'́'->'\''//acute accent
'̂'->'^'//circumflex accent
'̇'->'*'//dot above
//caron
'̧'->','//cedilla
'̃'->'~'//tilde
'·'->'.'//middle dot
//'̋'->'\''//double acute
//'̊'->'0'//ring above
//horn
'̀'->'`'//grave
//'̏'->'`'//double grave
//inverted breve
//comma below
//'̥'->'0'//ring below
'̣'->'.'//dot below
'̱'->'_'//macron below
//'̭'->'^'//circumflex accent below
//'̰'->'~'//tilde below
//breve below
'̤'->':'//diaeresis below
//hook above
'̓'->','//comma above
'̔'->','//reversed comma above
'̳'->'='//double low line
'̅'->'-'//overline
'⁄'->'/'//fraction slash
'̸'->'/'//long solidus overlay
else->this
}
fun String.removeAffixes() = this
.removeSuffix(" ET")//tironian sign et
.removeSuffix(" CLOCK")//alarm clock & timer clock
.removeSuffix(" CROSS")
.removeSuffix(" LATIN")
.removeSuffix(" WHITE")//shadowed white latin cross
.removeSuffix(" SYRIAC")
.removeSuffix(" SOURCE")
.removeSuffix(" CONSTANT")
.removeSuffix(" ORNAMENT")
.removeSuffix(" SYMBOL")
.removeSuffix(" MARK")
.removeSuffix(" SIGN")
.removeSuffix(" FOR")
.removeSuffix(" ACCENT")
.removeSuffix(" LINE")//property line, wavy line, centre line symbol
.removeSuffix(" SUIT")//playing cards
.removeSuffix(" CROSS")
.removeSuffix(" COPRODUCT")//amalgamation or coproduct
.removeSuffix(" PRODUCT")//wreath product
.removeSuffix(" PROOF")//end of proof
.removeSuffix(" ANGLE")//right angle
.removeSuffix(" WAVE")//sine wave
.removeSuffix(" OR")//amalgamation or coproduct
.removeSuffix(" OF")//element of
.removeSuffix(" TO")//identical to
.removeSuffix(" NOTES")
.removeSuffix(" NOTE")
.removeSuffix(" MOON")//first quarter moon
.removeSuffix(" QUARTER")//last quarter moon
.removeSuffix(" BALL")//soccer ball
.removeSuffix(" PUMP")//fuel pump
.removeSuffix(" NODE")//ascending descending
.removeSuffix(" PIECE")//shogi piece
.removePrefix("UNIVERSAL ")//universal recycling symbol
.removePrefix("JAPANESE ")//japanese bank symbol
.removePrefix("PLACE ")//place of interest sign
.removePrefix("PARTIAL ")//partial differential
.removePrefix("MONOGRAM ")
.removePrefix("DIGRAM ")
.removePrefix("HOT ")//beverage springs
.removePrefix("ICE ")//ice skate
.removePrefix("CROSS ")
.removePrefix("STAR ")
.removePrefix("STAFF ")
.removePrefix("AND ")
.removePrefix("OF ")
.removePrefix("FUNERAL ")//funeral urn
.removePrefix("HEADSTONE ")//headstone graveyard symbol
.removePrefix("BEAMED ")
.removePrefix("MUSIC ")
.removePrefix("MAP ")//map symbol for lighthouse
.removePrefix("SYMBOL ")
.removePrefix("FOR ")//for all
.removePrefix("THERE ")//there exists
.removePrefix("APL ")
.removePrefix("FUNCTIONAL ")
.removePrefix("LOGICAL ")
.removePrefix("CHESS ")
.removePrefix("DEGREE ")//celsius fahrenheit
.removePrefix("SYMBOL ")
.removePrefix("EARTH ")//earth ground
.removePrefix("HIGH ")//high voltage sign
.removePrefix("PERMANENT ")//permanent paper sign
.removePrefix("SUMMATION ")//summation top & bottom
@JvmStatic fun main(vararg args:String){
for(c in CHARS -'Ȩ'-'ȩ'){//map accented characters
val decomp = c.nfd
if(decomp.length < 2) continue
val sub = decomp.sub
if(!sub.isPrintableASCII()||!sub.hasLetterOrDigit()) continue
if(sub.length < 3){
sub..c
if(c!in "ĀāÁá")sub.reversed()..c
}else{
val r = sub.reversed()
r..c
if(c!in "ǕṺǖṻ")r[1]+(r[0]+r.substring(2))..c
}
}
for(c in "㍷㎜㎝㎞") c.nfkd+'1'..c
for(c in CHARS -('Ⅰ'..'ⅿ')-"ffſt㍷㎜㎝㎞㏂…⩶︙︰".asIterable()){//map ligatures
val s = c.nfkd.sub
if(s.length > 1 && s.isKeyboardLettersAndNotEntirelyDigits()) s..c
}
"OE"..'Œ'
"oe"..'œ'
"AE"..'Æ'
"ae"..'æ'
"-AE"..'Ǣ'
"-ae"..'ǣ'
"'AE"..'Ǽ'
"'ae"..'ǽ'
"am"..'㏂'
"ft"..'ſt'
+"2f"..'ff'
"SP"..'␠'
"BEL"..'␇'
+"$$"..'₪'
+"a$"..'؋'
+"A$"..'₳'
+"B$"..'₿'
+"B|"..'฿'
+"C$"..'₡'
+"C|"..'₵'
+"c$"..'¢'
+"D$"..'₯'
+"d$"..'₫'
+"E$"..'€'
+"e$"..'₠'
+"F$"..'₣'
+"f$"..'ƒ'
+"G$"..'₲'
+"K$"..'₭'
+"L$"..'₺'
+"M$"..'₼'
+"m$"..'₥'
+"N$"..'₦'
+"P$"..'₱'
+"r$"..'₢'
+"S$"..'₷'
+"Z$"..'₴'
+"T$"..'₮'
+"t$"..'₶'
+"W$"..'₩'
+"Y$"..'¥'
+"?$"..'֏'
+"&$"..'₰'
"<3"..'❤'
"3>"..'❥'
"(:"..'☻'
":)"..'☺'
":("..'☹'
"):"..'☹'
"/0"..'∅'
"+-"..'±'
"-+"..'∓'
+".+"..'∔'
"/\\"..'∧'
"\\/"..'∨'
"||"..'∥'
"/||"..'∦'
//"::"..'∷'
//"-."..'∸'
//"-:"..'∹'
":-:"..'∺'
//+":~"..'∻'
"~~"..'≈'
"/~~"..'≉'
+".="..'≐'
//"..="..'≑'
":="..'≔'
"=:"..'≕'
"=o"..'≖'
"o="..'≗'
+"*="..'≛'
+"d="..'≝'
+"m="..'≞'
+"?="..'≟'
+"/="..'≠'
+"-="..'≡'
"/-="..'≢'
//"=="..'≣'
"<-"..'≤'
">-"..'≥'
"<="..'≦'
">="..'≧'
"</="..'≨'
">/="..'≩'
"<<"..'≪'
">>"..'≫'
+")("..'≬'
"/<"..'≮'
"/>"..'≯'
"/-<"..'≰'
"/->"..'≱'
+"<~"..'≲'
+">~"..'≳'
"/~<"..'≴'
"/~>"..'≵'
"<>"..'≶'
"><"..'≷'
"{-".."≼"
"}-"..'≽'
"{~"..'≾'
"}~"..'≿'
"0+"..'⊕'
"0-"..'⊖'
"0x"..'⊗';"0X"..'⊗'
"0/"..'⊘'
"0."..'⊙'
"0o"..'⊚';"0O"..'⊚';"00"..'⊚'
"0*"..'⊛'
"0="..'⊜'
"0_"..'⊝'
"[+"..'⊞'
"[-"..'⊟'
"[x"..'⊠';"[X"..'⊠'
"[."..'⊡'
"<|"..'⊲'
"|>"..'⊳'
"=<|"..'⊴'
"=|>"..'⊵'
"|X"..'⋉';"|x"..'⋉'
"X|"..'⋊';"x|"..'⋊'
"=||"..'⋕'
+"<."..'⋖'
+".>"..'⋗'
"-<"..'⋜'
"->"..'⋝'
"SUM"..+'∑'
"PROD"..+'∏'
"COPROD"..+'∐'
"SQRT"..+'√'
"CBRT"..+'∛'
"FORT"..+'∜'
"1S"..+'∫'
"2S"..+'∬'
"3S"..+'∭'
"4S"..+'⨌'
"1DS"..+'∮'
"2DS"..+'∯'
"3DS"..+'∰'
"CS"..+'∱'
"C1DS"..+'∲'
"A1DS"..+'∳'
"EIGHTH"..'♫'+'♪'
"MAN"..'⛂'+'⛀'
"MEN"..'⛃'+'⛁'
"BULLET"..'•'+'◦'
"BOWTIE"..'⧓'+'⋈'
"LOZENGE"..'⧫'+'◊'
"RHOMBUS"..'◆'+'◇'
"SNOWMAN"..'⛇'+'☃'
"SPARKLE"..'❈'+'❇'
"SPARKLY"..+'✨'
"SAND"..+'⌛'
for(c in CHARS -'⍴'-'∷'-'⏥'-'⅌')with(c.name.removeAffixes()){if(isNotEmpty()&&' ' !in this&&'-' !in this&&c.nfkd.length<=1)this..+c}
for(b in CHARS -'◆'){
val name = b.name
if(!name.startsWith("BLACK "))continue
val bname = name.substring(6).removeAffixes()
if(' ' in bname)continue
for(w in CHARS -'◇'){
val name = w.name
if(!name.startsWith("WHITE "))continue
val wname = name.substring(6).removeAffixes()
if(wname==bname){
wname..b+w
break
}
}
}
recursivelySetIDs()
}
}
}