Python
Lipi Lekhika offers a powerful Python package with full type hints and IDE support. The package uses Rust-compiled functions for blazing-fast transliteration operations, providing high performance while maintaining Python’s ease of use.
Installation
Section titled “Installation”pip install lipilekhikauv add lipilekhikapoetry add lipilekhikaRequirements: Python 3.10 or higher
For transliteration, use the transliterate function. It returns a string directly (synchronous).
from lipilekhika import transliterate
result = transliterate('na jAyatE mriyatE vA', 'Normal', 'Devanagari')print(result) # न जायते म्रियते वा- The Script/Language names are fully typed with
Literaltypes, so you will get IDE autocompletion and type checking. - You can also pass custom transliteration options to
transliterateas the fourth argument.
Custom Transliteration Options
Section titled “Custom Transliteration Options”from lipilekhika import transliterate
result = transliterate( 'गङ्गा', 'Devanagari', 'Gujarati', {'brahmic_to_brahmic:replace_pancham_varga_varna_with_anusvAra': True})print(result) # ગંગા (instead of ગઙ્ગા)Preloading Script Data
Section titled “Preloading Script Data”For better performance in applications where you know which scripts you’ll use:
from lipilekhika import preload_script_data, transliterate
# Preload script data to avoid initial loading delaypreload_script_data('Telugu')preload_script_data('Devanagari')
# Now transliteration will be instantresult = transliterate('namaskAramu', 'Normal', 'Telugu')Getting Available Options
Section titled “Getting Available Options”from lipilekhika import get_all_options
# Get all available custom options for a script pairoptions = get_all_options('Normal', 'Devanagari')print(options)# ['normal_to_brahmic:use_alternative_mAtrA_form', ...]Core API Reference
Section titled “Core API Reference”Functions
Section titled “Functions”transliterate(text, from_script, to_script, options=None)
Section titled “transliterate(text, from_script, to_script, options=None)”Transliterates text from one script/language to another.
Parameters:
text: str— The text to transliteratefrom_script: ScriptLangType— The source script/languageto_script: ScriptLangType— The target script/languageoptions: dict[str, bool] | None— Optional custom transliteration options
Returns: str — The transliterated text
Raises: Exception — If an invalid script name is provided
Example:
result = transliterate('hello', 'Normal', 'Telugu')print(result) # హెల్లొpreload_script_data(script_name)
Section titled “preload_script_data(script_name)”Preloads the script data for the given script/language. This is useful for avoiding initial loading delay in applications.
Parameters:
script_name: ScriptLangType— The name of the script/language to preload
Raises: Exception — If an invalid script name is provided
Example:
preload_script_data('Devanagari')get_all_options(from_script, to_script)
Section titled “get_all_options(from_script, to_script)”Returns the list of all supported custom options for transliterations between the provided script pair.
Parameters:
from_script: ScriptLangType— The script/language to transliterate fromto_script: ScriptLangType— The script/language to transliterate to
Returns: list[str] — List of all supported custom option keys
Raises: Exception — If an invalid script name is provided
Example:
options = get_all_options('Normal', 'Devanagari')for option in options: print(option)get_normalized_script_name(script_name)
Section titled “get_normalized_script_name(script_name)”Get the normalized script name for the given script/language. This function maps language names to their corresponding script names.
Parameters:
script_name: ScriptLangType— The script/language name to normalize
Returns: str — The normalized script name
Raises: Exception — If an invalid script name is provided
Example:
from lipilekhika import get_normalized_script_name
script = get_normalized_script_name('Hindi')print(script) # 'Devanagari'get_schwa_status_for_script(script_name)
Section titled “get_schwa_status_for_script(script_name)”Returns the schwa deletion characteristic of the script. This is the property in which an inherent vowel ‘a’ (अ) is added to the end of vyanjana (consonant) characters.
Parameters:
script_name: ScriptLangType— The script/language name to check
Returns: bool | None
Trueif the script has schwa deletionFalseif it doesn’tNoneif the script is not a Brahmic script
Raises: Exception — If an invalid script name is provided
Example:
from lipilekhika import get_schwa_status_for_script
has_schwa = get_schwa_status_for_script('Devanagari')print(has_schwa) # TrueConstants & Types
Section titled “Constants & Types”The package exports the following constants:
from lipilekhika import SCRIPT_LIST, LANG_LIST, ALL_SCRIPT_LANG_LIST
print(SCRIPT_LIST) # ['Devanagari', 'Bengali', 'Telugu', ...]print(LANG_LIST) # ['Sanskrit', 'Hindi', 'Marathi', ...]print(ALL_SCRIPT_LANG_LIST) # Combined list| Export | Type | Description |
|---|---|---|
SCRIPT_LIST |
list[str] |
List of all supported script names |
LANG_LIST |
list[str] |
List of all supported language names mapped to scripts |
ALL_SCRIPT_LANG_LIST |
list[str] |
Combined list of all scripts and languages |
Type Hints:
The package provides comprehensive type hints for better IDE support:
ScriptLangType— Literal type for script/language identifiers (includes aliases)TransliterationOptionsType— Type for custom transliteration option keysScriptListType— Type for script namesLangListType— Type for language names
Real-time Typing
Section titled “Real-time Typing”Enable real-time transliteration as users type character by character. This is useful for building interactive text editors, chat applications, or any interface requiring real-time script input.
from lipilekhika.typing import create_typing_context
ctx = create_typing_context('Telugu')
# Process each character as the user typesfor char in "namaste": diff = ctx.take_key_input(char) # The diff tells you what to change in your text buffer: # - Remove the last diff.to_delete_chars_count characters # - Append diff.diff_add_text print(f"Delete {diff.to_delete_chars_count}, Add: {diff.diff_add_text}")Quick Example
Section titled “Quick Example”Here’s a complete example of emulating typing:
from lipilekhika.typing import create_typing_context
def emulate_typing(text: str, script: str) -> str: """Emulate typing character by character.""" ctx = create_typing_context(script) result = ""
for char in text: diff = ctx.take_key_input(char) # Apply the diff if diff.to_delete_chars_count > 0: result = result[:-diff.to_delete_chars_count] result += diff.diff_add_text
return result
# Usageoutput = emulate_typing("namaste", "Devanagari")print(output) # नमस्तेTyping API
Section titled “Typing API”create_typing_context(typing_lang, options=None)
Section titled “create_typing_context(typing_lang, options=None)”Creates a stateful isolated context for character-by-character input typing.
Parameters:
typing_lang: ScriptLangType— The script/language to type inoptions: TypingContextOptions | None— Optional configuration
Returns: TypingContext — A context object with the following methods:
clear_context()— Clears all internal states and contextstake_key_input(key: str) -> TypingDiff— Accepts character input and returns the diffupdate_use_native_numerals(value: bool)— Update native numerals settingupdate_include_inherent_vowel(value: bool)— Update inherent vowel settingget_use_native_numerals() -> bool— Get current native numerals settingget_include_inherent_vowel() -> bool— Get current inherent vowel setting
Raises: Exception — If an invalid script name is provided
Example:
from lipilekhika.typing import create_typing_context, TypingContextOptions
# Create with custom optionsoptions = TypingContextOptions( auto_context_clear_time_ms=4500, use_native_numerals=True, include_inherent_vowel=False)
ctx = create_typing_context('Devanagari', options)
# Process inputdiff = ctx.take_key_input('n')print(diff.diff_add_text) # 'न्'get_script_typing_data_map(script)
Section titled “get_script_typing_data_map(script)”Returns the typing data map for a script. This function is useful for comparing the krama array of two scripts or building typing helper UIs.
Parameters:
script: ScriptLangType— The script to get the typing data map for
Returns: ScriptTypingDataMap — Object containing:
common_krama_map— Mappings for common characters across scriptsscript_specific_krama_map— Mappings for script-specific characters
Each mapping is a tuple of (text, type, mappings) where:
text: str— The displayed character in the target scripttype: Literal["anya", "vyanjana", "matra", "svara"]— Character categorymappings: list[str]— List of input key sequences that produce this character
Raises: Exception — If an invalid script name is provided or if ‘Normal’ is used
Example:
from lipilekhika.typing import get_script_typing_data_map
typing_map = get_script_typing_data_map('Devanagari')
# Access common character mappingsfor text, char_type, mappings in typing_map.common_krama_map: print(f"{text} ({char_type}): {mappings}")
# Access script-specific mappingsfor text, char_type, mappings in typing_map.script_specific_krama_map: print(f"{text} ({char_type}): {mappings}")Typing Options
Section titled “Typing Options”TypingContextOptions
Section titled “TypingContextOptions”Configuration class for typing contexts.
Attributes:
auto_context_clear_time_ms: int— Time in ms after which context is cleared (default: 4500)use_native_numerals: bool— Whether to use native numerals (default: True)include_inherent_vowel: bool— Whether to include inherent vowels/schwa (default: False)
Example:
from lipilekhika.typing import TypingContextOptions, DEFAULT_AUTO_CONTEXT_CLEAR_TIME_MS
options = TypingContextOptions( auto_context_clear_time_ms=DEFAULT_AUTO_CONTEXT_CLEAR_TIME_MS, use_native_numerals=False, include_inherent_vowel=True)Typing Constants
Section titled “Typing Constants”from lipilekhika.typing import ( DEFAULT_AUTO_CONTEXT_CLEAR_TIME_MS, DEFAULT_USE_NATIVE_NUMERALS, DEFAULT_INCLUDE_INHERENT_VOWEL)
print(DEFAULT_AUTO_CONTEXT_CLEAR_TIME_MS) # 4500print(DEFAULT_USE_NATIVE_NUMERALS) # Trueprint(DEFAULT_INCLUDE_INHERENT_VOWEL) # FalsePerformance
Section titled “Performance”The Python package leverages Rust’s performance through PyO3 bindings:
- ⚡ Fast Transliteration — Core operations are compiled Rust code
- 🚀 Low Latency — Real-time typing with minimal overhead
- 📦 Efficient — Optimized memory usage and execution speed