Skip to content
greek conversion

Python

Conversion-analysis contract

convert_detailed() returns the converted text together with an analysis of the information retained by the target representation:

from greek_conversion import convert_detailed
result = convert_detailed(
"ἄνθρωπος", "greek", "greek", greek={"accentuation": "monotonic"}
)
result.output # → άνθρωπος
result.lossy # → True
loss = result.losses[0]
loss.code # → removed-diacritic
loss.diacritic # → smooth
loss.index # → 0
loss.offset # → 0
loss.length # → 1
loss.message
# → The target representation does not retain the smooth breathing.
# One span per source token.
len(result.spans) # → 8
result.spans[0]
# → ConversionSpan(input_start=0, input_end=1, output_start=0, output_end=1)
result.spans[1]
# → ConversionSpan(input_start=1, input_end=2, output_start=1, output_end=2)

convert() continues to return only a string. Both functions share the same parse-and-encode execution; detailed conversion additionally parses the target output to compare its canonical document with the source document. When the two documents do not have the same letters, it reads the output a second time with positions, to pair each target token with the source token whose output range it starts in. It does not encode the conversion a second time.

A conversion is lossy when the canonical target document does not retain all information present in the canonical source document. The comparison is about letters, case, diacritics, known glyph variants, punctuation meaning, and literal tokens—not byte or code-point identity.

Consequently:

  • NFC/NFD and tonos/oxia changes are not losses;
  • selected Greek punctuation glyphs are not losses;
  • a letter form (glyph_variant) is lost when the source format, under the options of the conversion, writes the letter differently from the source and from the reparsed target: a form the options fold, or write again from its position, is not a loss (see Letter forms);
  • deterministic marks added by an output policy are not losses;
  • removed diacritics, monotonic simplification, numeral conversion, dental assimilation, consonant changes, and unrepresented source tokens are losses when they occur in the actual input.

lossy is equivalent to len(losses) > 0 and is included as a convenience for callers that only need to display a warning.

ConversionLoss.index is the token index in the parsed source Document. offset and length locate that token in the input string, in characters (see Positions); text[offset : offset + length] is the text the loss is about.

Code Meaning
changed-case A source letter is retained but its uppercase/lowercase distinction is not
removed-glyph-variant A letter form of the source (the lunate, final, or medial sigma, or a symbol form such as ϐ) is absent from the reparsed target, and the options do not write it again; glyph_variant identifies it
added-glyph-variant The target writes a letter form the source did not have, which reads back as another spelling (a lunate sigma written c under lunate_sigma="c"); glyph_variant identifies the form written
removed-diacritic A source diacritic is absent from the reparsed target; diacritic identifies it
displaced-diacritic A source breathing is absent from its vowel but reappears on the following vowel of the reparsed target: a hiatus marked only by that breathing is read as a diphthong; diacritic identifies it
added-diacritic The target writes a diacritic the source token did not have, which reads back: the diaeresis on a contextual y that keeps a hiatus the reader would otherwise lose (ἀἑυ → aheÿ → ἀἑϋ), the diaeresis that greek.capital_marks: "omit" writes in capitals where an accent showed a hiatus (ΜΆΙΟΣ → ΜΑΪΟΣ), the accent of καί that greek.kai_accent writes where ϗ is written as letters, or a breathing that greek.double_rho: "smooth-rough" writes on a double rho read from a transliteration that writes it (rr → ῤῥ under rho="marked", which converts back to rrh); diacritic is "diaeresis", "acute", "grave", "smooth", or "rough"
inferred-breathing The target does not distinguish an unmarked initial vowel from one with a smooth breathing, so the reparsed target carries a smooth breathing the source did not have; index is the vowel that acquires it (the second element of an initial diphthong), and diacritic is "smooth"
unrepresented-grapheme A source letter or its position cannot be recovered from the target document
unrepresented-literal A source literal token cannot be recovered from the target document, a letter that a converter keeps out of its scope included, when its output does not convert back to its spelling in the source

The codes describe observable information loss rather than the implementation rule that caused it. A dental assimilation and an elision-induced consonant change can therefore both report unrepresented-grapheme.

spans has one entry per token of the parsed source, in token order: where the token was read in the input (input_start, input_end) and where its output was written (output_start, output_end). All four are offsets in characters as Python counts them, code points, the unit of len() and of slices, in the input exactly as supplied and in the output exactly as returned. The readers and writers normalize the text (NFD on input, NFC or NFD on output), but the positions are mapped back to the caller’s strings, so a precomposed ἄ is one character and the same letter written as α plus two combining marks is three. A character outside the Basic Multilingual Plane is one, as Python counts it. Ends are exclusive.

The readers and writers normalize the text one group at a time: a character and the code points that continue it, which are the combining marks that follow it and, in two scripts, the letters that canonical composition joins to the letter before them (the vowel and final jamo of Hangul, the vowel signs E and AI of Kirat Rai; they continue the group of whatever character precedes them). Where normalization leaves a group unchanged, or changes its code points one at a time without moving them (the ohm sign read as an omega), each code point keeps its own position. Where it composes, decomposes, or reorders the group’s code points, the tokens read from it or written to it cannot be told apart in the caller’s string, and each has the range of the whole group, even where its parts could have been separated. The span of a token therefore depends only on the groups it was read from or written to.

Input spans are in order and cover the input without gaps: their starts and their ends never decrease, and each span starts where the previous one ends or lies inside it and ends with it. Tokens read from one character of the input share its span: the three jamo of a Hangul syllable, which the reader keeps as literals, the two letters read from a modern-digraph b under ISO 843 Type 2, or the three letters read from ϗ, which share the span of the sign and of the marks after it. A breathing printed before a capital that the reader joins to it is one token with it (῾Ρ), and in Beta Code the < and > of an overline belong to the first and the last letter between them, in the input as in the output. A token read from part of a character that the previous token also draws on has that character’s span, inside the previous token’s: in pḥ written with a precomposed ḥ, the h belongs to the ph of phi, and the dot below, kept as a literal, has the span of ḥ.

Output spans are in order and cover the output: their starts and their ends never decrease, and each span starts where the previous one ends, lies inside it and ends with it, or is empty. Several tokens written as one share a span: the letters and numeral sign of a Greek numeral written as a decimal number, a whitespace run collapsed to one space, the two letters of μπ written as b, a dental and the sigma it is assimilated to. A token the output has nothing for (a leading or trailing whitespace run under whitespace="collapse") has an empty span where the output of the next token starts, or at the end of the output. A combining mark that stands alone in the input, kept as a literal, is written after the previous token’s output: its span is its own when the output keeps it apart, and that of the character it joins, inside the previous token’s, when the output composes or reorders them (an acute after a vowel).

These positions describe one conversion. The loss analysis compares documents, not positions, so spans is also returned when lossy is False.

A bound converter (create_converter()) reports the same positions. Its input spans are in the input as supplied, before the converter substitutes its own spellings: the span of a token read from an alias or a custom character is that of the spelling written in the input (#711 for a custom character spelled so in Beta Code), and the literals that preserve an out-of-scope character share the span of its spelling. Its output spans are in the output as returned, after the custom characters’ forms are written. The converter’s diagnostics carry the offset and length of the character they report, in the same input, both in convert_detailed() and in a CharacterScopeError; their index, like the losses’, is in the prepared document, so spans[diagnostic.index] is where the character was read.

The result describes the supplied input. It does not claim that the format pair is universally lossless. Greek to transliteration, for example, can be lossless for one document and lossy for another containing a coronis.

The pairwise risks and option-induced loss mechanisms are documented separately in the information-loss contract.