## ## Name: kore.py ## Purpose: Encodes and decodes "Koremutake" syllables ## Author: M. J. Fromberger, Copyright 2004. ## Info: $Id: kore.py 523 2007-11-14 19:38:30Z sting $ ## ## Notes: http://shorl.com/koremutake.php ## ## Permission is hereby granted, free of charge, to any person ## obtaining a copy of this software and associated documentation ## files (the "Software"), to deal in the Software without ## restriction, including without limitation the rights to use, copy, ## modify, merge, publish, distribute, sublicense, and/or sell copies ## of the Software, and to permit persons to whom the Software is ## furnished to do so, subject to the following conditions: ## ## The above copyright notice and this permission notice shall be ## included in all copies or substantial portions of the Software. ## ## THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, ## EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF ## MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND ## NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT ## HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, ## WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, ## OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER ## DEALINGS IN THE SOFTWARE. ## import re # This table taken from http://shorl.com/koremutake.php syllables = [ 'BA', 'BE', 'BI', 'BO', 'BU', 'BY', 'DA', 'DE', 'DI', 'DO', 'DU', 'DY', 'FA', 'FE', 'FI', 'FO', 'FU', 'FY', 'GA', 'GE', 'GI', 'GO', 'GU', 'GY', 'HA', 'HE', 'HI', 'HO', 'HU', 'HY', 'JA', 'JE', 'JI', 'JO', 'JU', 'JY', 'KA', 'KE', 'KI', 'KO', 'KU', 'KY', 'LA', 'LE', 'LI', 'LO', 'LU', 'LY', 'MA', 'ME', 'MI', 'MO', 'MU', 'MY', 'NA', 'NE', 'NI', 'NO', 'NU', 'NY', 'PA', 'PE', 'PI', 'PO', 'PU', 'PY', 'RA', 'RE', 'RI', 'RO', 'RU', 'RY', 'SA', 'SE', 'SI', 'SO', 'SU', 'SY', 'TA', 'TE', 'TI', 'TO', 'TU', 'TY', 'VA', 'VE', 'VI', 'VO', 'VU', 'VY', 'BRA', 'BRE', 'BRI', 'BRO', 'BRU', 'BRY', 'DRA', 'DRE', 'DRI', 'DRO', 'DRU', 'DRY', 'FRA', 'FRE', 'FRI', 'FRO', 'FRU', 'FRY', 'GRA', 'GRE', 'GRI', 'GRO', 'GRU', 'GRY', 'PRA', 'PRE', 'PRI', 'PRO', 'PRU', 'PRY', 'STA', 'STE', 'STI', 'STO', 'STU', 'STY', 'TRA', 'TRE' ] # Construct a regular expression matching any syllable # This could be more efficiently done by hand decoder = re.compile('(%s)' % str.join('|', syllables), re.IGNORECASE) def encode(val): """Encode a positive integer numeric value in Koremutake syllables.""" if not isinstance(val, (int, long)) or val < 0: raise TypeError("Input value must be a non-negative integer") if val == 0: return syllables[0].lower() out = [] while val <> 0: out.append(syllables[val % 128]) val //= 128 out.reverse() return str.join('', out).lower() def split(kor): """Split a sequence of Koremutake syllables into a list of strings. """ kor = kor.strip().replace(' ', '') out = [] ; sw = True for elt in decoder.split(kor): if sw: if elt: raise ValueError("substring '%s' does not " "begin a valid syllable" % elt) else: out.append(elt) sw = not sw return out def decode(kor): """Decode a sequence of Koremutake syllables to an integer.""" output = 0 for elt in split(kor): output *= 128 output += syllables.index(elt.upper()) return output # Here there be dragons