Skip to content

Preserve non-Latin script words when replacing homoglyphs - #258

Merged
mdoering merged 1 commit into
mainfrom
claude/sharp-cannon-61zo6c
Nov 19, 2026
Merged

mdoering merged 1 commit into
mainfrom
claude/sharp-cannon-61zo6c

Conversation

@mdoering

Copy link
Copy Markdown
Member

Summary

Refactors homoglyph replacement logic to preserve words written entirely in non-Latin scripts (Cyrillic, Greek, etc.) while still replacing look-alikes in Latin text. This fixes issue #257 where cultivar epithets and author names in other scripts were incorrectly having their letters replaced.

Key Changes

  • Script-aware homoglyph replacement: Implemented foreign_words() function to identify words written in non-Latin scripts that should be preserved. A word is considered "foreign" if:

    • It contains no Latin letters AND has at least one alphabetic character, AND
    • Either contains a letter with no Latin look-alike, OR has 3+ letters with at least one lowercase (indicating it's a real word, not random look-alikes)
  • Refactored replacement logic: Extracted common logic into replace() function used by both replace_homoglyphs() and replace_homoglyphs_in_name(), with a flag to control dotless ı (U+0131) handling

  • Dotless ı handling: The Turkish dotless ı is now excluded from the homoglyph table (matching Java behavior) and handled specially:

    • In names (genus/epithet): treated as a look-alike of i and replaced
    • In surnames: preserved as a distinct letter
  • Helper functions: Added words() to identify word boundaries and is_latin() to detect Latin script characters across all Latin Unicode blocks

  • Regex improvements: Updated cultivar epithet regexes to allow apostrophes ending words (transliterated soft signs, glottal stops in livestock breed names)

  • Test coverage:

    • Added homoglyph-cases.txt with 29 test cases shared between Rust and Java implementations
    • New tests for Cyrillic/Greek word preservation and dotless ı behavior
    • Updated homoglyph count assertion from 1733 to 1732 entries (dotless ı removed)

Notable Implementation Details

  • Early return optimization: replace() returns immediately if no homoglyphs or dotless ı are found
  • Byte range tracking: Uses (usize, usize) tuples to track which byte ranges should be preserved, avoiding character-by-character script detection during replacement
  • Shared test cases: homoglyph-cases.txt is tested by both Rust and Java to ensure implementations remain synchronized

…pt keep their letters

A cultivar epithet whose word ends in an apostrophe -- a glottal stop or a
transliterated soft sign in livestock breeds such as
`Ovis aries 'Kazakhskaya Korridel''` -- took that apostrophe for the closing
quote and became the authorship. An apostrophe after a letter that is followed
by the closing quote, or by whitespace inside a quoted epithet that ends the
name, now belongs to the epithet (#256).

The homoglyph replacement no longer garbles words written in another script:
`'Азербайджанский (кавказский) буйвол'` stays as it is instead of becoming
`Aɜep6aйджaHcĸий (ĸaBĸaɜcĸий) 6yйBoл` (#257). A word without Latin letters is
left alone if it has a letter with no Latin look-alike, or three letters or
more, one lower case; once there is such a word, so are all words without
Latin letters. Look-alikes in Latin text are still replaced (`LINE СЗ` ->
`LINE C3`).

`replace_homoglyphs` now behaves exactly like name-parser-api's
`UnicodeUtils.replaceHomoglyphs(s, false)`: the dotless ı is out of the shared
table, as in Java, and is only folded in a genus or epithet by
`replace_homoglyphs_in_name`. `resources/homoglyph-cases.txt` lists the cases
both implementations are tested against, from the crate and from the Java
binding's HomoglyphTableTest.

Fixes #256
Fixes #257
@mdoering
mdoering merged commit 05229e9 into main Nov 19, 2026
10 of 11 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants