-
Notifications
You must be signed in to change notification settings - Fork 0
/
xml2ibus_table.py
executable file
·134 lines (100 loc) · 2.64 KB
/
xml2ibus_table.py
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
#!/usr/bin/env python
# coding: utf-8
# In[1]:
from xml.dom import minidom
x = minidom.parse('ogsl-sl.xml')
# In[2]:
reps = [
['š','sz'],
['Š','SZ'],
['ṣ','s,'],
['Ṣ','S,'],
['ṭ','t,'],
['Ṭ','T,'],
['ś',"s'"],
['Ś',"S'"],
['ʾ',"'"],
['₀','0'],
['₁','1'],
['₂','2'],
['₃','3'],
['₄','4'],
['₅','5'],
['₆','6'],
['₇','7'],
['₈','8'],
['₉','9'],
['ₓ','x'],
['ḫ','h,'],
['Ḫ','H,'],
['ŋ','j'],
['Ŋ','J'],
['û','u'],
['ā','aa'],
['ē','ee'],
['ī','ii'],
['ū','uu']
]
def normalize(s):
for fro, to in reps:
s=s.replace(fro, to)
return s
# In[46]:
# In[70]:
def maybeadd(reading,unic,name=''):
reading=normalize(reading)
name=normalize(name)
if reading not in mapping:
mapping[reading]={(unic,name)}
else:
mapping[reading].add((unic,name))
# In[71]:
mapping = {}
for sign in x.getElementsByTagName('sign'):
name = sign.attributes['n'].value.strip('|')
unicodestr=''
for child in sign.childNodes:
if child.tagName == 'utf8':
unicodestr = (child.childNodes[0].nodeValue)
if child.tagName == 'v':
reading = child.attributes['n'].value
maybeadd(reading, unicodestr, name)
if unicodestr!= '':
maybeadd(name, unicodestr)
for form in sign.getElementsByTagName('form'):
name = form.attributes['n'].value.strip('|')
#print(name)
maybec = form.getElementsByTagName('g:c')
unicodestr=''
try:
if len(maybec)>0 :
unicodestr = maybec[0].attributes['g:utf8'].value
else:
unicodestr = form.getElementsByTagName('g:s')[0].attributes['g:utf8'].value
except :
pass
for reading in form.getElementsByTagName('v'):
if unicodestr != '':
maybeadd(reading.attributes['n'].value, unicodestr, name)
if unicodestr!= '':
maybeadd(name, unicodestr)
mapping['darengal']
# In[72]:
valid_chars = ''.join(sorted(list(set(''.join(k for k in mapping.keys())))))
#''.join(k for k in mapping.keys())
valid_chars
# In[73]:
inf = ""
with open("ibus-tables-cuneiform_pre.txt",'r') as infile:
inf = infile.read()
inf = inf.replace('{VALID_INPUTS}', valid_chars)
with open("ibus-tables-cuneiform.txt",'w') as outfile:
outfile.write(inf)
for reading, vals in mapping.items():
if len(vals)>1:
for unic,name in vals:
outfile.write(f"{reading}({name})\t{unic}\t0\t### {name}\n")
else:
unic,name = vals.pop()
outfile.write(f"{reading}\t{unic}\t0\t### {name}\n")
outfile.write("END_TABLE")