Repository navigation
Preserve non-Latin script words when replacing homoglyphs - #258
Merged
Merged
Conversation
…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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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:Refactored replacement logic: Extracted common logic into
replace()function used by bothreplace_homoglyphs()andreplace_homoglyphs_in_name(), with a flag to control dotless ı (U+0131) handlingDotless ı handling: The Turkish dotless ı is now excluded from the homoglyph table (matching Java behavior) and handled specially:
iand replacedHelper functions: Added
words()to identify word boundaries andis_latin()to detect Latin script characters across all Latin Unicode blocksRegex improvements: Updated cultivar epithet regexes to allow apostrophes ending words (transliterated soft signs, glottal stops in livestock breed names)
Test coverage:
homoglyph-cases.txtwith 29 test cases shared between Rust and Java implementationsNotable Implementation Details
replace()returns immediately if no homoglyphs or dotless ı are found(usize, usize)tuples to track which byte ranges should be preserved, avoiding character-by-character script detection during replacementhomoglyph-cases.txtis tested by both Rust and Java to ensure implementations remain synchronized