### Build Epitran (System-wide) Source: https://github.com/dmort27/epitran/blob/master/README.md Configure and build Epitran for system-wide installation using sudo. This method requires root privileges. ```bash ./configure && make sudo make install cd testsuite && make lex_lookup sudo cp lex_lookup /usr/local/bin ``` -------------------------------- ### Build Epitran (Local/Conda) Source: https://github.com/dmort27/epitran/blob/master/README.md Configure and build Epitran for local or Conda environment installation without requiring sudo. Specify the installation prefix using --prefix. ```bash ./configure --prefix=$CONDA_PREFIX make && make install cd testsuite && make lex_lookup cp lex_lookup $CONDA_PREFIX/bin/ ``` -------------------------------- ### G2P Rule Example Source: https://github.com/dmort27/epitran/blob/master/README.md This is an example of a G2P rule used in Epitran for schwa deletion in Indic languages. It specifies a context for deletion. ```regex ə -> 0 / (::vowel::)(::consonant::) _ (::consonant::)(::vowel::) ``` -------------------------------- ### Instantiate Epitran for Different Languages Source: https://context7.com/dmort27/epitran/llms.txt Instantiate Epitran objects for specific languages and scripts. Use language codes and script tags to select the correct variant. For example, 'amh-Ethi-pp' for Amharic with a phonetic preprocessor. ```python epi_amh_pp = epitran.Epitran('amh-Ethi-pp') # Amharic (more phonetic) epi_ben_red = epitran.Epitran('ben-Beng-red') # Bengali (reduced) epi_deu_nar = epitran.Epitran('deu-Latn-nar') # German (more phonetic) epi_fra_np = epitran.Epitran('fra-Latn-np') # French (no preprocessor) ``` -------------------------------- ### Basic Transliteration with Epitran Class Source: https://context7.com/dmort27/epitran/llms.txt Instantiate the Epitran class with a language-script code (e.g., 'tur-Latn') and use the `transliterate()` method to convert orthographic text to IPA. Examples cover Turkish, Spanish, Hindi, German, Japanese, Korean, Arabic, and Uyghur. ```python import epitran # Turkish transliteration epi_turkish = epitran.Epitran('tur-Latn') print(epi_turkish.transliterate('Haziran\'da')) # Output: haziɾan'da print(epi_turkish.transliterate('otoparkın')) # Output: otopaɾkɯn # Spanish transliteration epi_spanish = epitran.Epitran('spa-Latn') print(epi_spanish.transliterate('queso')) # Output: keso print(epi_spanish.transliterate('cuestión')) # Output: kwestjon # Hindi transliteration with schwa deletion epi_hindi = epitran.Epitran('hin-Deva') print(epi_hindi.transliterate('भारत')) # Output: b̤aːrət print(epi_hindi.transliterate('देवनागरी')) # Output: devnaːɡriː # German transliteration epi_german = epitran.Epitran('deu-Latn') print(epi_german.transliterate('Düğün')) # Output: dyɰyn # Japanese Hiragana transliteration epi_japanese = epitran.Epitran('jpn-Hira') print(epi_japanese.transliterate('とうきょう')) # Output: toːkʲoː # Korean transliteration epi_korean = epitran.Epitran('kor-Hang') print(epi_korean.transliterate('무늬')) # Output: muni print(epi_korean.transliterate('희망')) # Output: himaŋ # Arabic transliteration epi_arabic = epitran.Epitran('ara-Arab') print(epi_arabic.transliterate('كتاب')) # Output: ktɑːb # Uyghur in Perso-Arabic script epi_uyghur = epitran.Epitran('uig-Arab') print(epi_uyghur.transliterate('ئۇيغۇر')) # Output: ujɣur ``` -------------------------------- ### English G2P with Flite Source: https://context7.com/dmort27/epitran/llms.txt Performs English transliteration by converting orthography to ARPAbet using CMU Flite, then mapping ARPAbet to IPA. Requires Flite with the 'lex_lookup' binary installed. Examples show basic transliteration and handling of ligatures. ```python import epitran # Requires: Flite with lex_lookup installed # Installation: # git clone https://github.com/festvox/flite.git # cd flite && ./configure && make # sudo make install # cd testsuite && make lex_lookup # sudo cp lex_lookup /usr/local/bin epi = epitran.Epitran('eng-Latn') # Basic English transliteration print(epi.transliterate('Berkeley')) # Output: bɹ̩kli print(epi.transliterate('phonetics')) # Output: fənɛtɪks print(epi.transliterate('pronunciation')) # Output: pɹənənsijeʃən # With ligatures epi_lig = epitran.Epitran('eng-Latn', ligatures=True) print(epi_lig.transliterate('judge')) ``` -------------------------------- ### Backoff Class Usage Source: https://github.com/dmort27/epitran/blob/master/README.md Demonstrates how to initialize and use the Backoff class for transliterating text across multiple scripts with a fallback strategy. It shows examples of transliterating Hindi, English, and Chinese using different language-script codes. ```APIDOC ## Backoff Class ### Description The `Backoff` class allows for transliterating text in multiple scripts by falling back to alternative language modes if one fails. It processes text on a token-by-token basis. ### Initialization `Backoff(lang_script_codes, cedict_file=None)` - `lang_script_codes` (list): A list of language-script codes (e.g., 'eng-Latn', 'hin-Deva'). - `cedict_file` (str, optional): Path to a CEDICT file. ### Limitations - Does not support parameterized preprocessor and postprocessor application. - Does not support non-standard ligatures. - Does not support punctuation normalization. - Tokens with mixed scripts are returned as an empty string. ### Public Methods - **transliterate**: Returns a unicode string of IPA phonemes. - **trans_list**: Returns a list of IPA unicode strings, each representing a phoneme. - **xsampa_list**: Returns a list of X-SAMPA (ASCII) strings, each representing a phoneme. ### Request Example ```python from epitran.backoff import Backoff backoff = Backoff(['hin-Deva', 'eng-Latn', 'cmn-Hans'], cedict_file='cedict_1_0_ts_utf-8_mdbg.txt') # Transliterate Hindi ipa_hindi = backoff.transliterate('हिन्दी') print(f"Transliterated Hindi: {ipa_hindi}") # Transliterate English ipa_english = backoff.transliterate('English') print(f"Transliterated English: {ipa_english}") # Transliterate Chinese ipa_chinese = backoff.transliterate('中文') print(f"Transliterated Chinese: {ipa_chinese}") # Example using trans_list ipa_list_hindi = backoff.trans_list('हिन्दी') print(f"Transliterated Hindi (list): {ipa_list_hindi}") # Example using xsampa_list xsampa_list_hindi = backoff.xsampa_list('हिन्दी') print(f"Transliterated Hindi (X-SAMPA list): {xsampa_list_hindi}") ``` ### Response Example ``` Transliterated Hindi: ɦindiː Transliterated English: ɪŋɡlɪʃ Transliterated Chinese: ʈ͡ʂoŋwən Transliterated Hindi (list): ['ɦ', 'i', 'n', 'd', 'iː'] Transliterated Hindi (X-SAMPA list): ['h\', 'i', 'n', 'd', 'i:'] ``` ``` -------------------------------- ### Get word to tuples for English Source: https://context7.com/dmort27/epitran/llms.txt This snippet demonstrates how to convert an English word into a list of tuples, where each tuple contains linguistic information about the word. ```python import epitran tuples = epi.word_to_tuples('hello') for cat, case, orth, phon, vecs in tuples: print(f"{orth} -> {phon}") ``` -------------------------------- ### Epitran Class Initialization Source: https://github.com/dmort27/epitran/blob/master/README.md Demonstrates how to initialize the Epitran class with different language codes and optional parameters. ```APIDOC ## Epitran Class Initialization ### Description Initializes an Epitran object for a specified language and script, with options for pre-processors, post-processors, ligatures, and dictionary files. ### Method `Epitran(code, preproc=True, postproc=True, ligatures=False, cedict_file=None, tones=False)` ### Parameters - **code** (string) - Required - The ISO 639-3 code of the language plus a hyphen plus a four-letter code for the script (e.g., 'uig-Arab', 'cmn-Hans'). - **preproc** (boolean) - Optional - Enables pre-processors. Defaults to True. - **postproc** (boolean) - Optional - Enables post-processors. Defaults to True. - **ligatures** (boolean) - Optional - Enables non-standard IPA ligatures. Defaults to False. - **cedict_file** (string) - Optional - Path to the CC-CEDict dictionary file. Relevant for Mandarin Chinese. - **tones** (boolean) - Optional - Allows IPA tones to be included. Defaults to False. ### Request Example ```python import epitran # Initialize for Uyghur in Perso-Arabic script epi_uyghur = epitran.Epitran('uig-Arab') # Initialize for Mandarin Chinese with CC-CEDict epi_mandarin = epitran.Epitran('cmn-Hans', cedict_file='cedict_1_0_ts_utf-8_mdbg.txt') ``` ``` -------------------------------- ### Initialize and Use Backoff Class Source: https://github.com/dmort27/epitran/blob/master/README.md Instantiate the Backoff class with a list of language-script codes and optionally a CEDICT file. Use the transliterate method to convert text to IPA phonemes, falling back to subsequent languages if the primary one fails. ```python from epitran.backoff import Backoff >>> backoff = Backoff(['hin-Deva', 'eng-Latn', 'cmn-Hans'], cedict_file=‘cedict_1_0_ts_utf-8_mdbg.txt') >>> backoff.transliterate('हिन्दी') 'ɦindiː' >>> backoff.transliterate('English') 'ɪŋɡlɪʃ' >>> backoff.transliterate('中文') 'ʈ͡ʂoŋwən' ``` -------------------------------- ### Initialize and Use DictFirst Class Source: https://github.com/dmort27/epitran/blob/master/README.md Instantiate the DictFirst class with language-script codes for two languages and a dictionary file path. Use the transliterate method, which returns the Language A transliteration if the token is in the dictionary, otherwise returns the Language B transliteration. ```python >>> import dictfirst >>> df = dictfirst.DictFirst('tpi-Latn', 'eng-Latn', '../sample-dict.txt') >>> df.transliterate('pela') 'pela' >>> df.transliterate('pelo') 'pɛlow' ``` -------------------------------- ### Initialize Epitran for Mandarin Chinese (Simplified) Source: https://github.com/dmort27/epitran/blob/master/README.md Instantiate the Epitran class for Mandarin Chinese (Simplified Han) and specify the path to the CC-CEDict file. This is necessary for accurate transliteration of Chinese languages. ```python import epitran epi = epitran.Epitran('cmn-Hans', cedict_file='cedict_1_0_ts_utf-8_mdbg.txt') ``` -------------------------------- ### Transliteration with Custom Delimiter in Epitran Source: https://context7.com/dmort27/epitran/llms.txt Use trans_delimiter() to get IPA output with a specified delimiter between phoneme segments. This is useful for formatted output or integration with other tools. ```python import epitran epi = epitran.Epitran('tur-Latn') # Space-delimited phonemes (default) print(epi.trans_delimiter('Merhaba')) # Output: m e ɾ h a b a # Custom delimiter print(epi.trans_delimiter('Merhaba', delimiter='-')) # Output: m-e-ɾ-h-a-b-a # Tab-delimited for TSV output print(epi.trans_delimiter('Merhaba', delimiter='\t')) # Output: m e ɾ h a b a # Pipe-delimited epi_spa = epitran.Epitran('spa-Latn') print(epi_spa.trans_delimiter('queso', delimiter='|')) # Output: k|e|s|o ``` -------------------------------- ### VectorsWithIPASpace Class and word_to_segs Method Source: https://github.com/dmort27/epitran/blob/master/README.md Demonstrates the instantiation of VectorsWithIPASpace and the usage of its word_to_segs method for phonetic segmentation. ```APIDOC ## Using the ```epitran.vector``` Module The ```epitran.vector``` module provides the ```VectorsWithIPASpace``` class for phonetic segmentation. ### Constructor `VectorsWithIPASpace(code, spaces)` - `code` (string): The language-script code. - `spaces` (list of strings): Codes for the punctuation/symbol/IPA space. ### Method `VectorsWithIPASpace.word_to_segs(word, normpunc=False)` - `word` (string): The input Unicode string. - `normpunc` (boolean, optional): If True, normalizes punctuation to ASCII equivalents. ### Request Example ```python import epitran.vector vwis = epitran.vector.VectorsWithIPASpace('uzb-Latn', ['uzb-Latn']) result = vwis.word_to_segs('darë') print(result) ``` ### Response Structure Each element in the returned list is a tuple with the following structure: `(character_category, is_upper, orthographic_form, phonetic_form, in_ipa_punc_space, phonological_feature_vector)` - `character_category` (string): Unicode character category (e.g., 'L', 'N', 'P', 'Z'). - `is_upper` (integer): 0 for lowercase, 1 for uppercase. - `orthographic_form` (string): The original orthographic form of the character/segment. - `phonetic_form` (string): The phonetic representation (IPA). - `in_ipa_punc_space` (integer): Index in the known IPA space, -1 if unknown. - `phonological_feature_vector` (list of integers): A list of phonological features (-1, 0, or 1). ``` -------------------------------- ### Epitran Constructor Options Source: https://context7.com/dmort27/epitran/llms.txt Customize Epitran's behavior using constructor parameters like `preproc`, `postproc`, `ligatures`, `tones`, `cedict_file`, and `rev`. These options control preprocessing, postprocessing, ligature handling, tone marks, dictionary usage for Chinese/Japanese, and reverse transliteration. ```python import epitran # Basic instantiation with language-script code epi = epitran.Epitran('tur-Latn') # Disable preprocessor (use raw orthography-to-phoneme mapping) epi_no_preproc = epitran.Epitran('fra-Latn', preproc=False) # Disable postprocessor epi_no_postproc = epitran.Epitran('deu-Latn', postproc=False) # Enable non-standard IPA ligatures (e.g., ʤ instead of d͡ʒ) epi_ligatures = epitran.Epitran('eng-Latn', ligatures=True) # Enable tone marks for tonal languages like Vietnamese epi_vietnamese = epitran.Epitran('vie-Latn', tones=True) print(epi_vietnamese.transliterate('Việt Nam')) # Output includes tone markers: ˩˨˧˦˥ # Mandarin Chinese with CC-CEDict dictionary epi_chinese = epitran.Epitran('cmn-Hans', cedict_file='cedict_1_0_ts_utf-8_mdbg.txt') print(epi_chinese.transliterate('中文')) # Output: ʈ͡ʂoŋwən # Traditional Chinese epi_trad_chinese = epitran.Epitran('cmn-Hant', cedict_file='cedict_1_0_ts_utf-8_mdbg.txt') # Enable reverse transliteration (IPA to orthography) epi_reverse = epitran.Epitran('tur-Latn', rev=True) original = epi_reverse.reverse_transliterate('haziɾan') # Output: haziran ``` -------------------------------- ### Preprocessors and Postprocessors Source: https://github.com/dmort27/epitran/blob/master/README.md Explains the role of preprocessors and postprocessors in the Epitran library for handling orthography-to-phoneme mapping, especially for languages with complex sound-symbol correspondences like French and English. It highlights potential pitfalls regarding input word consistency. ```APIDOC ## Preprocessors, Postprocessors, and Pitfalls ### Description Preprocessors and postprocessors are employed in Epitran to build maintainable orthography-to-phoneme mappers. Preprocessors perform contextual substitutions before text is passed to the mapping system, which is particularly useful for languages with poor sound-symbol correspondence (e.g., French, English). ### Advantages - Enables straightforward grapheme-to-phoneme mappings. - The restricted regular expression language for preprocessing rules is more powerful than mapping rules. - Makes supporting languages like French and German practical. ### Pitfalls - When using a language with a preprocessor, the input word may not be identical to the concatenation of orthographic strings output by `Epitran.word_to_tuples`. - The output of `word_to_tuple` reflects the preprocessor's output, which might involve deleting, inserting, or changing letters. - This also affects other methods relying on `Epitran.word_to_tuple`, such as `VectorsWithIPASpace.word_to_segs`. ### Further Information For details on writing new pre- and post-processors, refer to the section on "Extending Epitran with map files, preprocessors and postprocessors". ``` -------------------------------- ### Supported Language Codes Initialization Source: https://context7.com/dmort27/epitran/llms.txt Demonstrates initializing Epitran for various common European, South Asian, East Asian, Middle Eastern, and African languages using their respective ISO codes and script identifiers. ```python import epitran # Common European languages epi_fra = epitran.Epitran('fra-Latn') # French epi_deu = epitran.Epitran('deu-Latn') # German epi_ita = epitran.Epitran('ita-Latn') # Italian epi_por = epitran.Epitran('por-Latn') # Portuguese epi_rus = epitran.Epitran('rus-Cyrl') # Russian (Cyrillic) epi_pol = epitran.Epitran('pol-Latn') # Polish epi_nld = epitran.Epitran('nld-Latn') # Dutch # South Asian languages epi_hin = epitran.Epitran('hin-Deva') # Hindi (Devanagari) epi_ben = epitran.Epitran('ben-Beng') # Bengali epi_tam = epitran.Epitran('tam-Taml') # Tamil epi_tel = epitran.Epitran('tel-Telu') # Telugu epi_mal = epitran.Epitran('mal-Mlym') # Malayalam epi_pan = epitran.Epitran('pan-Guru') # Punjabi (Gurmukhi) epi_urd = epitran.Epitran('urd-Arab') # Urdu (Arabic script) # East Asian languages epi_kor = epitran.Epitran('kor-Hang') # Korean (Hangul) epi_vie = epitran.Epitran('vie-Latn') # Vietnamese epi_tha = epitran.Epitran('tha-Thai') # Thai epi_mya = epitran.Epitran('mya-Mymr') # Burmese # Middle Eastern languages epi_ara = epitran.Epitran('ara-Arab') # Arabic epi_fas = epitran.Epitran('fas-Arab') # Farsi/Persian epi_tur = epitran.Epitran('tur-Latn') # Turkish epi_heb = epitran.Epitran('heb-Hebr') # Hebrew # African languages epi_amh = epitran.Epitran('amh-Ethi') # Amharic (Ethiopic) epi_swa = epitran.Epitran('swa-Latn') # Swahili epi_yor = epitran.Epitran('yor-Latn') # Yoruba epi_hau = epitran.Epitran('hau-Latn') # Hausa ``` -------------------------------- ### Initialize Epitran for Uyghur (Arabic Script) Source: https://github.com/dmort27/epitran/blob/master/README.md Instantiate the Epitran class for Uyghur language using the Arabic script. Ensure the correct ISO 639-3 and script codes are used. ```python import epitran epi = epitran.Epitran('uig-Arab') # Uyghur in Perso-Arabic script ``` -------------------------------- ### Japanese G2P (Reduced Variants) Source: https://context7.com/dmort27/epitran/llms.txt Initializes Epitran with reduced phoneme inventories for Japanese Hiragana and Katakana. These variants offer simplified phonological representations. ```python import epitran # Reduced variants (simplified phoneme inventories) epi_hira_red = epitran.Epitran('jpn-Hira-red') epi_kana_red = epitran.Epitran('jpn-Kana-red') ``` -------------------------------- ### DictFirst Class Usage Source: https://github.com/dmort27/epitran/blob/master/README.md Illustrates the usage of the DictFirst class, an alternative to Backoff, which uses a provided dictionary to determine the transliteration language for input tokens. ```APIDOC ## DictFirst Class ### Description The `DictFirst` class offers an alternative to `Backoff`. It prioritizes transliteration based on a provided dictionary of known words for a primary language. ### Initialization `DictFirst(lang_a_code, lang_b_code, dictionary_path)` - `lang_a_code` (str): The language-script code for the primary language (e.g., 'tpi-Latn'). - `lang_b_code` (str): The language-script code for the fallback language (e.g., 'eng-Latn'). - `dictionary_path` (str): The path to a UTF-8 encoded text file containing words of Language A, one word per line. ### Public Methods - **transliterate**: Transliterates an input token. If the token is found in the dictionary, it returns the Language A transliteration; otherwise, it returns the Language B transliteration. ### Request Example ```python import dictfirst df = dictfirst.DictFirst('tpi-Latn', 'eng-Latn', '../sample-dict.txt') # Token found in dictionary (assuming 'pela' is in sample-dict.txt) result1 = df.transliterate('pela') print(f"Transliteration of 'pela': {result1}") # Token not found in dictionary (assuming 'pelo' is not in sample-dict.txt) result2 = df.transliterate('pelo') print(f"Transliteration of 'pelo': {result2}") ``` ### Response Example ``` Transliteration of 'pela': pela Transliteration of 'pelo': pelo ``` ``` -------------------------------- ### Clone Epitran Repository Source: https://github.com/dmort27/epitran/blob/master/README.md Clone the latest source code for Epitran from GitHub. This is the recommended first step for obtaining the software. ```bash git clone https://github.com/festvox/flite.git cd flite ``` -------------------------------- ### Transliterate Method Source: https://github.com/dmort27/epitran/blob/master/README.md Converts orthographic text to IPA using the initialized Epitran object. ```APIDOC ## Transliterate Method ### Description Converts a given text string from the language's orthography to IPA. ### Method `Epitran.transliterate(text, normpunc=False, ligatures=False)` ### Parameters - **text** (string) - Required - The Unicode-encoded orthographic text to transliterate. - **normpunc** (boolean) - Optional - Enables punctuation normalization. Defaults to False. - **ligatures** (boolean) - Optional - Enables non-standard IPA ligatures. Defaults to False. ### Request Example ```python # Assuming 'epi' is an initialized Epitran object ipa_transcription = epi.transliterate('Düğün') print(ipa_transcription) ``` ### Response #### Success Response (200) - **ipa_transcription** (string) - The IPA representation of the input text. #### Response Example ``` dy\u0270yn ``` ``` -------------------------------- ### Backoff Class IPA and X-SAMPA Conversion Source: https://github.com/dmort27/epitran/blob/master/README.md Demonstrates using the Backoff class to convert Hindi text to IPA phonemes using `transliterate` and `trans_list`, and to X-SAMPA using `xsampa_list`. ```python >>> backoff.transliterate('हिन्दी') 'ɦindiː' >>> backoff.trans_list('हिन्दी') ['ɦ', 'i', 'n', 'd', 'iː'] >>> backoff.xsampa_list('हिन्दी') ['h\', 'i', 'n', 'd', 'i:'] ``` -------------------------------- ### Basic Context-Sensitive Rewrite Rule Source: https://github.com/dmort27/epitran/blob/master/README.md Illustrates a basic context-sensitive rewrite rule format: 'a -> b / X _ Y'. This means 'a' is rewritten as 'b' when it appears between 'X' and 'Y'. ```epitran a -> b / X _ Y ``` -------------------------------- ### Word to Tuples Method Source: https://github.com/dmort27/epitran/blob/master/README.md Converts a word into a list of tuples, where each tuple represents an IPA segment with detailed phonetic information. ```APIDOC ## Word to Tuples Method ### Description Converts a word into a list of tuples, providing detailed information about each IPA segment. ### Method `Epitran.word_to_tuples(word, normpunc=False)` ### Parameters - **word** (string) - Required - The Unicode string in a supported orthography. - **normpunc** (boolean) - Optional - Enables punctuation normalization. Defaults to False. ### Response #### Success Response (200) - **tuples** (list) - A list of tuples, where each tuple represents an IPA segment with the structure: ``` ( character_category :: String, is_upper :: Integer, orthographic_form :: Unicode String, phonetic_form :: Unicode String, segments :: List ) ``` The structure of the inner `segments` tuple is: ``` ( segment :: Unicode String, vector :: List ) ``` ### Request Example ```python import epitran epi = epitran.Epitran('tur-Latn') word_data = epi.word_to_tuples('Düğün') print(word_data) ``` ### Response Example ```json [ ["L", 1, "D", "d", [["d", [-1, -1, 1, -1, -1, -1, -1, -1, 1, -1, -1, 1, 1, -1, -1, -1, -1, -1, -1, 0, -1]]]], ["L", 0, "ü", "y", [["y", [1, 1, -1, 1, -1, -1, -1, 0, 1, -1, -1, -1, -1, -1, 1, 1, -1, -1, 1, 1, -1]]]], ["L", 0, "ğ", "ɰ", [["ɰ", [-1, 1, -1, 1, 0, -1, -1, 0, 1, -1, -1, 0, -1, 0, -1, 1, -1, 0, -1, 1, -1]]]], ["L", 0, "ü", "y", [["y", [1, 1, -1, 1, -1, -1, -1, 0, 1, -1, -1, -1, -1, -1, 1, 1, -1, -1, 1, 1, -1]]]], ["L", 0, "n", "n", [["n", [-1, 1, 1, -1, -1, -1, 1, -1, 1, -1, -1, 1, 1, -1, -1, -1, -1, -1, -1, 0, -1]]]]) ] ``` ``` -------------------------------- ### Chinese G2P with Tone Markers Source: https://context7.com/dmort27/epitran/llms.txt Transliterates Simplified Chinese text to IPA, including tone markers, using the CC-CEDict dictionary. Ensure the dictionary file is downloaded and accessible. ```python import epitran # Download CC-CEDict from: https://cc-cedict.org/ # File: cedict_1_0_ts_utf-8_mdbg.txt # With tone markers epi_tones = epitran.Epitran('cmn-Hans', cedict_file='cedict_1_0_ts_utf-8_mdbg.txt', tones=True) print(epi_tones.transliterate('你好')) # Output includes Chao tone numbers ``` -------------------------------- ### DictFirst Class: Dictionary-Based Fallback Transliteration Source: https://context7.com/dmort27/epitran/llms.txt The DictFirst class handles transliteration with a primary language dictionary and a fallback language. Words in the dictionary use the primary language's rules; others use the fallback. Requires a dictionary file. ```python from epitran.dictfirst import DictFirst # First language: Tok Pisin, Fallback: English # Words in dictionary use Tok Pisin rules, others use English df = DictFirst('tpi-Latn', 'eng-Latn', 'tok-pisin-dictionary.txt') # Word in dictionary - uses Tok Pisin transliteration print(df.transliterate('pela')) # Output: pela (Tok Pisin pronunciation) # Word not in dictionary - falls back to English print(df.transliterate('pelo')) # Output: pɛlow (English pronunciation) # Example dictionary file format (one word per line): # pela # bilong # mipela # yupela ``` -------------------------------- ### Chinese G2P (Mandarin Simplified) Source: https://context7.com/dmort27/epitran/llms.txt Transliterates Simplified Chinese text to IPA using the CC-CEDict dictionary. Ensure the dictionary file is downloaded and accessible. ```python import epitran # Download CC-CEDict from: https://cc-cedict.org/ # File: cedict_1_0_ts_utf-8_mdbg.txt # Simplified Chinese epi_hans = epitran.Epitran('cmn-Hans', cedict_file='cedict_1_0_ts_utf-8_mdbg.txt') print(epi_hans.transliterate('你好')) # Output: nihaʊ print(epi_hans.transliterate('中国')) # Output: ʈ͡ʂoŋkuo print(epi_hans.transliterate('北京')) # Output: peɪt͡ɕiŋ ``` -------------------------------- ### Epitran Basic Usage Source: https://github.com/dmort27/epitran/blob/master/README.md Instantiate the Epitran class with the desired language code and use the transliterate method to convert orthography to phonetics. Ensure the correct language code, such as 'eng-Latn' for English, is used. ```python import epitran epi = epitran.Epitran('eng-Latn') print epi.transliterate(u'Berkeley') ``` -------------------------------- ### Transform Initial Consonants in Epitran Source: https://github.com/dmort27/epitran/blob/master/epitran/data/rules/jyutping-to-ipa-tones.txt Rules for transforming various initial consonants in Cantonese Jyutping to their IPA representations. Each initial is mapped to a transformed IPA part prefixed and suffixed with 'x'. ```epitran f -> xfx / # _ ``` ```epitran h -> xhx / # _ ``` ```epitran j -> xjx / # _ ``` ```epitran gw -> xkʷx / # _ ``` ```epitran kw -> xkʷʰx / # _ ``` ```epitran g -> xkx / # _ ``` ```epitran k -> xkʰx / # _ ``` ```epitran l -> xlx / # _ ``` ```epitran m -> xmx / # _ ``` ```epitran n -> xnx / # _ ``` ```epitran ng -> xŋx / # _ ``` ```epitran b -> xpx / # _ ``` ```epitran p -> xpʰx / # _ ``` ```epitran s -> xsx / # _ ``` ```epitran d -> xtx / # _ ``` ```epitran t -> xtʰx / # _ ``` ```epitran z -> xtsx / # _ ``` ```epitran c -> xtsʰx / # _ ``` ```epitran w -> xwx / # _ ``` -------------------------------- ### Define Initial Consonants for Epitran Source: https://github.com/dmort27/epitran/blob/master/epitran/data/rules/jyutping-to-ipa-tones.txt Defines the set of initial consonants recognized by Epitran for Cantonese. These are used in the transformation rules. ```epitran ::init:: = f|h|j|g|k|gw|kw|l|m|n|ng|b|p|s|d|t|z|c|w ``` -------------------------------- ### Convert word to IPA segments with epitran.vector Source: https://github.com/dmort27/epitran/blob/master/README.md Use VectorsWithIPASpace to convert a word into a list of IPA segments. The constructor requires a language-script code and a list of space codes. The word_to_segs method returns a list of tuples, each representing a segment with detailed linguistic information. ```python >>> import epitran.vector >>> vwis = epitran.vector.VectorsWithIPASpace('uzb-Latn', ['uzb-Latn']) >>> vwis.word_to_segs('darë') ``` -------------------------------- ### Chinese G2P (Mandarin Traditional) Source: https://context7.com/dmort27/epitran/llms.txt Transliterates Traditional Chinese text to IPA using the CC-CEDict dictionary. Ensure the dictionary file is downloaded and accessible. ```python import epitran # Download CC-CEDict from: https://cc-cedict.org/ # File: cedict_1_0_ts_utf-8_mdbg.txt # Traditional Chinese epi_hant = epitran.Epitran('cmn-Hant', cedict_file='cedict_1_0_ts_utf-8_mdbg.txt') print(epi_hant.transliterate('臺灣')) # Output: tʰaɪwan ``` -------------------------------- ### Cantonese G2P with Tones Source: https://context7.com/dmort27/epitran/llms.txt Transliterates Traditional Chinese characters to IPA via Jyutping romanization, including tone markers, using the CC-Canto dictionary. Ensure the dictionary file is downloaded. ```python import epitran # Download CC-Canto from: https://cccanto.org/ # File: cccanto-170202.txt # With tones epi_yue_tones = epitran.Epitran('yue-Hant', cedict_file='cccanto-170202.txt', tones=True) print(epi_yue_tones.transliterate('你好')) # Output includes tone markers ``` -------------------------------- ### Detailed Segment Analysis with word_to_tuples() Source: https://context7.com/dmort27/epitran/llms.txt The word_to_tuples() method provides detailed segment information including category, case, orthography, phonetics, and feature vectors. It's essential for linguistic analysis and machine learning. ```python import epitran epi = epitran.Epitran('tur-Latn') # Get detailed segment information tuples = epi.word_to_tuples('Haziran') for segment in tuples: category, is_upper, orth, phon, features = segment print(f"Category: {category}, Upper: {is_upper}, Orth: '{orth}', Phon: '{phon}'") # Output: # Category: L, Upper: 1, Orth: 'H', Phon: 'h' # Category: L, Upper: 0, Orth: 'a', Phon: 'a' # Category: L, Upper: 0, Orth: 'z', Phon: 'z' # Category: L, Upper: 0, Orth: 'i', Phon: 'i' # Category: L, Upper: 0, Orth: 'r', Phon: 'ɾ' # Category: L, Upper: 0, Orth: 'a', Phon: 'a' # Category: L, Upper: 0, Orth: 'n', Phon: 'n' # Access phonological feature vectors tuples = epi.word_to_tuples('Düğün') for cat, case, orth, phon, feat_vecs in tuples: for segment, vector in feat_vecs: print(f"Segment: {segment}, Features: {vector[:5]}...") # First 5 features # Output shows binary feature vectors (-1, 0, 1) for each segment # Segment: d, Features: [-1, -1, 1, -1, -1]... # Segment: y, Features: [1, 1, -1, 1, -1]... # With punctuation normalization tuples_norm = epi.word_to_tuples('Haziran\'da', normpunc=True) # Punctuation characters get category 'P' instead of 'L' ``` -------------------------------- ### Define Initial Nasals Source: https://github.com/dmort27/epitran/blob/master/epitran/data/post/kor-Hang.txt Specifies the nasal consonants that can appear at the beginning of a syllable. ```regex ::initialNasals:: = n|m ``` -------------------------------- ### Japanese G2P (Mixed Script) Source: https://context7.com/dmort27/epitran/llms.txt Transliterates mixed Hiragana, Katakana, and Kanji Japanese text to IPA. The system automatically downloads the necessary dictionary for Kanji processing. ```python import epitran # Mixed Hiragana, Katakana, and Kanji (auto-downloads dictionary) epi_jpan = epitran.Epitran('jpn-Jpan') print(epi_jpan.transliterate('東京')) # Output: toːkʲoː print(epi_jpan.transliterate('日本語')) # Output: nihoŋɡo ``` -------------------------------- ### X-SAMPA Output with Epitran Source: https://context7.com/dmort27/epitran/llms.txt The xsampa_list() method converts text to X-SAMPA, an ASCII-compatible phonetic notation, which is useful for systems that cannot handle Unicode IPA characters. ```python import epitran epi = epitran.Epitran('hin-Deva') # Get X-SAMPA representation xsampa = epi.xsampa_list('हिन्दी') print(xsampa) # Output: ['h\\', 'i', 'n', 'd', 'i:'] # Turkish example epi_tur = epitran.Epitran('tur-Latn') xsampa = epi_tur.xsampa_list('Düğün') print(xsampa) # Output: ['d', 'y', 'M\\', 'y', 'n'] # Spanish example epi_spa = epitran.Epitran('spa-Latn') xsampa = epi_spa.xsampa_list('general') print(xsampa) # Output: ['x', 'e', 'n', 'e', '4', 'a', 'l'] ``` -------------------------------- ### ReRomanizer: IPA to Readable Romanization Source: https://context7.com/dmort27/epitran/llms.txt Converts orthographic text to IPA via Epitran, then maps IPA segments to a readable romanized form using custom tables. Useful for creating human-readable transcriptions. Can also romanize IPA segments directly. ```python from epitran import ReRomanizer # Create reromanizer for Amharic using a romanization table rerom = ReRomanizer('amh-Ethi', 'amh-Ethi') # Convert Amharic text to romanized form romanized = rerom.reromanize('አማርኛ') print(romanized) # Output: amarəña (readable romanization) # The process: # 1. Text is first converted to IPA via Epitran # 2. IPA segments are then mapped to romanized equivalents # 3. Result is a more readable representation # You can also romanize IPA segments directly ipa_segments = ['a', 'm', 'a', 'r', 'ə', 'ɲ', 'a'] romanized_segs = rerom.reromanize_ipa(ipa_segments) print(romanized_segs) # Output: ['a', 'm', 'a', 'r', 'ə', 'ñ', 'a'] ``` -------------------------------- ### Context-Free Rewrite Rule Source: https://github.com/dmort27/epitran/blob/master/README.md A rule that applies in a context-free manner, rewriting 'ch' as 'x'. The context is empty, indicated by '_' at the end. ```epitran ch -> x / _ ``` -------------------------------- ### Handle Consonants Preceding Syllabic Consonants Source: https://github.com/dmort27/epitran/blob/master/epitran/data/pre/nan-Latn.txt Applies the 'Bng' transformation to 'ng' when it is preceded by a consonant defined in '::consonant::'. This rule accounts for both cases with and without an immediately following tone. ```Epitran ng -> Bng / (::consonant::) _ # ``` ```Epitran ng -> Bng / (::consonant::) _ ([́̀̂̌̍̄̋]) # ``` -------------------------------- ### Assimilation and Coalescence Rules for Sibilants and Affricates Source: https://github.com/dmort27/epitran/blob/master/epitran/data/post/slv-Latn.txt Handles assimilation or coalescence of /s, z, ts/ when followed by postalveolar sounds. Includes rules for removing duplicates after assimilation. ```epitran ::postalveolar:: = ʃ|ʒ|tʃ|dʒ s -> ʃ / _ (::postalveolar::) z -> ʒ / _ (::postalveolar::) ts -> tʃ / _ (::postalveolar::) % remove duplicates after assimilation ʃ -> 0 / _ ʃ ʒ -> 0 / _ ʒ tʃ -> 0 / _ tʃ ``` -------------------------------- ### Japanese G2P (Katakana) Source: https://context7.com/dmort27/epitran/llms.txt Transliterates Katakana text to IPA. This configuration is suitable for Japanese text written exclusively in Katakana. ```python import epitran # Katakana only epi_kana = epitran.Epitran('jpn-Kana') print(epi_kana.transliterate('カタカナ')) # Output: katakana print(epi_kana.transliterate('コンピューター')) # Output: kompʲuːtaː ``` -------------------------------- ### Japanese G2P (Hiragana) Source: https://context7.com/dmort27/epitran/llms.txt Transliterates Hiragana text to IPA. This configuration is suitable for Japanese text written exclusively in Hiragana. ```python import epitran # Hiragana only epi_hira = epitran.Epitran('jpn-Hira') print(epi_hira.transliterate('とうきょう')) # Output: toːkʲoː print(epi_hira.transliterate('ひらがな')) # Output: hiɾaɡana ``` -------------------------------- ### Define Initial Consonants in Korean Source: https://github.com/dmort27/epitran/blob/master/epitran/data/post/kor-Hang.txt Defines the set of initial consonants in Korean, excluding 'ㅇ' (ng) as it has no sound value in this position. This is based on Article #2 of the Korean pronunciation rulebook. ```regex ::initialconsonants:: = k|k͈|n|t|t͈|ɾ|m|p|p͈|s|s͈|t͡ɕ|t͈͡ɕ|t͡ɕʰ|kʰ|tʰ|pʰ|h ```