Created
August 27, 2019 05:22
-
-
Save Taywee/a49b3135b7ff7fbbe2ad3e943022da73 to your computer and use it in GitHub Desktop.
Convert hexadecimal numbers to the S. R. Rogers pronunciation system (MIT License)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
# -*- coding: utf-8 -*- | |
# Copyright © 2019 Taylor C. Richberger | |
# This code is released under the MIT license | |
import argparse | |
base = { | |
'0': 'zero', | |
'1': 'one', | |
'2': 'two', | |
'3': 'three', | |
'4': 'four', | |
'5': 'five', | |
'6': 'six', | |
'7': 'seven', | |
'8': 'eight', | |
'9': 'nine', | |
'A': 'ten', | |
'B': 'eleven', | |
'C': 'twelve', | |
'D': 'draze', | |
'E': 'eptwin', | |
'F': 'fim', | |
} | |
teeks = { | |
'10': 'tex', | |
'11': 'oneteek', | |
'12': 'twenteek', | |
'13': 'thirteek', | |
'14': 'fourteek', | |
'15': 'fifteek', | |
'16': 'sixteek', | |
'17': 'sevteek', | |
'18': 'eighteek', | |
'19': 'nineteek', | |
'1A': 'tenteek', | |
'1B': 'levteek', | |
'1C': 'twelfteek', | |
'1D': 'drazeteek', | |
'1E': 'epteek', | |
'1F': 'fimteek' | |
} | |
teks = { | |
'2': 'twentek', | |
'3': 'thirtek', | |
'4': 'fourtek', | |
'5': 'fiftek', | |
'6': 'sixtek', | |
'7': 'sevtek', | |
'8': 'eightek', | |
'9': 'ninetek', | |
'A': 'tentek', | |
'B': 'levtek', | |
'C': 'twelftek', | |
'D': 'drazetek', | |
'E': 'eptek', | |
'F': 'fimtek', | |
} | |
powers = [ | |
'millek', | |
'billek', | |
'trillek', | |
'quadrillek', | |
'quintillek', | |
'sextillek', | |
'septillek', | |
'octillek', | |
'nonillek', | |
'decillek', | |
] | |
def rchunks(input, size): | |
'''Yield successive reversed chunks of the input in reverse order''' | |
for i in range(len(input) - size, -1, -size): | |
yield input[i:i + 4] | |
def round_to_multiple(value, base): | |
'''Round the value up to the nearest multiple of base''' | |
rem = value % base | |
if rem == 0: | |
return value | |
return value - rem + base | |
def normalize_hex(value: int): | |
'''Normalize a hex number, putting it into a common form for use, and padded to the left by zeroes in groups of four''' | |
outlength = round_to_multiple(len(f'{value:X}'), 4) | |
return f'{value:0{outlength}X}' | |
def hex_quad_to_words(quad): | |
output = [] | |
if quad[0] != '0': | |
output.append(base[quad[0]]) | |
output.append('thousek') | |
if quad[1] != '0': | |
output.append(base[quad[1]]) | |
output.append('hundrek') | |
if quad[2] == '0': | |
if quad[3] != '0': | |
output.append(base[quad[3]]) | |
elif quad[2] == '1': | |
output.append(teeks[quad[2:4]]) | |
else: | |
tek = teks[quad[2]] | |
if quad[3] == '0': | |
output.append(tek) | |
else: | |
one = base[quad[3]] | |
output.append(f'{tek}-{one}') | |
return output | |
def convert(value): | |
'''Convert an input integer into the Rogers system pronunciation representation''' | |
if value == 0: | |
return 'zero' | |
elif value < 0: | |
raise 'Can not handle negative numbers' | |
normalized = normalize_hex(value) | |
output = [] | |
for i, chunk in enumerate(rchunks(normalized, 4)): | |
words = hex_quad_to_words(chunk) | |
if words: | |
if i > 0: | |
words.append(powers[i - 1]) | |
output.append(' '.join(words)) | |
return ', '.join(reversed(output)) | |
def main(): | |
parser = argparse.ArgumentParser(description='Convert an input hexidecimal number into a pronounced form using the Rogers system') | |
parser.add_argument('-V', '--version', action='version', version='0.1') | |
parser.add_argument('number', help='The input hex number, with or without a leading 0x', type=lambda value: int(value, base=16)) | |
args = parser.parse_args() | |
print(convert(args.number)) | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment