Vipin
Back

1 Dec 2026 · 3 min read

Turning a legal rulebook into code: the compliance engine behind SentinelAI

How our HackSphere 2026 winning project reads product labels with OCR in English and Hindi, and scores them against India's Legal Metrology rules with regex and weighted penalties.

Every packaged product sold in India has to show certain details on its label: the MRP, net quantity, manufacturer, country of origin, date of manufacture and a customer-care contact. These are set by the Legal Metrology (Packaged Commodities) Rules, 2011. On e-commerce sites, these details are often missing, hidden in an image, or just wrong.

For KEC HackSphere 2026 our team built SentinelAI, which checks product listings against those rules automatically. It won first place. This post is about the core of it: how a legal document becomes a score from 0 to 100.

The pipeline

Playwright scrapers (7 platforms)
        ↓
product page text + label images
        ↓
Tesseract OCR (English + Hindi)
        ↓
field extraction (regex)
        ↓
compliance engine → score, status, violations
        ↓
dashboard

The scrapers collect listings from Amazon, Flipkart, BigBasket, Blinkit, Swiggy Instamart, Nykaa and Zepto. Many details only appear in the product photos, so the label images go through OCR.

Reading labels in two languages

Indian labels often mix English and Hindi, so the OCR worker loads both languages:

const Tesseract = require('tesseract.js');

let worker = null;

async function initializeWorker() {
  if (worker) return worker;              // reuse: creating a worker is slow
  worker = await Tesseract.createWorker('eng+hin');
  return worker;
}

Creating a Tesseract worker loads the language models, which takes a few seconds, so it's created once and reused for every image.

Turning rules into patterns

Each mandatory field becomes a regular expression that has to cope with the many ways sellers write the same thing:

const PATTERNS = {
  // ₹1234, Rs. 1234, MRP: 1234
  mrp: /(?:₹|Rs\.?|INR|MRP[:\s]*₹?)\s*(\d{1,6}(?:[.,]\d{1,2})?)/i,

  // 100g, 1.5kg, 500ml, 1L, 10pcs
  netQuantity: /(\d+(?:\.\d+)?)\s*(g|gm|gms|gram|grams|kg|kgs|ml|l|litre|liter|pcs|pieces|units)/i,

  countryOfOrigin: /(?:country\s*of\s*origin|made\s*in|manufactured\s*in|product\s*of)[:\s]*([a-zA-Z\s]+)/i,

  // FSSAI licence: 14 digits
  fssaiLicense: /(?:fssai|lic|license)[:\s#]*(\d{13,14})/i,
};

Writing these is less about regex skill and more about collecting real examples: Rs. with and without the dot, gm vs gms vs grams. The way to improve them is to run the scrapers and read what didn't match.

Scoring: weighted penalties

Not every missing field is equally serious. A product without an MRP is a much bigger problem than one without a customer-care email. So each field has a weight, and the score starts at 100 and loses points for each problem:

const FIELD_WEIGHTS = {
  mrp: 25,
  netQuantity: 20,
  manufacturer: 15,
  countryOfOrigin: 15,
  dateOfManufacture: 10,
  expiryDate: 10,      // food only
  customerCare: 5,
  importerDetails: 5,
  fssaiLicense: 5,     // food only
};

The rules are applied in three layers:

  1. Required fields: if missing or badly formatted, subtract the full weight and add a violation with a severity (critical if missing, warning if the format is wrong).
  2. Recommended fields (customer care, importer details): if missing, subtract half the weight, with severity info.
  3. Category rules: if the product name looks like food, the FSSAI licence and expiry date become mandatory too.

The final number maps to a status:

score = Math.max(0, score);

const status =
  score >= 80 ? 'compliant' :
  score >= 60 ? 'warning'   :
                'non-compliant';

Every result includes the list of violations, not just the number. A score of 55 isn't something a seller can fix; "Net quantity is missing a unit" is.

Why rules and not an LLM?

We could have sent each label to an LLM and asked whether it complies. We chose a rule engine for the core decision because:

  • It's explainable. Every lost point maps to a specific rule, which matters when the output could be used to flag a seller.
  • It's consistent. The same input always gives the same score.
  • It's fast and free. Thousands of products can be checked in a batch job without API costs.

AI does help with the messy input (OCR, and potentially LLM extraction from very noisy text), but the decision itself stays deterministic.

What I learned

  • Domain research comes first. The code is simple once you know exactly what the rules count as a violation. Reading the actual rulebook mattered more than any library choice.
  • Scrapers break constantly. Seven platforms means seven different page structures that change without notice, so one broken scraper should never stop the others.
  • Show the reason, not just the score. The violation list is what turns the tool into something a marketplace or a regulator could actually use.

Takeaway

A lot of "AI for compliance" is really careful rule-writing with AI handling the messy inputs. Get the rules right and keep them explainable, and use models where the data is too messy for rules.