forked from mideind/Netskrafl
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlanguages.py
More file actions
executable file
·342 lines (286 loc) · 9.13 KB
/
languages.py
File metadata and controls
executable file
·342 lines (286 loc) · 9.13 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
# -*- coding: utf-8 -*-
""" Language, locale and alphabet encapsulation module
Author: Vilhjalmur Thorsteinsson, 2014
The classes in this module encapsulate particulars of supported
languages, including the character set, scores, tiles in the
initial bag, sorting, etc.
Currently the only supported language is Icelandic.
"""
from functools import reduce
class Alphabet:
""" This implementation of the Alphabet class encapsulates particulars of the Icelandic
language. Other languages can be supported by modifying or subclassing this class.
"""
# Sort ordering of allowed Icelandic letters
def __init__(self):
pass
order = u'aábdðeéfghiíjklmnoóprstuúvxyýþæö'
# Upper case version of the order string
upper = u'AÁBDÐEÉFGHIÍJKLMNOÓPRSTUÚVXYÝÞÆÖ'
# All tiles including wildcard '?'
all_tiles = order + u'?'
# Sort ordering of all valid letters
full_order = u'aábcdðeéfghiíjklmnoópqrstuúvwxyýzþæö'
# Upper case version of the full order string
full_upper = u'AÁBCDÐEÉFGHIÍJKLMNOÓPQRSTUÚVWXYÝZÞÆÖ'
# Map letters to bits
letter_bit = { letter : 1 << ix for ix, letter in enumerate(order) }
# Locale collation (sorting) map, initialized in _init()
_lcmap = None # Case sensitive
_lcmap_nocase = None # Case insensitive
@staticmethod
def bit_pattern(word):
""" Return a pattern of bits indicating which letters are present in the word """
return reduce(lambda x, y: x | y, [Alphabet.letter_bit[c] for c in word], 0)
@staticmethod
def bit_of(c):
""" Returns the bit corresponding to a character in the alphabet """
return Alphabet.letter_bit[c]
@staticmethod
def all_bits_set():
""" Return a bit pattern where the bits for all letters in the Alphabet are set """
return 2 ** len(Alphabet.order) - 1
@staticmethod
def lowercase(ch):
""" Convert an uppercase character to lowercase """
return Alphabet.full_order[Alphabet.full_upper.index(ch)]
@staticmethod
def tolower(s):
""" Return the argument string converted to lowercase """
return u''.join([Alphabet.lowercase(c) if c in Alphabet.full_upper else c for c in s])
@staticmethod
def sort(l):
""" Sort a list in-place by lexicographic ordering according to this Alphabet """
l.sort(key = Alphabet.sortkey)
@staticmethod
def sorted(l):
""" Return a list sorted by lexicographic ordering according to this Alphabet """
return sorted(l, key = Alphabet.sortkey)
@staticmethod
def string_subtract(a, b):
""" Subtract all letters in b from a, counting each instance separately """
# Note that this cannot be done with sets, as they fold multiple letter instances into one
lcount = [a.count(c) - b.count(c) for c in Alphabet.all_tiles]
return u''.join([Alphabet.all_tiles[ix] * lcount[ix]
for ix in range(len(lcount)) if lcount[ix] > 0])
# noinspection PyUnusedLocal
@staticmethod
def format_timestamp(ts, format = None):
""" Return a timestamp formatted as a readable string """
# Currently always returns the full ISO format: YYYY-MM-DD HH:MM:SS
return u"" + ts.isoformat(' ')[0:19]
# noinspection PyUnusedLocal
@staticmethod
def format_timestamp_short(ts, format = None):
""" Return a timestamp formatted as a readable string """
# Returns a short ISO format: YYYY-MM-DD HH:MM
return u"" + ts.isoformat(' ')[0:16]
@staticmethod
def _init():
""" Create a collation (sort) mapping for the Icelandic language """
lcmap = [i for i in range(0, 256)]
def rotate(letter, sort_after):
""" Modifies the lcmap so that the letter is sorted after the indicated letter """
sort_as = lcmap[sort_after] + 1
letter_val = lcmap[letter]
# We only support the case where a letter is moved forward in the sort order
if letter_val > sort_as:
for i in range(0, 256):
if (lcmap[i] >= sort_as) and (lcmap[i] < letter_val):
lcmap[i] += 1
lcmap[letter] = sort_as
def adjust(s):
""" Ensure that the sort order in the lcmap is in ascending order as in s """
# This does not need to be terribly efficient as the code is
# only run once, during initialization
for i in range(1, len(s) - 1):
rotate(ord(s[i]), ord(s[i-1]))
adjust(Alphabet.full_upper) # Uppercase adjustment
adjust(Alphabet.full_order) # Lowercase adjustment
# Now we have a case-sensitive sorting map: copy it
Alphabet._lcmap = lcmap[:]
# Create a case-insensitive sorting map, where the lower case
# characters have the same sort value as the upper case ones
for i, c in enumerate(Alphabet.full_order):
lcmap[ord(c)] = lcmap[ord(Alphabet.full_upper[i])]
# Store the case-insensitive sorting map
Alphabet._lcmap_nocase = lcmap
@staticmethod
def sortkey(lstr):
""" Key function for locale-based sorting """
assert Alphabet._lcmap
return [Alphabet._lcmap[ord(c)] if ord(c) <= 255 else 256 for c in lstr]
@staticmethod
def sortkey_nocase(lstr):
""" Key function for locale-based sorting, case-insensitive """
assert Alphabet._lcmap_nocase
return [Alphabet._lcmap_nocase[ord(c)] if ord(c) <= 255 else 256 for c in lstr]
# Initialize the locale collation (sorting) map
Alphabet._init()
# noinspection PyUnresolvedReferences
class TileSet(object):
""" Abstract base class for tile sets. Concrete classes are found below. """
@classmethod
def score(cls, tiles):
""" Return the net (plain) score of the given tiles """
if not tiles:
return 0
return sum([cls.scores[tile] for tile in tiles])
@classmethod
def full_bag(cls):
""" Return a full bag of tiles """
if not hasattr(cls, "_full_bag"):
# Cache the bag
cls._full_bag = u''.join([tile * count for (tile, count) in cls.bag_tiles])
return cls._full_bag
@classmethod
def num_tiles(cls):
return sum(n for letter, n in cls.bag_tiles)
class OldTileSet(TileSet):
# Letter scores in the old (original) Icelandic tile set
scores = {
u'a': 1,
u'á': 4,
u'b': 6,
u'd': 4,
u'ð': 2,
u'e': 1,
u'é': 6,
u'f': 3,
u'g': 2,
u'h': 3,
u'i': 1,
u'í': 4,
u'j': 5,
u'k': 2,
u'l': 2,
u'm': 2,
u'n': 1,
u'o': 3,
u'ó': 6,
u'p': 8,
u'r': 1,
u's': 1,
u't': 1,
u'u': 1,
u'ú': 8,
u'v': 3,
u'x': 10,
u'y': 7,
u'ý': 9,
u'þ': 4,
u'æ': 5,
u'ö': 7,
u'?': 0
}
# Tiles in initial bag, with frequencies
bag_tiles = [
(u"a", 10),
(u"á", 2),
(u"b", 1),
(u"d", 2),
(u"ð", 5),
(u"e", 6),
(u"é", 1),
(u"f", 3),
(u"g", 4),
(u"h", 2),
(u"i", 8),
(u"í", 2),
(u"j", 1),
(u"k", 3),
(u"l", 3),
(u"m", 3),
(u"n", 8),
(u"o", 3),
(u"ó", 1),
(u"p", 1),
(u"r", 7),
(u"s", 6),
(u"t", 5),
(u"u", 6),
(u"ú", 1),
(u"v", 2),
(u"x", 1),
(u"y", 1),
(u"ý", 1),
(u"þ", 1),
(u"æ", 1),
(u"ö", 1),
(u"?", 2)] # Blank tiles
# Number of tiles in bag
OldTileSet.BAG_SIZE = OldTileSet.num_tiles()
class NewTileSet(TileSet):
# Scores in new Icelandic tile set
scores = {
u'a': 1,
u'á': 3,
u'b': 5,
u'd': 5,
u'ð': 2,
u'e': 3,
u'é': 7,
u'f': 3,
u'g': 3,
u'h': 4,
u'i': 1,
u'í': 4,
u'j': 6,
u'k': 2,
u'l': 2,
u'm': 2,
u'n': 1,
u'o': 5,
u'ó': 3,
u'p': 5,
u'r': 1,
u's': 1,
u't': 2,
u'u': 2,
u'ú': 4,
u'v': 5,
u'x': 10,
u'y': 6,
u'ý': 5,
u'þ': 7,
u'æ': 4,
u'ö': 6,
u'?': 0
}
# New Icelandic tile set
bag_tiles = [
(u"a", 11),
(u"á", 2),
(u"b", 1),
(u"d", 1),
(u"ð", 4),
(u"e", 3),
(u"é", 1),
(u"f", 3),
(u"g", 3),
(u"h", 1),
(u"i", 7),
(u"í", 1),
(u"j", 1),
(u"k", 4),
(u"l", 5),
(u"m", 3),
(u"n", 7),
(u"o", 1),
(u"ó", 2),
(u"p", 1),
(u"r", 8),
(u"s", 7),
(u"t", 6),
(u"u", 6),
(u"ú", 1),
(u"v", 1),
(u"x", 1),
(u"y", 1),
(u"ý", 1),
(u"þ", 1),
(u"æ", 2),
(u"ö", 1),
(u"?", 2)] # Blank tiles
# Number of tiles in bag
NewTileSet.BAG_SIZE = NewTileSet.num_tiles()