-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy paththai-consonants.pv
140 lines (107 loc) · 3.81 KB
/
thai-consonants.pv
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
import re
import yaml
import math
from datetime import datetime
with open("thai-consonants.yaml", "r", encoding='utf-8') as f:
data = yaml.load(f.read(), Loader=yaml.SafeLoader)
def alpha_generator(min_alpha):
frequencies = {}
with open("frequencies.txt", "r", encoding='utf-8') as f:
for l in f:
(char, num, _) = l.split()
num_ = int(num.replace(",", ""))
frequencies[char] = num_
m0 = min(frequencies.values())
m1 = max(frequencies.values())
a = (math.log(m1) - math.log(m0)) / (1 - min_alpha)
b = m1 / math.exp(a)
return lambda c: math.log(frequencies[c] / b) / a
alpha_ = alpha_generator(0.0)
page_width = 842
page_height = 612
margin = 30
row_width = 165
letter_size = 64
translit_size = 12
metadata_size = 9
final_size = 7
vertical_spacing = 60
letter_start_y = 90
#font_name = "Charis SIL"
# font_name = "Gentium Plus"
font_name = "Linux Libertine"
size(page_width, page_height)
background(1)
font(font_name, size=11)
stylesheet("initial", weight="bold")
def chunks(l, n):
""" Yield successive n-sized chunks from l """
for i in range(0, len(l), n):
yield l[i:i+n]
def style_line(l):
# swap out [ending] for <ending>ending</ending>
text_line = re.sub(r"\[(.*?)\]", r"<initial>\1</initial>", l)
return "<w>%s</w>" % text_line
def draw_row(letters, x, y, draw_line=False):
push()
translate(x, y)
if draw_line:
pen(0.25)
stroke(0.5)
#line(0, -letter_size, page_width - (margin * 2), -letter_size)
line(-20, -28, -20, page_height - (margin * 4))
stylesheet("initial", fill="#20602c")
stylesheet("italics", italic=True)
stylesheet("class", italic=True, fill="#002c7b")
stylesheet("final", italic=True, fill="#888888")
for letter in letters:
# font("Thonburi", size=letter_size, weight="regular", italic=False)
font("TH Sarabun New", size=letter_size, weight="regular", italic=False)
fill(0)
alpha(alpha_(letter['character']))
text(letter['character'], letter['x_adjust'], 0)
alpha(1.0)
font(font_name, size=translit_size)
text(40, -15, xml=letter['transliteration'])
content = "<class>" + letter['class'] + "</class> " + letter['meaning']
if letter['final']:
content += " <final>" + letter['final'] + "</final>"
font(font_name, size=metadata_size)
text(40, -2, row_width - 65, xml=content)
translate(0, vertical_spacing)
font(sc=False)
pop()
def parse_list(letters):
response = []
for line in letters:
data = line.split('|')
group = {
'character': data[0].strip(),
'transliteration': style_line(data[1].strip()),
'final': '-' + data[2].strip() if data[2].strip() else '',
'class': data[3].strip(),
'meaning': data[4].strip(),
'x_adjust': float(data[5].strip()) if data[5].strip() != '' else 0,
}
response.append(group)
return response
# Heading
font(font_name, size=14, tracking=15)
text("ТАЙСКОЕ ПИСЬМО — СОГЛАСНЫЕ", margin, margin + 10)
align(RIGHT)
font(font_name, size=9, italic=True, tracking=0)
stroke(0)
fill(0.5)
text("https://github.com/vasily-kartashov/thai-consonants • Последняя правка {}".format(datetime.today().strftime('%d/%m/%Y')), page_width-margin, margin + 10)
font(italic=False)
align(LEFT)
pen(0.5)
# Rows
letter_list = chunks(parse_list(data['letters']), 9)
for i, row in enumerate(letter_list):
draw_row(row, (i * row_width) + margin, letter_start_y, i)
# Manual stuff
font("Menlo", size=translit_size)
#for letter in data['manual']:
# text(letter['char'], letter['x'], letter['y'])
export(data['output'])