// Parse free-text amount input into grams // Handles: "200г", "половина пачки 400г", "2 ст.л.", "3 куска", etc. import { MEASURE_PATTERNS } from "./measures.js"; // Extract a number near a gram-like unit const GRAMS_RE = /(\d+(?:[.,]\d+)?)\s*(г|гр|грамм|g)\b/i; // Extract fraction of a package const FRACTION_PACK_RE = /(?:полов\w*|пол|1\/2)\s*(?:пачк\w*|упаков\w*|банк\w*)/i; const THIRD_PACK_RE = /(?:треть|1\/3)\s*(?:пачк\w*|упаков\w*|банк\w*)/i; const QUARTER_PACK_RE = /(?:четверт\w*|1\/4)\s*(?:пачк\w*|упаков\w*|банк\w*)/i; const PACK_WEIGHT_RE = /(\d+)\s*(г|гр|грамм|g)\b/i; // Count of items const COUNT_RE = /(\d+)\s*(кус\w*|штук\w*|шт\.?)\b/i; // Duration for activities (minutes/hours) const DURATION_MIN_RE = /(\d+)\s*(мин\w*|m)\b/i; const DURATION_HR_RE = /(\d+(?:[.,]\d+)?)\s*(час\w*|ч\.?|h)\b/i; // Just a bare number const BARE_NUMBER_RE = /^(\d+(?:[.,]\d+)?)$/; export function parseAmount(amountStr, description) { if (!amountStr || !amountStr.trim()) { return { grams: null, minutes: null, count: null, source: "default" }; } const text = amountStr.trim().toLowerCase(); // 1. Exact grams: "200г" let m = text.match(GRAMS_RE); if (m) { return { grams: parseFloat(m[1].replace(",", ".")), minutes: null, count: null, source: "grams" }; } // 2. Fraction of package: "половина пачки 400г" const combined = (amountStr + " " + (description || "")).toLowerCase(); if (FRACTION_PACK_RE.test(combined)) { const pw = combined.match(PACK_WEIGHT_RE); if (pw) { return { grams: parseFloat(pw[1]) / 2, minutes: null, count: null, source: "half-pack" }; } } if (THIRD_PACK_RE.test(combined)) { const pw = combined.match(PACK_WEIGHT_RE); if (pw) { return { grams: Math.round(parseFloat(pw[1]) / 3), minutes: null, count: null, source: "third-pack" }; } } if (QUARTER_PACK_RE.test(combined)) { const pw = combined.match(PACK_WEIGHT_RE); if (pw) { return { grams: parseFloat(pw[1]) / 4, minutes: null, count: null, source: "quarter-pack" }; } } // 3. Duration (for activities): "30 мин", "1.5 часа" m = text.match(DURATION_HR_RE); if (m) { return { grams: null, minutes: Math.round(parseFloat(m[1].replace(",", ".")) * 60), count: null, source: "hours" }; } m = text.match(DURATION_MIN_RE); if (m) { return { grams: null, minutes: parseInt(m[1]), count: null, source: "minutes" }; } // 4. Household measure: "2 ст.л." or just "ст.л." for (const mp of MEASURE_PATTERNS) { if (mp.pattern.test(text)) { // Check for a multiplier before the measure const before = text.split(mp.pattern)[0].trim(); const multiplier = before.match(/(\d+)/) ? parseInt(before.match(/(\d+)/)[1]) : 1; return { grams: mp.grams * multiplier, minutes: null, count: null, source: "measure" }; } } // 5. Count: "3 куска", "2 шт" m = text.match(COUNT_RE); if (m) { return { grams: null, minutes: null, count: parseInt(m[1]), source: "count" }; } // 6. Bare number → assume grams m = text.match(BARE_NUMBER_RE); if (m) { return { grams: parseFloat(m[1].replace(",", ".")), minutes: null, count: null, source: "bare-grams" }; } return { grams: null, minutes: null, count: null, source: "unknown" }; } // Estimate kcal from parsed amount + food data export function estimateKcal(parsed, food) { if (!food) return null; if (parsed.grams != null) { return Math.round((food.kcal / 100) * parsed.grams); } if (parsed.count != null) { return Math.round((food.kcal / 100) * food.portion * parsed.count); } // Use default portion return Math.round((food.kcal / 100) * food.portion); }