DEV Community

Ujjwal Dubey
Ujjwal Dubey

Posted on

Parsing Rupee Amounts From WhatsApp Messages: Lakh, Crore, "k" and Indian Digit Grouping in Python

Customers in India write money in many ways: "1,50,000", "1.5 lakh", "2L", "45k", "3500 rupees", or the rupee sign followed by "12,500". Before an automation can route a budget or match a payment, it has to read those numbers reliably. This is one of the first helpers an AI automation agency India message flows depend on should write, and it does not need AI.

The parser

import re

UNITS = {"k": 1_000, "thousand": 1_000, "l": 100_000, "lac": 100_000,
         "lakh": 100_000, "lakhs": 100_000, "cr": 10_000_000, "crore": 10_000_000}

PAT = re.compile(r"(?:rs\.?|inr|\u20b9)?\s*([0-9][0-9,]*(?:\.[0-9]+)?)\s*(k|thousand|l|lac|lakhs?|cr|crore)?\b",
                 re.I)

def parse_amounts(text: str):
    out = []
    for num, unit in PAT.findall(text or ""):
        value = float(num.replace(",", ""))
        if unit:
            value *= UNITS[unit.lower()]
        out.append(int(round(value)))
    return out

samples = ["budget 1.5 lakh", "around 45k", "paid 3500 rupees", "\u20b9 1,50,000 done", "2L to 3L"]
for s in samples:
    print(s, "->", parse_amounts(s))
Enter fullscreen mode Exit fullscreen mode

Things that bite

  1. Indian grouping. "1,50,000" has commas after the thousands and then every two digits. Stripping commas works; validating groups is optional.
  2. Ranges. "2L to 3L" returns two values. Keep both and let the flow decide (lower bound for filtering, both for the record).
  3. Dates and phone numbers. "12/11" or a ten-digit mobile can look like money. Run phone normalisation first and remove matches before parsing amounts.
  4. Ambiguous "k". Some customers write "5k" for five thousand, others mean five karat in jewellery chats. Use context or ask.
  5. Never auto-reply with a computed price. Parse to route and record; let a person confirm any figure sent back.

Test with real messages

Collect a few hundred anonymised messages, label the expected values by hand and run the parser against them. Fix the failures you see, not the ones you imagine.

Before writing any code, we usually map the whole flow first; the questions we ask are in our guide on walking through a process before adding AI.

Disclosure: I co-founded NxFlowAI, a Mumbai-based AI automation agency.

Top comments (0)