---
title: "Identify stop words with a custom list in C#"
url: https://recursiveai.org/onenlp/cookbook/match-custom-stop-words/
description: "Find stop words from your own list in C# with OneNLP. Match whole tokens regardless of case and return their original spans without deleting text."
version: "1.0.0"
---

# 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.

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

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.

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

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

```csharp
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](https://recursiveai.org/onenlp/cookbook/match-custom-stop-words/match-custom-stop-words.zip), 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.

```csharp
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`.

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

## Related recipes

- [Match text token patterns in C#](https://recursiveai.org/onenlp/cookbook/match-token-patterns/)
- [Match phrases in text in C#](https://recursiveai.org/onenlp/cookbook/match-phrases/)

---

## 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](https://apxml.com/courses/nlp-fundamentals/chapter-1-nlp-text-processing-techniques/advanced-stop-word-removal) discusses which words are worth keeping for different tasks.

## Can I use my own stop-word list?

Yes. [The matcher](https://recursiveai.org/onenlp/docs/matching/) 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.
