Skip to content

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:

# Always specify UTF-8 encoding
with open("nepali_text.txt", "r", encoding="utf-8") as f:
    text = f.read()
const fs = require("fs");

const text = fs.readFileSync("nepali_text.txt", "utf-8");

URL Encoding

When passing Nepali text as URL parameters, it must be properly encoded.

Path Parameters

For word lookups, encode the word:

import urllib.parse

word = "नेपाल"
encoded_word = urllib.parse.quote(word)
url = f"https://sabdasakha.com/api/v1/dictionary/word/{encoded_word}"
# Result: /api/v1/dictionary/word/%E0%A4%A8%E0%A5%87%E0%A4%AA%E0%A4%BE%E0%A4%B2
const word = "नेपाल";
const encodedWord = encodeURIComponent(word);
const url = `https://sabdasakha.com/api/v1/dictionary/word/${encodedWord}`;

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:

import unicodedata

def normalize_nepali(text):
    return unicodedata.normalize("NFC", text)

# Always normalize before API calls
word = normalize_nepali("कृष्ण")
response = requests.get(f"https://sabdasakha.com/api/v1/dictionary/word/{word}")
function normalizeNepali(text) {
  return text.normalize("NFC");
}

const word = normalizeNepali("कृष्ण");
fetch(`https://sabdasakha.com/api/v1/dictionary/word/${word}`);

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

Verify Encoding

text = "नेपाल"
print(f"Length: {len(text)}")
print(f"Bytes: {text.encode('utf-8')}")
print(f"Hex: {text.encode('utf-8').hex()}")