ks-g2p
This repo contains code, not model weights: a rule-based Python package and command-line pipeline.
Kashmiri (Perso-Arabic script) → IPA phonemes, for text-to-speech front ends and forced-alignment lexicons (Montreal Forced Aligner).
کٲشُر زَبان، دٮ۪د تہٕ گۄلاب۔ → kəːʃur zabaːn, dʲad tɨ ɡɔlaːb.
- Reads the script in context: و / ی as vowel or consonant, final ہ as vowel carrier, ؠ palatalization, sukun, shadda, the Arabic article ال, hamza / ayn carriers, and the legacy and ASR spellings found in real corpora (ۍ, ٮ۪, ۅ, ۭ, ٛ clusters). The reader is the one in the ks-devanagari transliterator, so both packages read a word the same way.
- Phoneme values from r12a's Kashmiri orthography notes: ژ ʦ, ژھ ʦʰ, ٕ ɨ, ٟ ɨː, ٔ ə, ٲ əː, ۄ ɔ, ؠ ʲ; loan letters ق k, غ ɡ, ع ʔ, ث ص s, ذ ض ظ z, ح h, ط t. خ is written as the native kʰ (r12a gives x, "sometimes aspirated"; Wiktionary writes kʰ in 16 of 21 entries).
- Sound rules the spelling does not show: nasal place (رَنگ raŋɡ, اَنبار ambaːr), nasal vowels (چھے٘ ʧʰeː̃), unwritten a in unmarked clusters (کران karaːn, یِمن jiman), final ہہ = h (کیٚنٛہہ kenh), final ہ after a written short vowel = h (دٔہ dəh), the genitive hund / hindʲ / hanz (تُہنٛدِس tuhundis), final ُو = uʋ (نیٚچُو neʧuʋ), y-glides (بیٛارِ bʲaːri, سُے suj, زٲمِیہِ zəːmiji).
- TTS-ready symbols: output uses only symbols of the Kokoro / Mithi phoneme vocabulary (one symbol per affricate: ʦ ʧ ʤ).
- Exceptions list for loanwords, extendable from a file.
- Pure Python, no dependencies. Each distinct word is converted once and cached.
Accuracy: against Wiktionary's Kashmiri IPA (1,197 words, notation differences normalised): 97.2 % of words exact, 0.63 % character error rate, 99.0 % within one edit.
Installation
Python 3.8 or newer, no dependencies.
pip install "ks-g2p @ git+https://huggingface.co/Omarrran/ks-g2p"
While the repo is private, log in first with a token that can read it (hf auth login), or
download it with hf download Omarrran/ks-g2p --local-dir ks-g2p and run pip install ./ks-g2p.
Or from a local copy of this folder:
pip install .
For development: pip install -e ".[test]" and pytest.
Python usage
from ks_g2p import phonemize, word_to_ipa, word_phones, load_exceptions
phonemize('کٲشُر زَبان، دٮ۪د تہٕ گۄلاب۔') # 'kəːʃur zabaːn, dʲad tɨ ɡɔlaːb.'
word_to_ipa('چھُ') # 'ʧʰu'
word_phones('دؠد') # ('dʲ', 'a', 'd') — tokens for lexicons
load_exceptions('my_words.tsv') # word<TAB>IPA lines, or a filled-in review sheet
| Function | What it does |
|---|---|
phonemize(text, ...) |
Text → phoneme string. Words are separated by spaces; ۔ ؟ ، ؛ become . ? , ;. Latin text and digits pass through unchanged. |
word_to_ipa(word, ...) |
One word → IPA string. |
word_phones(word, ...) |
One word → tuple of phone tokens (ʰ ʲ ː and nasalization stay on their phone). |
load_exceptions(path) |
Add whole-word pronunciations from a TSV file. Returns how many were read. |
unsupported(ipa) |
Characters of an IPA string that are not in the Kokoro / Mithi vocabulary. |
| Option | Default | Meaning |
|---|---|---|
implicit_schwa |
False |
Text written without short-vowel marks (Urdu style): a bare consonant before another keeps a. |
unwritten_a |
True |
Read an unmarked consonant pair at the start or end of a word as CaC. |
glottal |
True |
Write medial / final ع as ʔ (r12a). False drops it. |
With pandas / Hugging Face datasets
df['ipa'] = df['text'].map(phonemize)
ds = ds.map(lambda b: {'ipa': [phonemize(t) for t in b['text']]}, batched=True, num_proc=8)
Command-line pipeline
# A quick string
ks-g2p -t "کٲشُر زَبان"
# A text file, line by line (keeps blank lines and CRLF/LF endings); several files or stdin work too
ks-g2p input.txt -o phonemes.txt
cat input.txt | ks-g2p > phonemes.txt
# JSONL: adds "text_ipa" to every record
ks-g2p data.jsonl -f jsonl --field text -o data.ipa.jsonl
# CSV / TSV: adds "<column>_ipa" next to the source column
ks-g2p data.csv -f csv --column sentence -o data.ipa.csv
# Pronunciation lexicon for Montreal Forced Aligner: word<TAB>phones (+ count), most frequent first
ks-g2p corpus.txt --lexicon -o lexicon.tsv
ks-g2p data.jsonl -f jsonl --field text --lexicon --counts -o lexicon.tsv
# Your own pronunciations, big files on several processes
ks-g2p corpus.txt --exceptions my_words.tsv -o corpus.ipa.txt
ks-g2p corpus.txt -j 8 -o corpus.ipa.txt
python -m ks_g2p ... works the same as ks-g2p ....
| Flag | Meaning |
|---|---|
FILE ... |
Input files. If none are given, or - is given, reads stdin. |
-t, --text TEXT |
Convert this string instead of reading files. |
-o, --output FILE |
Output file (default: stdout). |
-f, --format |
text (default), jsonl, csv or tsv. |
--field NAME |
JSONL field to convert (default text). A list of strings works too. |
--column NAME |
CSV/TSV column to convert (default text). |
--out-field NAME |
Output field/column name (default <field>_ipa). Use the same name to overwrite. |
--lexicon |
Write word<TAB>phones, one line per distinct word, most frequent first. |
--counts |
With --lexicon: add the word count as a third column. |
--exceptions TSV |
Extra whole-word pronunciations; may be repeated. |
--implicit-schwa |
Input has no short-vowel marks. |
--no-unwritten-a |
Do not supply a in unmarked consonant pairs. |
--no-glottal |
Drop ع instead of writing ʔ. |
-j, --workers N |
Number of processes (default 1). Output is identical for any N. |
--encoding |
Input/output encoding (default utf-8). |
Phoneme reference
| Sound | Written | Example |
|---|---|---|
| a / aː | zabar َ / ا آ | اَپُز apuz, آب aːb |
| ə / əː | ٔ أ / ٲ | گٔر ɡər, دٲن dəːn |
| i / iː | zer ِ / یٖ, final ی | اِنسان insaːn, سیٖر siːr |
| ɨ / ɨː | ٕ / ٟ (ٳ) | کٔدٕل kədɨl, تٟر tɨːr |
| u / uː | pesh ُ / وٗ | کُن kun, پوٗت puːt |
| e / eː | یٚ ےٚ / medial ی, final ے | مےٚ me, ترٛے treː |
| o / oː | وٚ ۆ / و | دۆب dob, مور moːr |
| ɔ / ɔː | ۄ / ۄا | گۄلاب ɡɔlaːb, سۄاد sɔːd |
| ʲ | ؠ ۍ ٮ۪ | دؠد dʲad, ٲسۍ əːsʲ |
| nasal | ں نٛ | چھے٘ ʧʰeː̃, سُنٛد sund |
Consonants: پ p, ب b, ت t, د d, ٹ ʈ, ڈ ɖ, ک k, گ ɡ, ژ ʦ, چ ʧ, ج ʤ, س s, ز z, ش ʃ, ہ h, م m, ن n, و ʋ, ر r, ل l, ی j, ڑ ɽ, ف f, خ kʰ. Aspirates with ھ: pʰ tʰ ʈʰ kʰ ʧʰ ʦʰ (and bʰ dʰ ɖʰ ɡʰ ʤʰ ɽʰ in Urdu / Hindi loans).
Tools
| Script | Purpose |
|---|---|
tools/coverage.py |
Coverage report for a corpus and a frequency-ranked review sheet (word, IPA, empty correct_ipa). A filled-in sheet loads with load_exceptions / --exceptions. |
tools/eval_wiktionary.py |
Scores the package against Wiktionary's Kashmiri IPA. |
tools/export_web.py |
Generates the web app's tables and parity vectors from this package (web/). |
Web app
web/ is the Kashmiri G2P Space: a Next.js
app with the engine ported to TypeScript, tested to give identical output to this package on
174,876 vectors (cd web && npm test).
Limitations
- Vowels that are not written can only be guessed.
unwritten_aandimplicit_schwaare heuristics; loanwords in Urdu spelling may need ٗ / ٚ in the text or an exceptions entry. - An unmarked C + r onset is read with a (کران karaːn), so clusters typed without ٛ need it.
- Final ہ after a bare consonant is the silent -a ending (نہ na); after a written short vowel it is h (دٔہ dəh), except pronouns in the exceptions list (سُہ su).
- Numbers are not expanded to words; no stress marks; dialect differences are not modelled.
License
Apache-2.0. Author: Haq Nawaz Malik. The Perso-Arabic reader is shared with the ks-devanagari transliterator (same author, same license).