2246_verso_tests/verso_spelling/spell.py

99 lines
2.6 KiB
Python

import json
from sys import argv
from os import path, listdir
from dataclasses import dataclass
@dataclass(frozen=True)
class Tube:
barcode: str
row: int
col: int
@dataclass(frozen=True)
class LetterTube:
barcode: str
index: int
group: int
def main():
# ARGV[1] Should be path to Ziath Output
# ARGV[2] Should be path to Letter sequences
# ARGV[3] Should be path to output
# ARGV[4] Should be word
available_barcodes = argv[1]
letter_dir = argv[2]
output_path = argv[3]
input = argv[4]
if not validate_word(input, letter_dir):
print("Failed to validate word")
exit(1)
available_tubes = read_available(available_barcodes)
letters = []
for letter in input:
x = get_tubes_for_letter(letter,
len(letters) + 1,
available_tubes,
letter_dir)
available_tubes = available_tubes[len(x):]
letters += x
output = letter_tubes_to_obj(letters)
with open(output_path, 'w') as file:
json.dump(output, file)
def letter_tubes_to_obj(x: [LetterTube]) -> dict:
output = {}
output["Items"] = [{"BarcodeValue": y.barcode} for y in x]
output["TargetPositions"] = [{"PositionIndex": y.index,
"GroupIndex": y.group}
for y in x]
return output
def get_tubes_for_letter(letter: str,
letter_index: int,
available_tubes: [Tube],
letter_dir: str) -> [LetterTube]:
positions = get_letter_positions(letter, letter_dir)
if len(positions) > len(available_tubes):
raise Exception("Insufficient tubes to fill order")
pixels = []
for i, position in enumerate(positions):
pixel = LetterTube(available_tubes[i],
position,
letter_index)
pixels.append(pixel)
return pixels
def read_available(p: str) -> [str]:
with open(p, 'r') as file:
return [x.strip() for x in file.readlines()]
def validate_word(input: str, letter_dir: str) -> bool:
letters_available = [x[0] for x in listdir(letter_dir)]
has_required_letters = all([x in letters_available for x in input])
return has_required_letters
def get_letter_positions(letter: str, letter_dir: str) -> [int]:
filename = [f for f in listdir(letter_dir) if f[0] == letter][0]
with open(path.join(letter_dir, filename), 'r') as file:
return [int(x.strip()) for x in file.readlines()]
if __name__ == "__main__":
main()