-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathayerinumbers.py
executable file
·261 lines (206 loc) · 7.77 KB
/
ayerinumbers.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
#! /usr/bin/env python3
# -*- coding: utf-8 -*-
''' ayerinumbers.py -- Converts numbers to Ayeri number words '''
# Copyleft 2015 Carsten Becker <[email protected]>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
import argparse
import math
import sys
'''Morphemes for the powers 12^1 to 12^44'''
pword = {
1: 'lan', # 1 element
2: 'menang', # 2 elements
4: 'samang', # 3- 4 elements
8: 'kaynang', # 5- 6 elements
12:'yonang', # 7- 8 elements
16:'irinang', # 9-10 elements
20:'miyenang', # 11-12 elements
24:'itonang', # 13-14 elements
28:'henang', # 15-16 elements
32:'veyanang', # 17-18 elements
36:'malnang', # 19-20 elements
40:'tamang', # 21-22 elements
}
'''The number words for the numbers 0...B'''
nword = {
'0':'ja',
'1':'men',
'2':'sam',
'3':'kay',
'4':'yo',
'5':'iri',
'6':'miye',
'7':'ito',
'8':'hen',
'9':'veya',
'A':'mal',
'B':'tam',
}
'''Assign keys from nword to pword on the fly. We need this to generate
morphemes for powers beyond 'tamang'.'''
pnword = {}
for i, x in zip(sorted(nword), [pword[i] for i in sorted(pword)]):
pnword[i] = x
pnword['0'] = 'lanang'
def c(n):
'''Convert number > 9 to a string ABCDE…'''
if(n > 9):
n = chr(n + 55);
return n
def baseconv(n, b = 12):
'''Convert an integer number n in base 10 to another'''
if n == 0:
return str(n)
s = ''
while n > 0:
r = n % b # remainder
n = n // b # integer
s = str(c(r)) + str(s)
return s
'''List of numbers 01…0A'''
sd = ['0'+baseconv(i) for i in range(1,12)]
def numword_bigram(n, pn = 'nword'):
'''Take a number bigram (str!) and return the corresponding number word.'''
# Instantiate empty string as a container
s = ''
# Single digit
if len(n) < 2:
# Digit is 1...B and we're using regular number words
if n[0] != '0' and pn == 'nword':
s = '{}'.format(nword[n[0]])
# Digit is 1...B and we're using power words
elif n[0] != '0' and pn == 'pnword':
s = '{}'.format(pnword[n[0]])
# Two digits
else:
# Digits 10, 20, 30, ..., B0 and we're using regular number words
if n[0] != '0' and n[1] == '0' and pn == 'nword':
s = '{}{} '.format(nword[n[0]], pword[1])
# Digits 10, 20, 30, ..., B0 and we're using power words
elif n[0] != '0' and n[1] == '0' and pn == 'pnword':
s = '{}{} '.format(nword[n[0]], pnword['0'])
# Digits 11, 12, 13, ..., 21, 22, 23, ..., B1, B2, B3, ...
elif n[0] != '0' and n[1] != '0':
s = '{}{}-'.format(nword[n[0]], pword[1])
# Digits 01, 02, ..., 0B
elif n[0] == '0' and n[1] != '0':
s = 'nay '
# Last digit is not 0 and we're using regular number words
if n[1] != '0' and pn == 'nword':
s += '{}'.format(nword[n[1]])
# Last digit is not 0 and we're using power words
elif n[1] != '0' and pn == 'pnword':
s += '{}'.format(pnword[n[1]])
return s.strip()
def rsplit_str(s, i):
'''Splits string after i digits from the back'''
# Containers
chars = ''
spl = []
# Calculate the modulo the last index results in for the length of the group
# 12345, split into groups of max. 3 => last index: 4, 4 % 3 = 1
m = (len(s) - 1) % i
for n, char in enumerate(s):
chars += char;
if n % i == m:
spl.append(chars);
chars = ''
return spl
def split_num(s):
'''Split into list with the format [['00','00'],['00','00'], ...]'''
# Splitting into groups of 4
sp = rsplit_str(s, 4)
# Splitting into subgroups of 2
for i, n in enumerate(sp):
sp[i] = rsplit_str(n, 2)
return sp
def count_elements(l):
'''Count the number of elements in the list on the 1st sublevel'''
return sum(len(x) for x in l)
def get_power(i):
'''Gets the word for the respective power from number of elements'''
pow = 2 * i - 2
# If power word exceeds the hard-coded ones, generate one on the fly
if pow > max(pword):
x = numberword(baseconv((math.ceil(i/2)*2 - 1) // 2 + 1), 'pnword')
return x
# In case it's already readily defined
elif pow in pword:
return pword[pow]
# In case it's not already defined
elif pow not in pword and pow <= max(pword):
# Iterate through all the power words until one bigger than the input
# is found, use the previous one then.
p = 0
for x in sorted(list(pword)):
if x > pow:
break
else:
p = x
return pword[p]
else:
return False
def numberword(n, pn = 'nword'):
'''The function to form the number word.'''
# Make n a string if it's not yet provided as such
# (Thing is, we're dealing with base 12, so normal number manipulation
# doesn't work because that's all in base 10)
n = str(n)
# In case it's zero
if n == '0':
return nword[n]
# Otherwise, instantiate new list to collect converted strings in:
s = []
# Split the number up into myriads and hundreds
n = split_num(n)
for i, myrgrp in enumerate(n):
# The power word for the current group, avoid *menang menang
if i < len(n) - 1 and n[i] != ['00', '00']:
s.append(get_power((len(n) - i) * 2))
# Some fixing: [['01', ...]] -> [['1', ...]]
if len(n[i]) > 1 and n[i][0] in sd:
n[i][0] = n[i][0][1]
# Some fixing: [['00', '01'], [...]] -> [['00', '1'], ...]
if i < len(n) - 1 and len(n[i]) > 1 and n[i][0] == '00' and n[i][1] in sd:
n[i][1] = n[i][1][1]
# Get the word for power of the element in the group
if len(n[i]) > 1 and n[i][0] != '00':
s.append(get_power(len(n[i])))
# For each group of hundreds, get the number word
for j, hungrp in enumerate(n[i]):
if j == len(n[i]) - 1 and pn == 'pnword':
s.append(numword_bigram(n[i][j], 'pnword'))
else:
s.append(numword_bigram(n[i][j]))
return ' '.join(filter(None, s))
def main(argv=None):
"""Main function providing command line option parser."""
if argv is None:
argv = sys.argv[1:]
# Parser for command line options
parser = argparse.ArgumentParser(description=
"Converts numbers to Ayeri number words.")
parser.add_argument('n', type=int, help='''an integer number 0 <= n < 12^44''')
parser.add_argument('-s', '--show-conversion', action='store_const',
const=True, default=False, help='''show the conversion into base 12'''),
args = parser.parse_args(argv)
# Return string
s = ''
if args.show_conversion:
s += '{}₁₂: '.format(",".join(rsplit_str(baseconv(args.n),4)))
s += '{}'.format(numberword(baseconv(args.n)))
return s
if __name__ == '__main__':
sys.exit(main())