-
Notifications
You must be signed in to change notification settings - Fork 0
/
palieasyread.py
284 lines (229 loc) · 6.8 KB
/
palieasyread.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
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
# -*- coding: utf-8 -*-
# Split Roman pali words into syllables
# It splits correctly for most of the words, but not all.
# Update: https://github.com/vpnry/palieasyread
# this version: 0.0.3
import sys
import os
import string
import re
from collections import OrderedDict
# -------- modify these 3 values to your choice
my_word_divider = ' _ '
my_syllable_divider = ' '
my_show_origin = True
# below is the app logic
args = sys.argv
vowel_str = 'a,ā,i,ī,u,ū,e,o'
vowels = vowel_str.split(',')
vowels += vowel_str.upper().split(',')
# asp_consonants = 'ch,jh,kh,gh,th,ṭh,dh,ḍh,bh,ph'.split(',')
escape_xh = OrderedDict([
# Myanmar number 1->0
('kh', '၁'),
('gh', '၂'),
('ch', '၃'),
('jh', '၄'),
('th', '၅'),
('ṭh', '၆'),
('dh', '၇'),
('ḍh', '၈'),
('ph', '၉'),
('bh', '၀'),
('vh', '$'),
# pariyogāḷhadhammo => pa ri yo gā ḷha dham mo
('ḷh', '¢'),
# gārayhā => gā ra yhā
('yh', '£'),
('br', '€'),
('by', '¥')])
final_manual_fix = OrderedDict([
('K@h', 'Kh'),
('G@h', 'Gh'),
('C@h', 'Ch'),
('J@h', 'Jh'),
('T@h', 'Th'),
('Ṭ@h', 'Ṭh'),
('D@h', 'Dh'),
('Ḍ@h', 'Ḍh'),
('P@h', 'Ph'),
('B@h', 'Bh'),
('V@h', 'Vh'),
('Ḷ@h', 'Ḷh'),
('Y@h', 'Yh'),
('B@r', 'Br'),
('B@y', 'By'),
# Manually replace
('D@v', 'Dv'),
# khadv
('d@v', '@dv'),
('t@v', '@tv'),
('s@v', '@sv'),
('t@r', '@tr')
])
not_allow_divs = [v for k, v in escape_xh.items()]
not_allow_divs.append('@')
rex_nonWord = re.compile(r'\W+')
def add_div_consonant(word):
word_ = word.strip('@1234567890' + string.punctuation + string.whitespace)
if not word_:
return word
# like kkh =>k-kh etc
three = re.compile(
r'([^aāiīuūeo])(ch|jh|kh|gh|th|ṭh|dh|ḍh|bh|ph)',
re.IGNORECASE)
three_con = re.findall(three, word)
if three_con:
for tup in three_con:
w = tup[0] + tup[1]
rw = tup[0] + '@' + tup[1]
word = word.replace(w, rw)
for k, v in escape_xh.items():
word = word.replace(k, str(v))
# like nn =>n-n etc
two = re.compile(
r'([^.aāiīuūeo1234567890@])([^.aāiīuūeo1234567890@])',
re.IGNORECASE)
two_con = re.findall(two, word)
if two_con:
for tup in two_con:
w = tup[0] + tup[1]
rw = tup[0] + '@' + tup[1]
word = word.replace(w, rw)
# restore escaped ?h
for k, v in escape_xh.items():
word = word.replace(str(v), k)
return word
def manual_fix_chunk(word):
rex = re.compile(r'@([^aāiīuūeo])@', re.IGNORECASE)
# @t@ => t@
word = re.sub(rex, r'\1@', word)
# fix misc PTT html
word = word.replace('@,', ',')
word = word.replace('@.', '.')
word = word.replace('@;', ';')
word = word.replace('@ṃ', 'ṃ')
word = word.replace('@ṁ', 'ṁ')
word = word.replace('‘@‘', '‘‘')
word = word.replace('’@’', '’’')
word = word.replace('‘@', '‘')
for k, v in final_manual_fix.items():
word = word.replace(k, str(v))
return word.strip('@')
def split_syl_word(word):
if len(word) <= 2:
return word
word = add_div_consonant(word)
chunk = ''
chars = [char for char in word]
lenChar = len(chars)
for i in range(lenChar):
if re.match(rex_nonWord, chars[i]):
chunk += chars[i]
continue
if chars[i] == '@':
chunk += chars[i]
continue
if chars[i] not in vowels:
chunk += chars[i]
# consider a valid syllable after meeting a vowel
# it works for most of the words.
else:
chunk += chars[i] + '@'
chunk = chunk.strip('@')
return manual_fix_chunk(chunk)
def check_div_collision(word_div, syl_div):
divs = word_div.strip() + syl_div.strip()
for i in not_allow_divs:
if i in divs:
return True
return False
def easy_read(text, word_div=' _ ', show_origin=True, syl_div=' '):
error_div = check_div_collision(word_div, syl_div)
if error_div:
print(
'Error: word_div or syl_div must not contain these chars\n',
not_allow_divs)
print('Please use other dividers.')
return ''
res = ''
lines = text.strip().splitlines()
for line in lines:
line_chunk = ''
if not line:
res += '\n'
continue
words = line.strip().split(' ')
for word in words:
syls = split_syl_word(word)
if syls.strip():
line_chunk += syls + word_div
line_chunk = line_chunk.strip(' ' + word_div)
if word_div == '] [':
line_chunk = f'[{line_chunk}]'
if show_origin:
res += f'{line}\n{line_chunk}\n'
else:
res += f'\n{line_chunk}\n'
if syl_div != '@':
res = res.replace('@', syl_div)
# fix misc double word_div
di = word_div.strip()
double_word_div = f' {di} {di} '
one_word_div = f' {di} '
res = res.replace(double_word_div, one_word_div)
return res.strip()
# ------- Command line -------
def easy_read_text(*wordss):
text = ""
for word in wordss:
text += word + " "
text = text.strip()
res = easy_read(text,
word_div=my_word_divider,
show_origin=my_show_origin,
syl_div=my_syllable_divider)
return res
def easy_read_file(fn):
text = ''
with open(fn, 'r', encoding='utf-8') as f:
text = f.read()
res = easy_read(text,
word_div=my_word_divider,
show_origin=my_show_origin,
syl_div=my_syllable_divider)
savefn = fn + '_done.txt'
with open(savefn, 'w', encoding='utf-8') as fo:
fo.write(res)
print('Done! Check:', savefn)
def printHelp():
name = args[0]
print('How to use:')
print(
"1. To split a short pali text, run:\n python3 " +
name +
" <text>")
print(
"2. To split a pali plain text file, run:\n python3 " +
name +
" yourPaliFile.txt")
print('')
if __name__ == '__main__':
if len(args) > 0:
try:
if not os.path.isfile(args[1]):
try:
tdone = easy_read_text(*args[1:])
print(tdone)
except Exception as e:
print("Errors occured! " + str(e))
printHelp()
else:
try:
print('Processing file:', args[1])
easy_read_file(args[1])
except Exception as e:
print("Errors occured! " + str(e))
printHelp()
except IndexError:
printHelp()