Skip to content

Add Turkish text normalization (tr) - #547

Open
dorukdumlu wants to merge 6 commits into
NVIDIA:mainfrom
dorukdumlu:tr-tn
Open

dorukdumlu wants to merge 6 commits into
NVIDIA:mainfrom
dorukdumlu:tr-tn

Conversation

@dorukdumlu

@dorukdumlu dorukdumlu commented Nov 29, 2026 •

Copy link
Copy Markdown

What does this PR do ?

Adds Turkish text normalization (--lang=tr): cardinal, ordinal, decimal, fraction, money, measure (with percent), date, time, telephone, whitelist and word.

written spoken
1.250.000, -5 bir milyon iki yüz elli bin, eksi beş
4'e, 1990'larda, 4'er dörde, bin dokuz yüz doksanlarda, dörder
3. taksit, II. Mehmet / Sırada 5. üçüncü taksit, ikinci Mehmet / Sırada beş.
3,05, 2,5 milyon, %2,5'luk üç virgül sıfır beş, iki buçuk milyon, yüzde iki buçukluk
3/4'ünü, 140/90 mmHg dörtte üçünü, yüz kırk bölü doksan milimetre cıva
1.250,50 TL'ye, 100 TL'dir bin iki yüz elli lira elli kuruşa, yüz liradır
5 km'ye, 120 km/sa, 30x20x10 cm beş kilometreye, saatte yüz yirmi kilometre, otuz çarpı yirmi çarpı on santimetre
01.01.2027'de, 31 Aralık'a, 2026-03-15 bir ocak iki bin yirmi yedide, otuz bir aralığa, on beş mart iki bin yirmi altı
14:30'da, saat 17.30, 09:00-18:00 on dört otuzda, saat on yedi otuz, dokuz ile on sekiz
3-5 gün, 2-1, 3:2, 3.12.1 üç ile beş gün, iki bir, üçe iki, üç nokta on iki nokta bir
kodunuz: 482913, 12-34-56 kodunuz: dört sekiz iki dokuz bir üç, on iki, otuz dört, elli altı
0532 123 45 67'den sıfır beş yüz otuz iki, yüz yirmi üç, kırk beş, altmış yediden

Turkish-specific design

  • Suffixes are rebuilt, not copied. Turkish writes a case or possessive suffix after an apostrophe, but the writer chooses it for what they have in mind (TL'ye for the letters "te-le", %2,5'luk for "iki buçuk") and sometimes misspells it (4'de). The words a suffix can land on are a closed set (number words, currency, units, month names, "buçuk"), so inflection.py maps every surface form of the common suffixes to the form each of these words takes: vowel harmony, d/t assimilation, buffer consonants and softening (dört → dördü, aralık → aralığa). The result is one string map; an unknown suffix is copied as written at a higher cost.
  • Word order differs from the written order for fractions (denominator first, in the locative: 3/4 → "dörtte üç"), ISO dates (day month year), percent (read first) and speeds ("saatte ..."). ISO dates need no reordering in the grammar: the date fields are emitted without preserve_order, and the verbalizer's field order (day, month, year) puts them in spoken order, both in Python and in Sparrowhawk. Percent and speeds put the unit first and keep preserve_order; the speed word goes in morphosyntactic_features. Proper fractions with a named denominator (2-100 and 1000) are enumerated, since an FST cannot swap two unbounded strings; others are read "a bölü b".
  • Three composite classes cover patterns that span tokens. They compose the taggers and verbalizers of the simple classes (like en's RangeFst and SerialFst) and emit the spoken text as one name token:
    • RangeFst: ranges are read with "ile" (1914-1918, 09.00-17.30, 36,5-37,2 °C, %10-20); two numbers of up to three digits are compared, so 3-5 is a range but 11-9 and 2-0'lık are scores; 30x20x10 cm is read with "çarpı".
    • SerialFst: versions (3.12.1), ratios (3:2 → "üçe iki"), three or more dash-separated groups (12-34-56).
    • ContextFst: a number with a dot is an ordinal only before a lower-case word or a known noun (5. sırada, 1. Dünya Savaşı; at the end of a sentence Sırada 5. stays a number), in lower-cased text only before known nouns; saat 17.30 is a time (otherwise a decimal); a number after kodunuz/şifre/PIN is read digit by digit.
  • Readings chosen where Turkish has more than one: "ile" for ranges (not "ila"), "buçuk" for ,5 after a whole number, € as "euro". Letter-digit names (PS5, F1, GPT-4) are left as written.

Found by the Sparrowhawk tests

  • Sparrowhawk prints a name token as it is, so the non-breaking space that convert_space puts into a multi-word name stays in its output (vb. gave "ve benzeri" with a non-breaking space). The Turkish name tokens therefore keep plain spaces, and VerbalizeFinalFst accepts such names. The other languages' whitelist test files have no multi-word expansion, so they may be affected too.
  • Sparrowhawk does not print preserve_order, so the verbalizers treat it as optional (as en does); making it required gave empty output for percent and speeds.
  • 0,50 TL had two paths of equal weight; pynini and Sparrowhawk broke the tie differently. The zero-major path is removed when a minor part follows.
  • Sparrowhawk's token parser fails on a double quote inside a name token, both in a word ("Ben) and as a punctuation token (name: """), which is what en's PunctuationFst emits too; en does not run its punctuation cases in Sparrowhawk. The Turkish word class leaves the quote to punctuation, so pynini output is right, and the quoted-text cases run in pytest only.

Testing

  • 264 pytest cases in tests/nemo_text_processing/tr (one file per class, plus sentence-level, lower-cased and quoted-text cases); the cased ones except the quoted-text cases also pass in Sparrowhawk (export_grammars.sh --GRAMMARS=tn_grammars --LANGUAGE=tr --INPUT_CASE=cased --MODE=test: 12 test functions, OK).
  • Evaluation on general text: 301 sentences with numbers (240 written for this with an AI assistant, in eight domains: news, sports, health, recipes, transport, technology, history, customer service; plus the 61 distinct sentences with digits in FLEURS tr_tr test), split in half before the grammar was tuned. The held-out half (152 sentences) was run once, without changes: 84% of the sentences came out fully correct, 2% (3 sentences) had a reading that changes the meaning (802.11g read as grams, 140/90 mmHg read as a mixed fraction, 0212 444 0 123). Those three were fixed afterwards and are now regression tests, so the held-out score is the one before the fixes; the test half was not run again to tune on it. Most of the remaining 14% are tokens left as written (letter-digit names, rare units). I confirmed the defects the evaluation flagged.
  • Evaluation on independent text: 200 sentences with digits that we did not write, 100 from Turkish Wikipedia (2023-11-01 dump) and 100 from Turkish Wikinews, at most one per article, drawn with a fixed seed and frozen before the grammar was run on them; the grammar was not changed afterwards. 92.5% of the sentences came out fully correct (Wikinews 99%, Wikipedia 86%), 3% had a reading that changes the meaning, 2.5% were wrong but kept the meaning, 2% had digits left as written. All six meaning-changing errors are population figures written with an English thousands separator in bot-generated Wikipedia pages (12,865), which the grammar reads as a Turkish decimal. The others: 6/8 as a musical meter ("altı sekizlik"), 1885-6, Naked Gun 2½, an ISBN and BA-06/13. The labels were drafted with an AI assistant; I checked every row labeled wrong and a random 20 of the rows labeled correct, and agreed with all but one (the ISBN reading, left open).
  • Grammar size: the cased classify FAR is 16.6 MB, built in about 35 s on one CPU core.

Not in this PR

Letter-digit names and IBANs, e-mail and URLs, identifiers whose reading depends on context (order or policy numbers), and Turkish ITN. Only deterministic normalization is implemented; deterministic=False builds and gives the same option.

Known limitations:

  • The small-number comparison is a heuristic: 0-24 ay and 5-3 kişi are read as scores, 2024-118 as a range.
  • A suffix after an ISO date (2026-03-15'te) is not supported.
  • A single-letter unit written without a space (500g) is left as written, so that 5G or 802.11g are not read as grams.
  • A number written with an English thousands separator (12,865) is read as a Turkish decimal; treating every comma followed by three digits as a thousands separator would break decimals such as 2,500 kg.

The Jenkinsfile gets a grammar-build stage and a pytest stage with TR_TN_CACHE; that cache folder needs to be created on the CI machine.

Before your PR is "Ready for review"

Pre checks:

  • Have you signed your commits? Use git commit -s to sign.
  • Do all unittests finish successfully before sending PR?
    1. pytest or (if your machine does not have GPU) pytest --cpu from the root folder (given you marked your test cases accordingly @pytest.mark.run_only_on('CPU')).
    2. Sparrowhawk tests bash tools/text_processing_deployment/export_grammars.sh --MODE=test ...
  • If you are adding a new feature: Have you added test cases for both pytest and Sparrowhawk here.
  • Have you added __init__.py for every folder and subfolder, including data folder which has .TSV files?
  • Have you followed codeQL results and removed unused variables and imports (report is at the bottom of the PR in github review box) ?
  • Have you added the correct license header Copyright (c) 2023, NVIDIA CORPORATION & AFFILIATES. All rights reserved. to all newly added Python files?
  • If you copied nemo_text_processing/text_normalization/en/graph_utils.py your header's second line should be Copyright 2015 and onwards Google, Inc.. See an example here.
  • Remove import guards (try import: ... except: ...) if not already done.
  • If you added a new language or a new feature please update the NeMo documentation (lives in different repo).
  • Have you added your language support to tools/text_processing_deployment/pynini_export.py.

PR Type:

  • New Feature
  • Bugfix
  • Documentation
  • Test

Adds Turkish text normalization: cardinal, ordinal, decimal, fraction, money, measure (with percent), date, time,
telephone, whitelist and word.

- Suffixes written after an apostrophe are attached to the spoken number ("4'e" -> "dörde", "1990'lı" -> "bin
  dokuz yüz doksanlı", "%14'ü" -> "yüzde on dördü"); "dört" softens before a vowel.
- "5." is an ordinal only before a lower-case word ("3. taksit" -> "üçüncü taksit"); at the end of a sentence it
  stays a number.
- Fractions are read denominator first ("3/4" -> "dörtte üç"), dates day-month-year, the percent sign first.

Signed-off-by: Doruk Dumlu <dorukdumlu@windowslive.com>
- A multi-word whitelist expansion is emitted as one name token per word: Sparrowhawk prints name tokens as they
  are, so the non-breaking space from convert_space stayed in its output ("vb." -> "ve benzeri").
- "0,50 TL" had two paths of equal weight ("elli kuruş" and "sıfır lira elli kuruş"); pynini and Sparrowhawk broke
  the tie differently. The zero major path is gone when a minor part follows.

Signed-off-by: Doruk Dumlu <dorukdumlu@windowslive.com>
Suffixes after an apostrophe are rebuilt for the last word read (vowel harmony, d/t, buffer consonants, softening), so 'TL'ye' -> 'liraya', 'Aralık'a' -> 'aralığa', '%2,5'luk' -> 'yüzde iki buçukluk', '4'de' -> 'dörtte'. Adds ranges and scores, Roman numerals, ratios, dimensions, speeds, versions, codes after 'kodunuz', 'saat 17.30', and fixes amounts with kuruş plus a suffix, lower-cased currency codes and English number formats.

Signed-off-by: Doruk Dumlu <dorukdumlu@windowslive.com>
Restores fraction, measure units and telephone. Only proper fractions are read denominator first; others use 'bölü' (140/90 mmHg). A unit written without a space needs two or more letters (802.11g, 5G stay names). Adds scores with zero, date and four-digit ranges, 'saatte ... km/sa', an area code before a call-centre number, and kcal, g/dl, IU/ml, nm, MP.

Signed-off-by: Doruk Dumlu <dorukdumlu@windowslive.com>
- RangeFst, SerialFst and ContextFst (like en's range and serial classes) replace the context patterns that lived
  in tokenize_and_classify; they read their neighbours with composed taggers and verbalizers and emit name tokens.
- "bölü" and "saatte" are morphosyntactic_features instead of text inside fields; "ile" and "nokta" are part of
  the composed readings instead of injected tokens.
- Multi-word names keep plain spaces (no per-word name tokens), ISO dates rely on the Date proto field order
  instead of enumerating day and month, and suffixes are attached once per fraction: the tagger goes from 1.15M to
  365k states and the FAR from 39.5 MB to 16.6 MB.
- Fixes: lower-case currency codes only after an amount and never "try"; a dotted time needs a case suffix
  ("1.25'lik" is a decimal); the unbounded fst ** 0 in telephone groups.
- Unit tests for the suffix engine, negative tests for over-matches. A suffix after an ISO date is no longer
  supported.

Signed-off-by: Doruk Dumlu <dorukdumlu@windowslive.com>
- Double quotes are left to PunctuationFst: a quote inside a name token
  breaks Sparrowhawk parsing; quoted cases are pytest-only (as in en).
- ContextFst: saat with a time range, codes after a colon and in dash
  groups, capitalised fixed-phrase nouns (1. Dunya Savasi, 1. Lig).
- SerialFst: three or more dash-separated groups read as a code.
- Lower-case unit variants; stale docstrings fixed.

Signed-off-by: Doruk Dumlu <dorukdumlu@windowslive.com>
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.

1 participant