minigpt-multi / unit_parser.py
Murali Chandran
updated unit_parser
f2f7976
Raw History Blame Contribute Delete
4.2 kB
# unit_parser_fixed.py
import re
from typing import Optional
# =============================
# Currency / money patterns
# =============================
CURRENCY_PREFIX = r"[$£€₹¥₩₦]|A\$|C\$|CHF|د\.إ"
MONEY_RE = re.compile(
rf"(?P<prefix>{CURRENCY_PREFIX})?\s*(?P<val>\d+(?:,\d{{3}})*(?:\.\d{{1,2}})?)\s*(?P<word>(?:[a-zA-Z]+(?:\s[a-zA-Z]+)?)s?)?",
re.IGNORECASE,
)
# =============================
# Unit / group price patterns
# =============================
UNIT_LIKE_RE = re.compile(
r"(?P<q1>\d+)\s+(?P<item>[\w\s]+?)\s+(?:is|are|cost|costs|priced at|=)\s+"
r"(?P<cost>\d+(?:,\d{3})*(?:\.\d+)?(?:\s*\w+)?)"
r"[,\.]?\s*(?:how much|what\s*'?s|price|cost).*?(?P<q2>\d+)\s+(?P=item)\b",
re.IGNORECASE,
)
GROUP_LIKE_RE = re.compile(
r"(?P<q1>\d+)\s+(?P<item>\w+?s?)\s+for\s+(?P<cost>.+?)\b[,\.]?\s*(?:how much|what\s*'?s|price|cost).*?(?P<q2>\d+)\s+(?P=item)\b",
re.IGNORECASE,
)
ADD_RE = re.compile(r"^\s*([\d+\s=\?]+)\s*$")
NUM_RE = re.compile(r"\d+")
# =============================
# Helpers
# =============================
def money(v: float) -> str:
return str(int(round(v))) if abs(v - round(v)) < 1e-9 else f"{v:.2f}"
def parse_money_str(s: str) -> str:
m = MONEY_RE.search(s)
if not m:
return s.strip()
prefix = (m.group('prefix') or '').strip()
val = float((m.group('val') or '').replace(',', ''))
word = (m.group('word') or '').strip()
if prefix:
return f"{prefix}{money(val)}"
if word:
if word.lower().endswith('s') and abs(val-1.0) < 1e-9:
word = word[:-1]
return f"{money(val)} {word}"
return money(val)
# =============================
# Canonicalizers
# =============================
def canonicalize_nl(text: str) -> Optional[str]:
t = text.strip()
m = UNIT_LIKE_RE.search(t)
if m:
q1 = int(m.group('q1')); item = m.group('item').rstrip('.')
cost = parse_money_str(m.group('cost'))
q2 = int(m.group('q2'))
return f"q: {q1} {item} cost {cost}. how much is {q2} {item}? a:"
m = GROUP_LIKE_RE.search(t)
if m:
q1 = int(m.group('q1')); item = m.group('item').rstrip('.')
cost = parse_money_str(m.group('cost'))
q2 = int(m.group('q2'))
return f"q: {q1} {item} for {cost}, what's the price for {q2} {item}? a:"
return None
def is_addition_query(text: str) -> bool:
t = text.strip()
if not ADD_RE.match(t):
return False
return ('+' in t) and (len(NUM_RE.findall(t)) >= 2)
def canonicalize_addition(text: str) -> str:
nums = [int(x) for x in NUM_RE.findall(text)]
return " + ".join(str(n) for n in nums)
def sum_addends(text: str) -> int:
return sum(int(x) for x in NUM_RE.findall(text))
# =============================
# Deterministic solver
# =============================
_NUMBER_TOKEN_RE = re.compile(r"\d+(?:,\d{3})*(?:\.\d+)?")
def try_solve_unit_like(text: str) -> Optional[str]:
m = UNIT_LIKE_RE.search(text)
if not m:
return None
q1 = int(m.group('q1'))
q2 = int(m.group('q2'))
item = (m.group('item') or '').strip().rstrip('.')
cost_raw = (m.group('cost') or '').strip()
nums = _NUMBER_TOKEN_RE.findall(cost_raw)
if not nums:
return None
token = max(nums, key=lambda s: len(s.replace(',', '')))
cost_val = float(token.replace(',', ''))
if q1 == 0:
return "final answer: undefined"
unit = cost_val / q1
total = unit * q2
mcur = MONEY_RE.search(cost_raw)
prefix = (mcur.group('prefix') or '').strip() if mcur else ''
word = (mcur.group('word') or '').strip() if mcur else ''
def _fmt(v: float) -> str:
s = money(v)
if prefix:
return f"{prefix}{s}"
if word:
sing = word[:-1] if word.lower().endswith('s') and abs(v-1.0) < 1e-9 else word
return f"{s} {sing}"
return s
base_item = item[:-1] if item.lower().endswith('s') else item
return (
f"step 1: find unit price: {_fmt(cost_val)} / {q1} = {_fmt(unit)} per {base_item}. "
f"step 2: multiply by {q2}: {_fmt(unit)} × {q2} = {_fmt(total)}. "
f"final answer: {_fmt(total)}"
)