|
| 1 | +from dataclasses import dataclass, is_dataclass |
| 2 | +from typing import Any, Dict, List, Literal, Optional |
| 3 | + |
| 4 | +__all__ = ( |
| 5 | + "Language", |
| 6 | + # dictionaries |
| 7 | + "Dictionary", |
| 8 | + # querying |
| 9 | + "Translation", |
| 10 | + "Arab", |
| 11 | + "Rom", |
| 12 | + "Hit", |
| 13 | + "EntryHit", |
| 14 | + "TranslationHit", |
| 15 | + "NestedEntryHit", |
| 16 | + "create_hit", |
| 17 | +) |
| 18 | + |
| 19 | + |
| 20 | +Language = Literal[ |
| 21 | + "de", "el", "en", "es", "fr", "it", "pl", "pt", "ru", "sl", "tr", "zh" |
| 22 | +] |
| 23 | + |
| 24 | + |
| 25 | +@dataclass(repr=False) |
| 26 | +class Dictionary: |
| 27 | + key: str |
| 28 | + simple_label: str |
| 29 | + directed_label: Dict[str, str] |
| 30 | + languages: List[Language] |
| 31 | + |
| 32 | + def __repr__(self) -> str: |
| 33 | + return f"Dictionary(key='{self.key}')" |
| 34 | + |
| 35 | + |
| 36 | +@dataclass(repr=False) |
| 37 | +class Translation: |
| 38 | + source: str |
| 39 | + target: str |
| 40 | + |
| 41 | + def __repr__(self) -> str: |
| 42 | + return "Translation()" |
| 43 | + |
| 44 | + |
| 45 | +class Arab: |
| 46 | + header: str |
| 47 | + translations: List[Translation] |
| 48 | + |
| 49 | + def __init__(self, data: Dict[str, Any]) -> None: |
| 50 | + self.header = data["header"] |
| 51 | + self.translations = [ |
| 52 | + Translation(**translation) for translation in data["translations"] |
| 53 | + ] |
| 54 | + |
| 55 | + def __repr__(self) -> str: |
| 56 | + return f"Arab(header='{self.header}')" |
| 57 | + |
| 58 | + |
| 59 | +class Rom: |
| 60 | + headword: str |
| 61 | + headword_full: str |
| 62 | + wordclass: Optional[str] |
| 63 | + arabs: List[Arab] |
| 64 | + |
| 65 | + def __init__(self, data: Dict[str, Any]) -> None: |
| 66 | + self.headword = data["headword"] |
| 67 | + self.headword_full = data["headword_full"] |
| 68 | + self.wordclass = data["wordclass"] |
| 69 | + self.arabs = [Arab(arab) for arab in data["arabs"]] |
| 70 | + |
| 71 | + def __repr__(self) -> str: |
| 72 | + return f"Rom(headword='{self.headword}')" |
| 73 | + |
| 74 | + |
| 75 | +class Hit: |
| 76 | + type: Literal["entry", "translation", "entry_with_secondary_entries"] |
| 77 | + |
| 78 | + def __repr__(self) -> str: |
| 79 | + return f"{self.__class__.__name__}()" |
| 80 | + |
| 81 | + |
| 82 | +class EntryHit(Hit): |
| 83 | + type = "entry" |
| 84 | + opendict: bool |
| 85 | + roms: List[Rom] |
| 86 | + |
| 87 | + def __init__(self, data: Dict[str, Any]) -> None: |
| 88 | + self.opendict = data["opendict"] |
| 89 | + self.roms = [Rom(rom) for rom in data["roms"]] |
| 90 | + |
| 91 | + @property |
| 92 | + def translations(self) -> List[str]: |
| 93 | + to_return = [] |
| 94 | + for rom in self.roms: |
| 95 | + for arab in rom.arabs: |
| 96 | + for translation in arab.translations: |
| 97 | + to_return.append(translation.target) |
| 98 | + return to_return |
| 99 | + |
| 100 | + |
| 101 | +@dataclass(repr=False) |
| 102 | +class TranslationHit(Hit, Translation): |
| 103 | + type = "translation" |
| 104 | + opendict: bool |
| 105 | + |
| 106 | + |
| 107 | +class NestedEntryHit(Hit): |
| 108 | + type = "entry_with_secondary_entries" |
| 109 | + primary: EntryHit |
| 110 | + secondary: List[EntryHit] |
| 111 | + |
| 112 | + def __init__(self, data: Dict[str, Any]) -> None: |
| 113 | + self.primary = EntryHit(data["primary_entry"]) |
| 114 | + self.secondary = [EntryHit(entry) for entry in data["secondary_entries"]] |
| 115 | + |
| 116 | + |
| 117 | +def create_hit(data: Dict[str, Any]) -> Hit: |
| 118 | + hit_types = {cls.type: cls for cls in [EntryHit, TranslationHit, NestedEntryHit]} |
| 119 | + cls = hit_types[data.pop("type")] |
| 120 | + if is_dataclass(cls): |
| 121 | + return cls(**data) |
| 122 | + return cls(data) |
0 commit comments