Skip to content

Ingredient Parsing and Units

Mike Christensen edited this page Aug 28, 2026 · 2 revisions

Ingredient parsing and units

KitchenPC turns human ingredient text into structured data and can aggregate compatible measurements.

Parse an ingredient usage

using KitchenPC.Core.NLP;

Result result = context.ParseIngredientUsage("a dozen eggs");

if (result.Status == MatchResult.Match || result.Usage is not null)
{
   var usage = result.Usage;
   Console.WriteLine(usage.Ingredient.Name);
   Console.WriteLine(usage.Amount);
}
else
{
   Console.WriteLine($"Could not parse '{result.Input}': {result.Status}");
}

Inputs can contain quantities, fractions, standard units, ingredient or form synonyms, custom unit words, and preparation notes. The parser resolves synonyms to the root ingredient and selects a form compatible with the parsed unit.

The result may be a normal Match, an AnomalousMatch produced by a safe fallback, a PartialMatch, or NoMatch. Failure statuses distinguish cases such as an unknown ingredient, unknown unit/form, missing form pairing, and incompatible form. Design the UI to preserve the original text and ask for clarification instead of discarding it.

ParseIngredient(string) is a simpler ingredient lookup. AutocompleteIngredient(string) is a separate substring index; each has its own capability.

Amounts and conversion

var amount = new Amount(1.5f, Units.Cup);
var normalized = context.ConvertIngredientUsage(usage);

UnitConverter handles conversion within compatible unit types. KitchenPC distinguishes weight, volume, and unit/count measurements. Ingredient forms provide ratios needed to convert between representations such as a cup of chopped ingredient and a weight. Without suitable metadata, the engine correctly refuses to guess.

Aggregate usages

var first = context.ParseIngredientUsage("1 cup milk");
var second = context.ParseIngredientUsage("8 fluid ounces milk");

var combined = context.AggregateIngredients(first.Usage, second.Usage);

Aggregation groups by normalized ingredient identity and combines compatible forms and amounts. Unspecified quantities and incompatible forms require application-specific presentation. Preserve source entries if users must later remove or cross out only one contribution; the Shopping List sample demonstrates this pattern.

Data drives parsing

The parser needs more than an ingredient name list. A useful store includes:

  • ingredient synonyms;
  • unit synonyms and custom unit-to-form mappings;
  • form synonyms and default weight/volume/unit pairings;
  • preparation notes;
  • anomalous ingredient rules;
  • numeric vocabulary and grammar templates.

The sample snapshot includes enough metadata to demonstrate common entries, but unknown household goods and uncommon foods are expected. Production quality depends on the completeness of your catalog.

Tracing a parse

For detailed decisions, set an ITracer with NlpTracer.SetTracer. The built-in DefaultTracer writes through Microsoft.Extensions.Logging. Tracing is intentionally verbose; enable it temporarily around development or diagnostics. See Logging and diagnostics.

Clone this wiki locally