Recursive AI

Match NLP tokens by word shape 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.

Use Pattern.Shape("XXdd") to find codes like AB12 and CD34 by shape: X = uppercase letter, x = other letters, d = digit.

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 doc = Pipeline.Blank().Process(
    "AB12 CD34 ABC12 ab12");
var matcher = new Matcher();
matcher.Add("CODE", Pattern.Shape("XXdd"));
foreach (var span in matcher.MatchSpans(doc))
    Console.WriteLine(span.Text);

A run of identical shape characters is capped at four.

The snippet prints:

Text
AB12
CD34

AB12 and CD34 match XXdd. ABC12 has an extra capital, and ab12 is lowercase, so neither matches. If the tokenizer splits a code into several tokens, a single Pattern.Shape won’t span those tokens.

Unzip the example, then dotnet run.

Variations

Match exactly four digits. Use both SHAPE and LENGTH; a shape of dddd alone would also match longer digit runs.

C#
using RecursiveAI.OneNlp;
using RecursiveAI.OneNlp.Matching;
 
var doc = Pipeline.Blank()
    .Process("1234 123456");
var matcher = new Matcher();
matcher.AddJson("PIN", """
    [{"SHAPE":"dddd","LENGTH":4}]
    """);
foreach (var span in matcher.MatchSpans(doc))
    Console.WriteLine(span.Text);
Text
1234

The short answer

Use Pattern.Shape("XXdd") with a Matcher to find tokens made of two uppercase letters followed by two digits. Call MatchSpans to read the original text. A blank pipeline supplies the tokens; you don’t need a language model to check the shape of text.

What is word shape in NLP?

Shape keeps the kinds of characters in a token and discards their identities. OneNLP writes X for an uppercase letter, x for other letters and d for a digit. It leaves other characters in place. For example, Hello becomes Xxxxx, while 12-3456 has shape dd-dddd if passed as one token. The checks use Unicode properties, so a digit shape isn’t restricted to ASCII 0 through 9. Use shape to find candidates, not to check that a code exists or meets a separate ASCII-only specification.

Each run of an identical shape character is capped at four. That makes 1234 and 123456 look the same to a shape-only rule. If a format needs a fixed length, combine shape with a LENGTH check, as in the variation. LENGTH counts Unicode code points. A SHAPE check doesn’t discard original characters from the document: the match still returns the original spelling, case and digits.

What is word tokenization in NLP, and why does it affect shape matching?

Word tokenization splits text into the units your rules see. A shape pattern tests one of those units at a time. For a hyphenated identifier, inspect whether your tokenizer retained it as one token or made the hyphen a separate token before you write the rule. If the code is split, write a pattern with one position for each token, including the punctuation position if there is one. If your input already comes with its own token boundaries, keep them when you create the document so that the rule sees the same boundaries you expect. The matching docs explain how pattern positions map to tokens.

All onenlp recipes