Humanizr/Humanizer · error · ArgumentException

Unrecognized number word: {unrecognizedWord}

Error message

Unrecognized number word: {unrecognizedWord}

What it means

ScaleLeadingCompoundWordsToNumberConverter.Convert throws this ArgumentException at line 15 when TryConvert cannot parse the phrase under the scale-leading grammar (where scale nouns precede count words). Only Convert raises it; the Try overloads return false with the unrecognized token.

Source

Thrown at src/Humanizer/Localisation/WordsToNumber/ScaleLeadingCompoundWordsToNumberConverter.cs:15

namespace Humanizer;

/// <summary>
/// Parses decimal number words whose scale nouns precede their count words.
/// </summary>
internal class ScaleLeadingCompoundWordsToNumberConverter(ScaleLeadingCompoundWordsToNumberProfile profile) : GenderlessWordsToNumberConverter
{
    readonly ScaleLeadingCompoundWordsToNumberProfile profile = profile;

    /// <inheritdoc />
    public override long Convert(string words)
    {
        if (!TryConvert(words, out var parsedValue, out var unrecognizedWord))
        {
            throw new ArgumentException($"Unrecognized number word: {unrecognizedWord}");
        }

        return parsedValue;
    }

    /// <inheritdoc />
    public override bool TryConvert(string words, out long parsedValue) =>
        TryConvert(words, out parsedValue, out _);

    /// <inheritdoc />
    public override bool TryConvert(string words, out long parsedValue, out string? unrecognizedWord)
    {
        if (string.IsNullOrWhiteSpace(words))
        {
            parsedValue = default;
            unrecognizedWord = words ?? string.Empty;
            return false;
        }

View on GitHub (pinned to ffc2b77c0f)

Solutions

  1. Use TryToNumber(words, out var n, culture, out var bad) instead of ToNumber.
  2. Inspect bad to find and correct the offending token.
  3. Verify the culture's grammar family matches the phrase.
  4. Clean the input (trim, normalize spaces) before parsing.

Example fix

// before
var n = phrase.ToNumber(culture);
// after
if (phrase.TryToNumber(out var n, culture, out var bad))
    Use(n);
else
    Warn($"unrecognized token: {bad}");
Defensive patterns

Strategy: validation

Validate before calling

if (!words.TryToNumber(out var n, culture, out var unrecognized))
    return Result.Fail($"unrecognized token: {unrecognized}");
return Result.Ok(n);

Try / catch

try { var n = words.ToNumber(culture); }
catch (ArgumentException ex) when (ex.Message.StartsWith("Unrecognized number word"))
    { /* handle */ }

Prevention

When it happens

Trigger: Calling ToNumber with tokens outside the locale's scale-leading cardinal/ordinal map, malformed compounds, or foreign words. Fires at line 15 when TryConvert returns false.

Common situations: Wrong culture for the phrase; user free text; ordinal phrases unsupported by the grammar; tokens left over after tokenization.

Related errors


AI-assisted analysis of Humanizr/Humanizer@ffc2b77c0f (2026-08-13). Data as JSON: /api/errors/10793a4401865b48. Report an issue: GitHub.