---
title: "Train a dependency parser in C#"
url: https://recursiveai.org/onenlp/cookbook/train-dependency-parser/
description: "A dependency parser links each word to its grammatical head. Train those links and their labels from annotated sentences."
version: "1.0.0"
---

# Train a dependency parser in C#

**OneNLP** is a free, open-source natural language processing library for .NET from Recursive AI. MIT or Apache-2.0, no native dependencies.

[NuGet](https://www.nuget.org/packages/RecursiveAI.OneNlp.Neural) · [GitHub](https://github.com/recursive-ai-oss/onenlp-csharp) · [Docs](https://recursiveai.org/onenlp/docs/) · [Download example (.zip)](https://recursiveai.org/onenlp/cookbook/train-dependency-parser/train-dependency-parser.zip)

A dependency parser links each word to its grammatical head.

```shell
dotnet add package RecursiveAI.OneNlp.Neural
```

Requires [.NET 10](https://dotnet.microsoft.com/download/dotnet/10.0) (net10.0); .NET 9 and earlier aren’t supported.

```csharp
// train.conllu and dev.conllu: zip inputs shown below.
using RecursiveAI.OneNlp;
using RecursiveAI.OneNlp.Data;
using RecursiveAI.OneNlp.Neural.Components;
using RecursiveAI.OneNlp.Neural.Encoders;

using var nlp = Pipeline.Blank(Language.English).Add(
    new Parser(FastEncoder.English(7),
        arcSize: 32, labelSize: 16) { OwnsEncoder = true });
var train = ConllUReader.Read("train.conllu")
    .Select(s => Example.FromReference(s.Doc)).ToList();
var dev = ConllUReader.Read("dev.conllu")
    .Select(s => Example.FromReference(s.Doc)).ToList();
nlp.Train(train, new TrainOptions { Epochs = 30,
    Seed = 7, Dropout = 0f, DevExamples = dev });
var doc = nlp.Process("Birds chase cats");
foreach (var token in doc)
    Console.WriteLine($"{token.Text}: head={token.HeadIndex} {token.Dep}");
```

```text
Birds: head=1 nsubj
chase: head=1 root
cats: head=1 obj
```

Unzip [the example](https://recursiveai.org/onenlp/cookbook/train-dependency-parser/train-dependency-parser.zip), then `dotnet run`.

## Full program

These toy sentences demonstrate training, not real-world accuracy. The full program trains on four sentences, uses a separate sentence for development, saves `model.onenlp` and reloads it before predicting “Birds chase cats”. That prediction sentence is not in either input file.

`train.conllu` (tab-separated):

```text
1	Cats	cats	NOUN	NNS	_	2	nsubj	_	_
2	chase	chase	VERB	VBP	_	0	root	_	_
3	birds	birds	NOUN	NNS	_	2	obj	_	_

1	Dogs	dogs	NOUN	NNS	_	2	nsubj	_	_
2	chase	chase	VERB	VBP	_	0	root	_	_
3	cats	cats	NOUN	NNS	_	2	obj	_	_

1	Birds	birds	NOUN	NNS	_	2	nsubj	_	_
2	watch	watch	VERB	VBP	_	0	root	_	_
3	dogs	dogs	NOUN	NNS	_	2	obj	_	_

1	Cats	cats	NOUN	NNS	_	2	nsubj	_	_
2	watch	watch	VERB	VBP	_	0	root	_	_
3	birds	birds	NOUN	NNS	_	2	obj	_	_
```

`dev.conllu` (tab-separated):

```text
1	Dogs	dogs	NOUN	NNS	_	2	nsubj	_	_
2	watch	watch	VERB	VBP	_	0	root	_	_
3	cats	cats	NOUN	NNS	_	2	obj	_	_
```

```csharp
using RecursiveAI.OneNlp;
using RecursiveAI.OneNlp.Data;
using RecursiveAI.OneNlp.Neural;
using RecursiveAI.OneNlp.Neural.Components;
using RecursiveAI.OneNlp.Neural.Encoders;

using var nlp = Pipeline.Blank(Language.English).Add(
    new Parser(FastEncoder.English(7),
        arcSize: 32, labelSize: 16) { OwnsEncoder = true });
var train = ConllUReader.Read("train.conllu")
    .Select(s => Example.FromReference(s.Doc)).ToList();
var dev = ConllUReader.Read("dev.conllu")
    .Select(s => Example.FromReference(s.Doc)).ToList();
nlp.Train(train, new TrainOptions { Epochs = 30,
    Seed = 7, Dropout = 0f, DevExamples = dev });
PipelineFile.Save(nlp, "model.onenlp");
using var loaded = PipelineFile.Load("model.onenlp");
var doc = loaded.Process("Birds chase cats");
foreach (var token in doc)
    Console.WriteLine($"{token.Text}: head={token.HeadIndex} {token.Dep}");
Console.WriteLine("Saved model.onenlp");
```

`dotnet run` prints:

```text
Birds: head=1 nsubj
chase: head=1 root
cats: head=1 obj
Saved model.onenlp
```

Malformed CoNLL-U throws `DataFormatException` with a line number; a trained head rejects new labels outside its existing label set with `ArgumentException`.

## Variations

### Find the root word

After the program creates `doc`, find the token whose head is itself:

```csharp
// doc is the prediction from the program above.
foreach (var token in doc)
    if (token.HeadIndex == token.Index)
        Console.WriteLine($"Root: {token.Text}");
```

```text
Root: chase
```

## Related recipes

- [Use pretrained part-of-speech tagging](https://recursiveai.org/onenlp/cookbook/part-of-speech-tagging/)
- [Use a pretrained dependency parser](https://recursiveai.org/onenlp/cookbook/dependency-parsing/)
- [Read CoNLL-U training data](https://recursiveai.org/onenlp/cookbook/read-conllu-training-data/)
- [Save NLP results as JSON](https://recursiveai.org/onenlp/cookbook/save-nlp-results-as-json/)

---

## The short answer

To train a dependency parser in C#, add OneNLP’s `Parser` to a blank pipeline with a trainable encoder, convert dependency-annotated CoNLL-U sentences into examples and call `Train`. The program above saves and reloads the resulting pipeline, then prints each predicted head index and relation. It learns from the supplied corpus rather than loading a packaged parser.

## What is dependency parsing?

Dependency parsing represents a sentence as links between words. Each word has a head and a relation describing that link. In the sample, “Birds” is the subject of “chase”, while “cats” is its object. “Chase” is the root. These are token-to-token grammatical links, not arbitrary relationships between records or entities.

OneNLP’s [parser contract](https://recursiveai.org/onenlp/docs/parser/) describes a graph-based parser with arc and label scores and maximum-spanning-tree decoding. It builds a tree per sentence, including non-projective trees, and enforces exactly one token attached to the artificial root. If the label inventory contains `root`, only that token receives it. The sample reduces arc and label projection sizes for its toy corpus; these settings are not a recommendation for a production model.

## How do you train a dependency parser on a new corpus?

The CoNLL-U input supplies gold token boundaries, head links and dependency labels. `Example.FromReference` turns each sentence document into a training example. The parser uses examples only when predicted and reference tokenizations match one to one. Check token alignment before interpreting a training result: labels cannot supervise the intended arcs if the words no longer line up.

Dependency labels come from the reference documents, in ordinal order, including relation subtypes. A trained parser has a fixed label inventory. If a new corpus adds labels absent from an existing head, train a new head; the existing head rejects unseen labels rather than silently expanding its weights. The [training documentation](https://recursiveai.org/onenlp/docs/training/) specifies this behavior for the parser and other fixed-label heads.

Keep development data separate, as in the example, and use another untouched set for final evaluation. Learning these short repeated patterns does not demonstrate parsing quality on news, conversation or your application’s documents. Real training needs representative annotated text and a license that permits its use. The invented sample sentences are provided under CC0-1.0 so they can travel with the example.

## What is an example of a dependency?

In the output, `Birds: head=1 nsubj` means that the token “Birds” depends on token index 1, “chase”, with relation `nsubj`. Indices in the document API are zero-based. The root token points to itself, so `chase: head=1 root` is expected. CoNLL-U uses one-based word IDs and head 0 for the root; the reader performs that conversion.

Sentence boundaries also matter. When present, the parser uses them. Otherwise it predicts sentence starts and writes them into the document. A corpus of single sentences demonstrates arc learning, but it does not teach the boundary predictor how to split running multi-sentence text. The pinned parser documentation says sentences longer than 400 tokens are parsed in pieces of 400. Account for that documented behavior when choosing inputs and interpreting long-sentence trees.

Saving the trained pipeline with `PipelineFile` preserves the model for later inference. Storing the predicted documents in a binary bin is a separate operation: it keeps annotations, not the encoder and parser weights.
