Recursive AI

Add a custom NLP pipeline component 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.

A pipeline component runs your code after text has been split into tokens. This example adds two named steps and needs no model.

Terminal
dotnet add package RecursiveAI.OneNlp

Requires .NET 10 (net10.0); .NET 9 and earlier aren’t supported.

C#
using RecursiveAI.OneNlp;
 
using var nlp = Pipeline.Blank();
nlp.Add(new CountTokens("first"));
nlp.Add(new CountTokens("second"));
nlp.Process("Cold brew.");
 
sealed class CountTokens(string name)
    : IPipelineComponent
{
    public string Name => name;
    public void Process(Doc doc) =>
        Console.WriteLine(
            $"{Name}: {doc.Length}");
}
Text
first: 3
second: 3

Unzip the example, then dotnet run.

Both steps see three tokens: Cold, brew, and ..

Adding a second component with the same name throws ArgumentException at Add, even if it’s a different instance.

Variations

Change the order. Replace the two Add calls with these. The pipeline now prints second: 3 before first: 3.

C#
nlp.Add(new CountTokens("second"));
nlp.Add(new CountTokens("first"));

The short answer

Implement IPipelineComponent with a unique Name and a Process(Doc doc) method. Register an instance with Pipeline.Add(component). When you call Pipeline.Process(text), the tokenizer creates a Doc, then the pipeline calls your component with that document. You don’t need a separate model package or a registry entry to add your own instance.

What are the components of an NLP pipeline?

In OneNLP, the tokenizer is the first step. The components that follow can read the document’s tokens and fill annotations such as tags, sentence boundaries, or entities. A component may also simply read the document, as CountTokens does. It doesn’t rewrite Doc.Text or move token boundaries. If your task needs different token boundaries, choose or implement a tokenizer instead of changing them in a component.

Pipeline.Blank() takes care of tokenization but adds no components. Use Add with your instance rather than Add<T>(), which selects a trained component from the model package used to create a pipeline. Choose a name that describes the step and distinguishes it from others in that pipeline. The example uses two names so you can watch the same class run twice.

In what order do NLP pipeline components run?

They run in the order you added them, not in name order. If one component writes sentence boundaries and another uses those boundaries to extract spans, add the writer first. A new document has no sentence boundaries set; a component reading sentence spans at that point sees the document treated as one sentence rather than the output of a sentence splitter. A later component can also overwrite annotations an earlier one set. Place a reporting step after the last writer to see the final values.

For document batches, the same order applies across the batch: the first component runs over the batch before the second. The interface’s default ProcessBatch calls Process for each document, so a simple component does not need a separate batch implementation. If you process documents on multiple threads through a shared pipeline, keep temporary state inside Process rather than in mutable fields on the component.

All onenlp recipes