paisa icon indicating copy to clipboard operation
paisa copied to clipboard

Add rules to predict account based on regex patterns

Open tonypius opened this issue 3 months ago • 1 comments

Account Rules Feature

This implementation adds regex-based account prediction rules to Paisa's import system.

Overview

The new feature provides:

  1. Account Rules Management UI: Located at /more/account-rules
  2. New Template Helper: predictAccountWithRules function for import templates
  3. API Endpoints: For managing account rules in the configuration

How to Use

1. Create Account Rules

  1. Navigate to More > Account Rules in the Paisa interface
  2. Click "New Rule" to create a rule
  3. Fill in the rule details:
    • Name: A descriptive name (e.g., "Amazon Purchases")
    • Pattern: A regex pattern (e.g., AMAZON.*|AMZ.*)
    • Account: Target account (e.g., Expenses:Shopping:Online)
    • Description: Optional description
    • Enabled: Toggle to enable/disable the rule

2. Update Import Templates

Replace predictAccount with predictAccountWithRules in your import templates:

Before:

{{date ROW.A "DD/MM/YYYY"}}  {{predictAccount ROW.B ROW.C prefix="Expenses:"}}  {{amount ROW.D}}
    Assets:Checking:SBI                           {{negate (amount ROW.D)}}

After:

{{date ROW.A "DD/MM/YYYY"}}  {{predictAccountWithRules ROW.B ROW.C prefix="Expenses:"}}  {{amount ROW.D}}
    Assets:Checking:SBI                           {{negate (amount ROW.D)}}

3. How It Works

  1. predictAccountWithRules first tries to match transaction data against your regex rules
  2. If a rule matches, it returns the configured account
  3. If no rules match, it falls back to the original TF-IDF based prediction
  4. Rules are processed in order - first match wins
  5. Rules respect the prefix parameter if provided

Example Rules

E-commerce

  • Pattern: AMAZON.*|AMZ.*Account: Expenses:Shopping:Online
  • Pattern: FLIPKART|FKRT.*Account: Expenses:Shopping:Online

Transportation

  • Pattern: UBER|LYFT|OLAAccount: Expenses:Transportation:Rideshare
  • Pattern: METRO|SUBWAYAccount: Expenses:Transportation:Public

Utilities

  • Pattern: ELECTRIC.*|POWER.*Account: Expenses:Utilities:Electric
  • Pattern: WATER.*|H2OAccount: Expenses:Utilities:Water

Banking

  • Pattern: ATM.*FEE|WITHDRAWAL.*FEEAccount: Expenses:Banking:Fees
  • Pattern: INTEREST.*CREDITAccount: Income:Interest:Bank

Technical Details

Configuration Storage

Rules are stored in the paisa.yaml configuration file under account_rules:

account_rules:
  - name: "Amazon Purchases"
    pattern: "AMAZON.*"
    account: "Expenses:Shopping:Online" 
    description: "Amazon purchases"
    enabled: true

API Endpoints

  • GET /api/account-rules - List all rules
  • POST /api/account-rules/upsert - Create/update a rule
  • POST /api/account-rules/delete - Delete a rule

Function Signature

predictAccountWithRules(...args, options)

Same parameters as predictAccount, with the added regex matching logic.

Benefits

  1. Precise Matching: Regex patterns allow exact matching of transaction descriptions
  2. Fallback Support: Falls back to ML-based prediction when no rules match
  3. Easy Management: Web UI for creating and managing rules
  4. Backward Compatible: Existing predictAccount templates continue to work
  5. Configurable: Rules can be enabled/disabled without deletion

tonypius avatar Aug 27 '25 09:08 tonypius

Have you tried the match function, which more or less does what you want.

        {{#if (isDate ROW.B "DD/MM/YYYY")}}
        {{date ROW.B "DD/MM/YYYY"}} {{ROW.D}}
          {{#if (or (eq (trim ROW.I) "0.00") (isBlank (trim ROW.I)))}}
          {{or (match ROW.D Expenses:Food="ZOMATO" Expenses:Groceries="paytm" Liabilities:CreditCard:UB="POS")
               "Expenses:Unknown"}} {{trim ROW.H}} INR
                Assets:Checking:Union
          {{else}}
                Assets:Checking:Union
               {{predictAccount prefix="Income"}} -{{trim ROW.I}} INR
          {{/if}}
        {{/if}}

ananthakumaran avatar Aug 28 '25 13:08 ananthakumaran