kopia lustrzana https://gitlab.com/4ham/koch-method-real-words
62 wiersze
2.0 KiB
Python
Executable File
62 wiersze
2.0 KiB
Python
Executable File
#!/usr/bin/env python3
|
|
|
|
class Letter2Bitmask:
|
|
"A class that handles translation of letters to bitmask bits and reverse."
|
|
|
|
def _dicts_from_letters(letters):
|
|
"""Return c2b and b2c dictonaries from an array of letters.
|
|
|
|
The argument is an array containing all 26 letters from the ASCII alphabet
|
|
(each letter exactly once).
|
|
|
|
The function returns a pair of maps:
|
|
- The first map, c2b, has the characters as keys
|
|
and the corresponding bitmap as values.
|
|
- The second map, b2c, is the reverse."""
|
|
c2b = {}
|
|
b2c = {}
|
|
bitmask = 1
|
|
for c in letters:
|
|
c2b[c] = bitmask
|
|
b2c[bitmask] = c
|
|
bitmask <<= 1
|
|
return [c2b, b2c]
|
|
|
|
|
|
def __init__(self, letters):
|
|
self._c2b, self._b2c = Letter2Bitmask._dicts_from_letters(letters)
|
|
|
|
def bit(self, c):
|
|
return self._c2b[c]
|
|
|
|
def char(self, bit):
|
|
return self._b2c[bit]
|
|
|
|
def number(self, chars):
|
|
result = 0
|
|
for c in chars:
|
|
result |= self._c2b[c]
|
|
return result
|
|
|
|
def chars(self, number):
|
|
result = set()
|
|
bitmask = 1 << number.bit_length()
|
|
while bitmask:
|
|
if number & bitmask:
|
|
result.add(self._b2c[bitmask])
|
|
bitmask >>= 1
|
|
return sorted(result)
|
|
|
|
if __name__ == "__main__":
|
|
import argparse
|
|
from letters_rare_first import from_lettercount_file
|
|
parser = argparse.ArgumentParser(description="Show map keys used for \"database\" files.")
|
|
parser.add_argument("--lettercount",
|
|
default="lettercount",
|
|
type=argparse.FileType("r", encoding="UTF8"),
|
|
help="The input lettercount file as generated by lettercount")
|
|
args = parser.parse_args()
|
|
with args.lettercount as lettercount_file:
|
|
l2b = Letter2Bitmask(from_lettercount_file(lettercount_file))
|
|
print("We use this translation of letters to single-bit-bitmasks:\n{}".format(l2b._c2b))
|