Sanskrit is a classical language with a rich inflectional morphology and a relatively free word order. Because grammatical relations are primarily expressed through case endings rather than fixed positions, traditional phrasestructure parsing techniques often struggle with Sanskrit text. Dependency parsingwhere the syntactic structure is represented as a set of binary headdependent relationsoffers a natural fit for languages like Sanskrit. This page provides an overview of the challenges, major research efforts, and practical tools for building and using Sanskrit dependency parsers.
Understanding Sanskrit grammar helps in designing a parser.
The eight karaka roleskarta (agent), karma (patient), karana (instrument), sampradna (beneficiary), apdna (source), adhikra (location), sambandha (relation), and vibhakti (oblique)are essentially dependency labels. Modern parsers commonly map these to universal dependencies such as nsubj, obj, iobj, etc.
Sanskrit compounds can be up to several words long, with internal dependencies that are not obvious from surface order. A good parser must identify the head of the compound and attach modifiers appropriately.
Phonological coalescence (sandhi) merges word boundaries, creating challenges for tokenisation. Most pipelines first run a sandhi splitter before syntactic analysis.
The SanskritDT is a manually annotated corpus of around 3,000 sentences drawn from classical texts (the Vedas, Upanishads, and epic literature). It follows the Universal Dependencies (UD) scheme, providing POS tags, morphological features, and headdependent relations.
CSDP, released by the International Institute of Classical Studies, uses a transitionbased parser built on the spaCy framework. Training on SanskritDT yields an F1 score of ~89% for labeled attachment, which is competitive with parsers for other highly inflected languages.
Recent work applies transformerbased models (e.g., BERTSanskrit, mBERT) as contextual encoders, feeding their representations to a biaffine graphbased parser. Experiments reported in ACL 2023 achieve >92% LAS on heldout test sets.
SFST or the sandhi-splitter library to obtain word boundaries.Vka or shallow parser models that output case, gender, number, etc.import spacyfrom spacy.tokens import Docfrom sacremoses import MosesTokenizer# 1. Load a Sanskrit BERT model (e.g., indicnlp/bert-base-sanskrit)import transformersbert = transformers.AutoModel.from_pretrained('indicnlp/bert-base-sanskrit')tokenizer = transformers.AutoTokenizer.from_pretrained('indicnlp/bert-base-sanskrit')# 2. Build a spaCy pipelinenlp = spacy.blank("sa") # 'sa' is the ISO code for Sanskrit# custom component: BERT embeddingsdef bert_vectors(doc): inputs = tokenizer([t.text for t in doc], return_tensors='pt', padding=True) with torch.no_grad(): embeddings = bert(**inputs).last_hidden_state for token, vec in zip(doc, embeddings[0]): token._.set('bert_vec', vec) return docspacy.Token.set_extension('bert_vec', default=None)nlp.add_pipe(bert_vectors, name="bert_vectors", first=True)# 3. Add a dependency parser (trained on SanskritDT)parser = spacy.load("path/to/sanskrit_parser")nlp.add_pipe(parser)# 4. Parse a sentencedoc = nlp(" ")for token in doc: print(token.text, token.dep_, token.head.text, token.morph) Standard metrics are Unlabeled Attachment Score (UAS) and Labeled Attachment Score (LAS). For Sanskrit, it is also useful to report accuracy on specific karaka roles, because some relations (e.g., obl vs. advmod) are harder to distinguish.
While existing parsers achieve respectable scores, several avenues remain open:
If you want to experiment with Sanskrit dependency parsing, follow these steps:
spacyudpipe or the stanza Sanskrit model.pip install stanzapython -m stanza.install saimport stanzanlp = stanza.Pipeline('sa')doc = nlp(" ")doc.sentences[0].print_dependencies() This will output a tree with heads and dependency labels compatible with the UD schema.
