Recursive AI

Identify stop words with a custom list 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.

Stop words are words you choose to ignore for a task. OneNLP matches them without removing them, and the core package ships no default list.

Terminal
dotnet add package RecursiveAI.OneNlp

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

C#
using RecursiveAI.OneNlp;
using RecursiveAI.OneNlp.Matching;
 
var text = "The cat and the dog.";
var doc = Nlp.Analyze(text);
var matcher = new Matcher();
matcher.StopWords = ["the", "and"];
matcher.AddJson("STOP", """
    [{"IS_STOP":true}]
    """);
foreach (var span in matcher.MatchSpans(doc))
    Console.WriteLine(span.Text);

Unzip the example, then dotnet run.

The pattern [{"IS_STOP":true}] matches any single token in StopWords.

Text
The
and
the

The full program

The same pattern runs twice on the same text. Before the list is set there are zero matches; afterward the program prints the matching spans and the unchanged document.

C#
using RecursiveAI.OneNlp;
using RecursiveAI.OneNlp.Matching;
 
var doc = Nlp.Analyze("The cat and the dog.");
var matcher = new Matcher();
matcher.AddJson("STOP", """[{"IS_STOP":true}]""");
Console.WriteLine($"Before: {matcher.MatchSpans(doc).Count}");
matcher.StopWords = ["the", "and"];
foreach (var span in matcher.MatchSpans(doc))
    Console.WriteLine(span.Text);
Console.WriteLine(doc.Text);
Text
Before: 0
The
and
the
The cat and the dog.

If IS_STOP returns nothing, check that you set StopWords on the matcher you’re using and that its entries are lowercase.

Variations

Mark a domain-specific word. In the snippet at the top, replace the text and list with these values. It prints Ticket and ticket.

C#
var text = "Ticket open. Close the ticket.";
// Set this after creating the matcher.
matcher.StopWords = ["ticket"];

The short answer

Set Matcher.StopWords to a collection of lowercase strings, add a pattern with one IS_STOP test, and call MatchSpans on the tokenized document. The matcher tests lowercase token text against your list, but the span gives you the original spelling. In the example, The and the match the same entry and appear as two separate results.

What are stop words?

Stop words are terms you treat as uninformative for a particular job. For a keyword list, the and and may be useful to set aside. For a search index of support tickets, you could also choose to set aside ticket if people use that word in nearly every record. A word’s frequency alone isn’t enough to decide. For example, not changes the meaning of “not available” and may be important to a search or text classifier. The guide to custom stop words discusses which words are worth keeping for different tasks.

Can I use my own stop-word list?

Yes. The matcher starts with an empty list. Assigning StopWords replaces the current list and copies your entries into a set. If you edit the source collection later, assign it again. OneNLP lowercases the token for comparison, not the supplied entries, so put "the" in the list rather than "The".

The one-position pattern matches complete tokens, not substrings: the does not match inside there. It also does not treat a two-word entry as two token positions. To detect a multiword phrase, use a phrase pattern instead. If a word you expected doesn’t match, check how the tokenizer split it before changing the rule.

Why identify stop words instead of removing them?

A span lets you highlight or count an occurrence without changing the text or its offsets. This is useful when one part of your app computes keyword counts and another shows where each match came from. The matcher does not label the document’s tokens in place or produce a cleaned string. If you need a filtered stream for a downstream feature, treat that as a separate result and keep the original document for display or for tasks that need the missing context.

All onenlp recipes