Working with Nepali Text
This guide covers best practices for handling Nepali (Devanagari) text when using the Sabdasakha API.
Character Encoding
Always use UTF-8 encoding. All API requests and responses use UTF-8.
HTTP Headers
Include the proper content type for POST requests:
curl -X POST "https://sabdasakha.com/api/v1/spellcheck/text" \
-H "Content-Type: application/json; charset=utf-8" \
-d '{"text": "नेपाल"}'
File Encoding
When reading Nepali text from files:
URL Encoding
When passing Nepali text as URL parameters, it must be properly encoded.
Path Parameters
For word lookups, encode the word:
Tip
Most HTTP libraries (requests, axios, fetch) handle URL encoding automatically. You only need to manually encode when building URLs as strings.
Query Parameters
import requests
# requests handles encoding automatically
response = requests.get(
"https://sabdasakha.com/api/v1/dictionary/suggest",
params={"q": "नेपा"} # No manual encoding needed
)
Unicode Normalization
Nepali text can be represented in multiple Unicode forms. Normalize for consistent matching.
The Problem
The same-looking text can have different byte representations:
text1 = "कृ" # Single character: क + ृ
text2 = "कृ" # Two characters: क + ृ (combining)
# These might look identical but:
text1 == text2 # Could be False!
The Solution
Use NFC normalization:
Common Nepali Characters
Vowels (स्वर)
| Character | Name | Unicode |
|---|---|---|
| अ | a | U+0905 |
| आ | aa | U+0906 |
| इ | i | U+0907 |
| ई | ii | U+0908 |
| उ | u | U+0909 |
| ऊ | uu | U+090A |
| ए | e | U+090F |
| ऐ | ai | U+0910 |
| ओ | o | U+0913 |
| औ | au | U+0914 |
Common Consonants (व्यञ्जन)
| Character | Name | Unicode |
|---|---|---|
| क | ka | U+0915 |
| ख | kha | U+0916 |
| ग | ga | U+0917 |
| घ | gha | U+0918 |
| न | na | U+0928 |
| म | ma | U+092E |
| र | ra | U+0930 |
| ल | la | U+0932 |
| स | sa | U+0938 |
| ह | ha | U+0939 |
Special Marks
| Character | Name | Unicode | Purpose |
|---|---|---|---|
| ् | Halant | U+094D | Removes inherent vowel |
| ं | Anusvara | U+0902 | Nasal sound |
| ँ | Chandrabindu | U+0901 | Nasalization |
| ः | Visarga | U+0903 | Aspiration |
Text Cleaning
Before sending text to the API, clean it appropriately:
import re
def clean_nepali_text(text):
# Remove extra whitespace
text = " ".join(text.split())
# Remove numbers (optional, depends on use case)
# text = re.sub(r'[0-9०-९]', '', text)
# Keep only Devanagari characters and basic punctuation
# text = re.sub(r'[^\u0900-\u097F\s।,]', '', text)
return text.strip()
Handling Mixed Scripts
When text contains both Nepali and English:
def is_nepali_word(word):
"""Check if a word is primarily Nepali (Devanagari)."""
devanagari_chars = sum(1 for c in word if '\u0900' <= c <= '\u097F')
return devanagari_chars > len(word) / 2
def extract_nepali_words(text):
"""Extract only Nepali words from mixed text."""
words = text.split()
return [w for w in words if is_nepali_word(w)]
# Example
text = "Nepal नेपाल is beautiful सुन्दर"
nepali_words = extract_nepali_words(text)
# Result: ['नेपाल', 'सुन्दर']
Keyboard Input
Romanized Input
Many users type using Roman keyboards. Consider supporting transliteration:
| Roman | Nepali |
|---|---|
| nepal | नेपाल |
| namaste | नमस्ते |
| kitab | किताब |
The Sabdasakha web app includes built-in transliteration. For API users, consider integrating a transliteration library.
Virtual Keyboard
For web applications, consider providing a Nepali virtual keyboard for users without Nepali keyboard layouts.
Debugging Tips
Check Character Codes
When text doesn't match as expected:
def debug_string(text):
for char in text:
print(f"'{char}' = U+{ord(char):04X}")
debug_string("नेपाल")
# Output:
# 'न' = U+0928
# 'े' = U+0947
# 'प' = U+092A
# 'ा' = U+093E
# 'ल' = U+0932