Recursive AI

Batch process text documents 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.

Run the same NLP steps on each line of notes.txt (in the zip). This example counts sentences without a 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("sentencizer");
var lines = File.ReadLines("notes.txt");
var docs = nlp.ProcessMany(lines, batchSize: 2);
foreach (var doc in docs)
    Console.WriteLine(
        $"{doc.Sentences.Count}: {doc.Text}");
Text
1: Gate open.
2: Rain stopped. Roads cleared.
1: Bus arrived.

Unzip the example, then dotnet run.

Save these three lines as notes.txt beside the project file if you aren’t using the zip. Each line is one document, even if it contains several sentences.

Text
Gate open.
Rain stopped. Roads cleared.
Bus arrived.

A line longer than Pipeline.MaxLength throws ArgumentOutOfRangeException when the loop reaches it; the loop doesn’t skip that line.

Variations

Take one document per file instead. Replace the lines declaration with this input sequence. With the bundled file, this produces a single document with four sentences; line breaks stay in its text.

C#
var lines = new[] { File.ReadAllText("notes.txt") };

The short answer

Pass File.ReadLines("notes.txt") to Pipeline.ProcessMany and consume its results in a foreach loop. With batchSize: 2, OneNLP takes two strings at a time, tokenizes them, then runs the configured components over that batch. The loop still gets individual documents, in the same order as the input. The third input in the example is processed even though the final batch isn’t full.

What does File.ReadLines return in C#?

File.ReadLines returns an IEnumerable<string> you can enumerate before the whole file has been read. Each string is one line without its line terminator. Here, that line becomes one Doc. The method does not parse CSV fields, read a PDF, or know whether a line is a heading or a continuation of the previous one. If each file represents one document, supply one string per file instead, as in the variation.

The loop consumes the results as they arrive. Calling ToList() on the results instead would finish reading and hold all the documents before your code could use the list. The pipeline also shares a string table across its documents, and that table can grow. Lazy reading limits how much input the pipeline pulls ahead; it is not a promise that total memory stays fixed for every input and component.

What does batch size mean in an NLP pipeline?

For ProcessMany, it is the maximum number of input documents grouped for a pass through the pipeline, not the number of sentences, words, or token IDs. The default is 64; the example sets it to 2 so three lines exercise both a full batch and a shorter final batch. Pick a smaller batch when you need to limit how many long documents are processed together. The same input ordered in batches of one still produces one document per line.

Each component sees the batch before the next component runs. By default, a component’s batch method calls its single-document method for each document. A component can override that behavior, so grouping documents is not the same as processing them on separate threads. It also isn’t padding: the sentence-counting example never creates fixed-length token-ID arrays.

All onenlp recipes