def parse_etyl(template: str) -> Iterator[Ref]: mot = extract_named_argument(template, "mot") if mot is not None: yield Ref(word=mot, origin=None, kind="etyl") dif = extract_named_argument(template, "dif") if dif is not None: yield Ref(word=dif, origin=None, kind="etyl") if mot is None: separators = template.count("|") if separators >= 3: parts = template.split("|") yield Ref(word=parts[3], origin=None, kind="etyl")
def parse_affix(parts: List[str]) -> Iterator[Ref]: """This template shows the parts (morphemes) that make up a word, for use in etymology sections. Although it is called affix, it can be used for compounds too, like {{compound}}: https://en.wiktionary.org/wiki/Template:affix/documentation """ if len(parts) > 2: yield Ref(origin=parts[1], word="".join(parts[2:]), kind="affix")
def parse_derived(parts: List[str]) -> Iterator[Ref]: """This template is used to format the etymology of terms derived from another language. it combines the functions of {{etyl}} and {{m}} into a single template, with fewer keystrokes and without writing out the language codes twice: https://en.wiktionary.org/wiki/Template:derived/documentation. """ if len(parts) > 3: yield Ref(origin=parts[2], word=parts[3], kind="derived")
def parse_prefix(parts: List[str]) -> Iterator[Ref]: """https://en.wiktionary.org/wiki/Template:prefix/documentation""" if len(parts) == 2: yield Ref(origin=None, word=f"{parts[1]}-", kind="prefix") yield Ref(origin=None, word=parts[1], kind="prefix-") elif len(parts) == 3: yield Ref(origin=None, word=f"{parts[1]}- + {parts[2]}", kind="prefix") yield Ref(origin=None, word=parts[1], kind="prefix-") yield Ref(origin=None, word=parts[2], kind="-prefix") elif len(parts) == 4: yield Ref(origin=parts[1], word=f"{parts[2]}- + {parts[3]}", kind="prefix") yield Ref(origin=parts[1], word=parts[2], kind="prefix-") yield Ref(origin=parts[1], word=parts[3], kind="-prefix")
def parse_suffix(parts: List[str]) -> Iterator[Ref]: """https://en.wiktionary.org/wiki/Template:suffix/documentation""" if len(parts) == 2: yield Ref(origin=None, word=f"-{parts[1]}", kind="suffix") yield Ref(origin=None, word=parts[1], kind="-suffix") elif len(parts) == 3: yield Ref(origin=None, word=f"{parts[1]} + -{parts[2]}", kind="suffix") yield Ref(origin=None, word=parts[1], kind="suffix-") yield Ref(origin=None, word=parts[2], kind="-suffix") elif len(parts) == 4: yield Ref(origin=parts[1], word=f"{parts[2]} + -{parts[3]}", kind="suffix") yield Ref(origin=parts[1], word=parts[2], kind="suffix-") yield Ref(origin=parts[1], word=parts[3], kind="-suffix") # else: # print("SUFFIX", parts) return None
def parse_blend(parts: List[str]) -> Iterator[Ref]: """https://en.wiktionary.org/wiki/Template:blend/documentation""" if len(parts) == 3: yield Ref(origin=None, word=f"{parts[1]} ~ {parts[2]}", kind="blend") yield Ref(origin=None, word=parts[1], kind="blend-") yield Ref(origin=None, word=parts[2], kind="-blend") elif len(parts) == 4: yield Ref(origin=parts[1], word=f"{parts[2]} ~ {parts[3]}", kind="blend") yield Ref(origin=parts[1], word=parts[2], kind="blend-") yield Ref(origin=parts[1], word=parts[3], kind="-blend")
def parse_clipping(parts: List[str]) -> Iterator[Ref]: """https://en.wiktionary.org/wiki/Template:clipping/documentation""" if len(parts) == 2: yield Ref(origin=None, word=parts[1], kind="clipping") elif len(parts) == 3: yield Ref(origin=parts[1], word=parts[2], kind="clipping")
def parse_borrowed(parts: List[str]) -> Iterator[Ref]: """This template is used to format the etymology of borrowings and loanwords: https://en.wiktionary.org/wiki/Template:borrowed/documentation """ if len(parts) > 3: yield Ref(origin=parts[2], word=parts[3], kind="borrowed")
def parse_non_cognate(parts: List[str]) -> Iterator[Ref]: """https://en.wiktionary.org/wiki/Template:noncognate""" if len(parts) > 2: yield Ref(origin=parts[1], word=parts[2], kind="noncog")
def parse_cognate(parts: List[str]) -> Iterator[Ref]: """https://en.wiktionary.org/wiki/Template:cognate/documentation""" if len(parts) > 2: yield Ref(origin=parts[1], word=parts[2], kind="cognate")
def parse_semantic_loan(parts: List[str]) -> Iterator[Ref]: """https://en.wiktionary.org/wiki/Template:semantic_loan/documentation""" if len(parts) > 3: yield Ref(origin=parts[2], word=parts[3], kind="semantic-loan")
def parse_back_form(parts: List[str]) -> Iterator[Ref]: """https://en.wiktionary.org/wiki/Template:back-formation/documentation""" if len(parts) == 2: yield Ref(origin=None, word=parts[1], kind="back-formation") elif len(parts) == 3: yield Ref(origin=parts[1], word=parts[2], kind="back-formation")
def parse_lien(template: str) -> Iterator[Ref]: if template.count("|") == 2: parts = template.split("|") yield Ref(word=parts[1], origin=None, kind="lien")
def parse_recons(template: str) -> Iterator[Ref]: if template.count("|") == 1: yield Ref(word=template.split("|")[-1], origin=None, kind="recons")
def parse_link(link: str) -> Iterator[Ref]: if "|" in link: yield Ref(word=link.rsplit("|", 1)[-1], origin=None, kind="link") else: yield Ref(word=link, origin=None, kind="link")
def parse_compound(parts: List[str]) -> Iterator[Ref]: """https://en.wiktionary.org/wiki/Template:compound/documentation""" if len(parts) == 3: yield Ref(origin=None, word=f"{parts[1]} + {parts[2]}", kind="compound") yield Ref(origin=None, word=parts[1], kind="compound-") yield Ref(origin=None, word=parts[2], kind="-compound") elif len(parts) == 4: yield Ref(origin=parts[1], word=f"{parts[2]} + {parts[3]}", kind="compound") yield Ref(origin=parts[1], word=parts[2], kind="compound-") yield Ref(origin=parts[1], word=parts[3], kind="-compound") elif len(parts) == 5: yield Ref( origin=parts[1], word=f"{parts[2]} + {parts[3]} + {parts[4]}", kind="compound", ) yield Ref(origin=parts[1], word=parts[2], kind="compound-") yield Ref(origin=parts[1], word=parts[3], kind="-compound-") yield Ref(origin=parts[1], word=parts[4], kind="-compound") elif len(parts) == 6: yield Ref( origin=parts[1], word=f"{parts[2]} + {parts[3]} + {parts[4]} + {parts[5]}", kind="compound", ) yield Ref(origin=parts[1], word=parts[2], kind="compound-") yield Ref(origin=parts[1], word=parts[3], kind="-compound-") yield Ref(origin=parts[1], word=parts[4], kind="-compound-") yield Ref(origin=parts[1], word=parts[5], kind="-compound")
def parse_inherited(parts: List[str]) -> Iterator[Ref]: """This template is used to format the etymology of terms inherited from an earlier stage of the same language: https://en.wiktionary.org/wiki/Template:inherited. """ if len(parts) > 3: yield Ref(origin=parts[2], word=parts[3], kind="inherit")
def parse_short_for(parts: List[str]) -> Iterator[Ref]: """https://en.wiktionary.org/wiki/Template:clipping/documentation""" if len(parts) > 2: yield Ref(origin=None, word=parts[1], kind="short-for")
def deserialize_ref(ref: str) -> Optional[Ref]: try: word, kind, origin = ref.strip().split("|") return Ref(word=word, kind=kind, origin=origin or None) except ValueError: return None