Skip to content
greek conversion

Python

Knowing what a conversion loses

Some conversions necessarily merge distinctions. convert_detailed() runs the conversion once and reports, token by token, what the target cannot express and where, so that an application can warn about a loss, highlight it, or rely on a round trip. What formats and options lose says which distinctions each format and option can lose, in general.

Each loss names what the target cannot keep (code) and locates it in the input. Writing monotonic Greek, for example, removes the breathings:

from greek_conversion import convert_detailed
monotonic = convert_detailed(
"ἄνθρωπος",
"greek",
"greek",
greek={"accentuation": "monotonic"},
)
monotonic.output # → άνθρωπος
monotonic.lossy # → True
monotonic.losses[0].code # → removed-diacritic
monotonic.losses[0].diacritic # → smooth
monotonic.losses[0].offset # → 0
# Monotonic or uncial Greek has no breathings; transliteration cannot tell
# "no breathing" from "smooth breathing", so reading it back adds one.
convert_detailed("άνθρωπος", "greek", "transliteration").losses[0].code
# → inferred-breathing

Each loss locates its token in the input (offset, length, in UTF-16 code units), and spans gives, for every token of the source, where it was read and where its output was written, so an application can highlight a loss or map a selection from one text to the other:

from greek_conversion import convert_detailed
detailed = convert_detailed("ἄνθρωπος", "greek", "transliteration")
span = detailed.spans[2]
"ἄνθρωπος"[span.input_start : span.input_end] # → θ
detailed.output[span.output_start : span.output_end] # → th

A text mixed with another language reports losses for the letters of that language, since the library converts the whole string and the target would read some of them as Greek. Filter the losses by code to keep the ones about the Greek. The filter also drops the few literals of the Greek itself that a target cannot keep, such as a parenthesis before a digit in Beta Code:

from greek_conversion import convert_detailed
mixed = convert_detailed(
"Voir λόγος ; cf. ψυχή.",
"greek",
"transliteration",
)
mixed.output # → Voir lógos ? cf. psychḗ.
mixed.lossy # → True
about_greek = [
loss
for loss in mixed.losses
if loss.code != "unrepresented-literal"
]
len(about_greek) # → 0

convert_greek_passages_detailed() converts the Greek passages only, copies the rest, shared punctuation included, and reports no loss for it:

from greek_conversion import convert_greek_passages_detailed
passages = convert_greek_passages_detailed(
"Voir λόγος ; cf. ψυχή.",
"transliteration",
)
passages.output # → Voir lógos ; cf. psychḗ.
passages.lossy # → False

When lossy is False, converting the output back with the same options gives the same canonical text; after a restyle, swap source and the other options. The loss codes are:

Code What the target cannot keep
removed-diacritic A diacritic of the source
displaced-diacritic The position of a breathing: a hiatus marked only by a breathing (Ἀίδης) reads back as a diphthong (Αἴδης)
added-diacritic A diaeresis that transliteration writes on a contextual y, or Greek output in capitals (greek.capital_marks), to keep a hiatus, or the accent greek.kai_accent writes on the καί of ϗ, which read back; diacritic identifies it
inferred-breathing The absence of a breathing on an initial vowel, which reads back as a smooth breathing
changed-case The letter case
removed-glyph-variant A letter form, such as ϲ, ϐ, or a σ at the end of a word; glyph_variant identifies it
added-glyph-variant A letter form the target writes where the source has none, and that converts back as another spelling; glyph_variant identifies it
unrepresented-grapheme A letter, or its position
unrepresented-literal A literal token

See the information-loss contract for each format pair and option, and the conversion-analysis contract for the result format.