Recursive AI

See which address fields came from a model in C#

OneContact is a free, open-source contact data library for .NET from Recursive AI. MIT or Apache-2.0, no native dependencies, no network access.

Read ParseDetail.Fields to see where the parser used a rule and where it used the model.

Terminal
dotnet add package RecursiveAI.OneContact.Models

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

C#
using System.Buffers;
using RecursiveAI.OneContact;
 
var text = "Alex Example\n123 Main St\n"
    + "Fort Worth TX 76102";
var status = PostalAddress.TryParse(
    text, Region.US, out _, out var detail);
if (status != OperationStatus.Done)
    throw new FormatException(
        detail.PrimaryReason.Id);
foreach (var field in detail.Fields)
    Console.WriteLine(
        $"{field.Name}: {field.Source}");

Paste the code above into Program.cs of a new dotnet new console project and dotnet run, or unzip the example and dotnet run.

Text
recipient: Model
street.house-number: Rule
street.name: Rule
street.type: Rule
locality: Rule
admin-area: Rule
postal-code: Rule

Check for OperationStatus.Done before reading the fields; the example reports the parser’s reason ID if the parse fails.

Variations

For a review screen, show just the original text of fields the model read. Use each field’s offsets against the original input, not the formatted address:

C#
using System.Buffers;
using RecursiveAI.OneContact;
 
const string text =
    "Alex Example\n123 Main St\nFort Worth TX 76102";
var status = PostalAddress.TryParse(
    text, Region.US, out _, out var detail);
if (status != OperationStatus.Done)
    throw new FormatException(detail.PrimaryReason.Id);
 
foreach (var field in detail.Fields)
{
    if (field.Source != DecisionSource.Model)
        continue;
    var span = field.Span;
    Console.WriteLine(text.Substring(span.Start, span.Length));
}
Text
Alex Example

The short answer

Use the PostalAddress.TryParse overload that returns both an OperationStatus and a ParseDetail. After OperationStatus.Done, read detail.Fields. A field whose Source is DecisionSource.Model was decided by the model; DecisionSource.Rule means a rule or lookup table decided it. Installing the model package does not make every parsed field model-derived.

How do I trace parsed address components back to the original string?

Use ParsedField.Name to find the component and ParsedField.Span to locate it. The span gives Start and Length in UTF-16 code units, the units used by string.Substring. The variation uses those offsets to recover Alex Example, with the original capitalization. Don’t apply them to address.Canonical: formatting may insert separators, change the case, or otherwise move the text. Keep the input string with the parse details if you need to reconstruct what someone entered.

The source and the formatted value answer different questions. The output’s recipient: Model says how the parser decided the role of the leading line, not that it generated a new recipient. Compare that with locality: Rule: the rules already recognized Fort Worth as the locality, so the model did not replace it. These distinctions are useful in review interfaces that highlight fields needing a second look without marking an entire row as a guess.

What does an address parser’s confidence score tell me?

ParsedField.Confidence ranges from 0 to 1. It’s exactly 1 for a rule decision; a model field carries the confidence reported by the model. Use Source, not a confidence threshold, to distinguish the two. The score describes how the parser assigned a field. It doesn’t tell you that the person lives there, that the street number exists, or that a postal service will deliver to it.

For a review queue, keep parse failure separate from a successful parse with a model-derived field. A failure has a reason you can show or log; a successful parse can contain a mix of rule and model decisions, as the example does. The parse API describes reason codes, and the model notes describe how field confidence is obtained.

All onecontact recipes