File size: 12,481 Bytes
24e6f5b | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 | """
Category classification utility with comprehensive keyword-based detection
"""
# --- EXPENSE CATEGORIES ---
EXPENSE_KEYWORDS = {
"Housing": [
'rent', 'mortgage', 'lease', 'hoa', 'property tax', 'home maintenance',
'repair', 'pest control', 'apartment', 'realtor', 'landlord', 'security deposit'
],
"Food & Dining": [
'ramen', 'sushi', 'pizza', 'burger', 'sandwich', 'salad',
'coffee', 'tea', 'soda', 'drink', 'beverage', 'juice',
'restaurant', 'cafe', 'diner', 'bistro', 'eatery', 'bar',
'breakfast', 'lunch', 'dinner', 'brunch', 'meal',
'food', 'dish', 'cuisine', 'menu', 'takeout', 'mcdonald', 'kfc',
'subway', 'starbucks', 'domino', 'walmart grocery', 'instacart', 'doordash', 'ubereats',
'grubhub', 'grocery', 'supermarket', 'mart', 'store', 'whole foods',
'trader joe', 'costco food', 'canteen', 'dining hall', 'bakery', 'donut', 'patisserie',
'winery', 'brewery', 'pub', 'liquor', 'wine', 'beer', 'safeway', 'kroger', 'tesco',
'swiggy', 'zomato', 'deliveroo', 'spices', 'sweets', 'organic', 'vegetable', 'meat'
],
"Transportation": [
'uber', 'lyft', 'taxi', 'cab', 'ride', 'gas', 'fuel', 'petrol', 'diesel',
'gasoline', 'parking', 'toll', 'highway', 'bus', 'train', 'metro', 'subway',
'transit', 'car', 'vehicle', 'auto', 'automobile', 'rental', 'rent-a-car',
'hertz', 'avis', 'enterprise', 'oil change', 'tire', 'garage',
'charging', 'ev', 'tesla supercharger', 'parking meter', 'amtrak', 'commuter',
'fare', 'ferry', 'cycling', 'bike', 'scooter', 'fastag', 'puc', 'license', 'ola'
],
"Shopping": [
'amazon', 'ebay', 'walmart', 'target', 'costco', 'clothing', 'apparel',
'fashion', 'zara', 'h&m', 'electronics', 'laptop', 'phone', 'tablet',
'computer', 'apple', 'best buy', 'furniture', 'home decor', 'ikea',
'homedepot', 'lowe', 'book', 'toy', 'hobby lobby', 'michaels', 'gift',
'mall', 'outlet', 'department store', 'boutique', 'stationary', 'hardware',
'jewelry', 'shoes', 'sneakers', 'nike', 'adidas', 'macys', 'nordstrom', 'shein',
'flipkart', 'myntra', 'ajio', 'meesho', 'nykaa', 'optical', 'watch', 'accessories'
],
"Healthcare": [
'doctor', 'hospital', 'clinic', 'medical', 'health', 'pharmacy', 'medicine',
'drug', 'prescription', 'cvs', 'walgreens', 'dental', 'dentist', 'teeth',
'orthodont', 'vision', 'eye', 'glasses', 'contact', 'therapy', 'counseling',
'psycholog', 'physio', 'chiropract', 'dermatolog', 'copay', 'deductible',
'medical bill', 'vaccine', 'checkup', 'test', 'lab', 'blood work'
],
"Insurance": [
'insurance', 'premium', 'policy', 'state farm', 'geico', 'progressive',
'allstate', 'liberty mutual', 'auto insurance', 'life insurance',
'home insurance', 'renters insurance', 'health insurance', 'lic', 'hdfc ergo', 'tata aig'
],
"Entertainment": [
'movie', 'cinema', 'theater', 'film', 'concert', 'show', 'event',
'ticketmaster', 'game', 'gaming', 'xbox', 'playstation', 'nintendo', 'steam',
'gym', 'fitness', 'yoga', 'sport', 'recreation', 'park', 'museum', 'zoo',
'aquarium', 'gallery', 'hobby', 'club', 'membership', 'nightlife', 'festival'
],
"Subscriptions": [
'subscription', 'recurring', 'netflix', 'hbo', 'max', 'spotify', 'hulu',
'disney', 'paramount', 'youtube premium', 'patreon', 'onlyfans', 'medium',
'substack', 'nyt', 'wsj', 'icloud', 'google one', 'dropbox', 'microsoft 365',
'adobe', 'canva', 'audible', 'itunes'
],
"Bills & Utilities": [
'electric', 'power', 'water', 'sewer', 'trash', 'garbage', 'recycling',
'gas bill', 'heating', 'cooling', 'hvac', 'internet', 'wifi', 'broadband',
'cable', 'comcast', 'xfinity', 'phone bill', 'mobile', 'cellular',
'verizon', 'at&t', 't-mobile', 'recharge', 'prepaid', 'postpaid', 'topup',
'maintenance', 'landline', 'utility', 'energy'
],
"Education": [
'tuition', 'school', 'college', 'university', 'course', 'class', 'training',
'workshop', 'udemy', 'coursera', 'edx', 'skillshare', 'tutor', 'seminar',
'textbook', 'learning', 'study', 'student'
],
"Personal Care": [
'salon', 'haircut', 'barber', 'spa', 'massage', 'wellness', 'cosmetic',
'makeup', 'beauty', 'skincare', 'sephora', 'ulta', 'shampoo', 'toiletries',
'grooming', 'hygiene', 'barbershop'
],
"Travel": [
'hotel', 'motel', 'airbnb', 'booking', 'accommodation', 'lodging', 'vacation',
'trip', 'tour', 'travel', 'holiday', 'expedia', 'kayak', 'resort', 'cruise',
'excursion', 'sightseeing', 'flight', 'airline', 'airport', 'ticket',
'luggage', 'suitcase', 'backpack', 'travel insurance'
],
"Pets": [
'pet', 'dog', 'cat', 'vet', 'veterinary', 'chewy', 'pet food', 'pet store',
'grooming', 'petco', 'petsmart', 'animal', 'puppy', 'kitten', 'boarding', 'daycare'
],
"Family & Kids": [
'childcare', 'daycare', 'babysitter', 'nanny', 'diapers', 'baby food', 'toys',
'child support', 'allowance', 'school supplies', 'tutoring', 'camp', 'extracurricular'
],
"Gifts & Donations": [
'gift', 'present', 'donation', 'charity', 'goodwill', 'contribution',
'fundraiser', 'nonprofit', 'gofundme', 'birthday', 'wedding', 'anniversary',
'floral', 'flowers'
],
"Taxes": [
'tax', 'irs', 'state tax', 'federal tax', 'income tax', 'estimated tax',
'tax return payment', 'turbo tax', 'hr block', 'h&r block'
],
"Professional Services": [
'legal', 'lawyer', 'attorney', 'accountant', 'tax prep', 'consultant',
'advisor', 'notary', 'architect', 'engineer', 'design fee', 'fiverr', 'upwork', 'freelancer'
],
"Investments & Savings": [
'contribution', 'vanguard', 'fidelity', 'charles schwab',
'robinhood', '401k', '401-k', 'ira', 'roth', 'stock purchase', 'etf purchase',
'crypto purchase', 'sip', 'mutual fund', 'zerodha', 'upstox', 'groww'
],
"Other Expenses": [
'other', 'misc', 'miscellaneous', 'general', 'extra'
]
}
# --- INCOME CATEGORIES ---
INCOME_KEYWORDS = {
"Salary": [
'salary', 'wage', 'payroll', 'paycheck', 'direct deposit',
'monthly pay', 'biweekly', 'weekly pay', 'stipend',
'employment', 'employer', 'company pay'
],
"Freelance & Gigs": [
'freelance', 'contract', 'gig', 'project', 'consulting', 'consultant',
'client work', 'upwork', 'fiverr', 'freelancer', 'toptal', 'taskrabbit',
'uber income', 'lyft income', 'doordash income'
],
"Business Revenue": [
'business', 'revenue', 'sales', 'profit', 'commission', 'merchant',
'customer payment', 'invoice', 'client payment', 'stripe', 'paypal',
'square', 'shopify', 'payout', 'e-commerce'
],
"Investments & Dividends": [
'dividend', 'interest', 'capital gains', 'payout', 'return on investment',
'portfolio', 'profit sharing', 'yield', 'invest', 'stock return',
'stock', 'share', 'nasdaq', 'nyse', 'trading', 'brokerage', 'equity',
'robinhood', 'vanguard', 'fidelity', 'charles schwab', 'etrade', 'ameritrade'
],
"Rental Income": [
'rent', 'rental income', 'tenant', 'lease', 'property', 'airbnb income',
'sublease', 'real estate'
],
"Refunds & Cashbacks": [
'refund', 'reimbursement', 'cashback', 'rebate', 'rakuten', 'return',
'credit', 'compensation', 'claim', 'honey', 'retailmenot', 'venmo refund'
],
"Gifts & Bonuses": [
'gift', 'present', 'bonus', 'prize', 'award', 'lottery', 'inheritance',
'donation received', 'birthday gift', 'monetary gift', 'sign-on bonus',
'performance bonus'
],
"Crypto Bit Coins": [
'crypto', 'bitcoin', 'btc', 'eth', 'ethereum', 'binance', 'coinbase',
'wallet', 'blockchain', 'ledger', 'kraken', 'doge', 'solana', 'nft', 'tether', 'usdt'
],
"Grants & Scholarships": [
'grant', 'scholarship', 'financial aid', 'fellowship', 'bursary',
'stipend', 'education award', 'research grant', 'academic award'
],
"Pensions & Social Security": [
'pension', 'social security', 'ssi', 'retirement income',
'401k withdrawal', 'ira withdrawal', 'annuity'
],
"Other Income": [
'miscellaneous', 'other', 'extra', 'side hustle', 'passive income',
'royalty', 'allowance', 'government', 'tax refund', 'stimulus'
]
}
def classify_transaction(title, transaction_type='Expense'):
"""
Classify transaction category based on title keywords
"""
if not title:
return {'category': 'Other', 'type': transaction_type}
# --- STRATEGY 1: Cloud AI (Gemini Fallback) ---
try:
from finance.ai_helper import classify_text_with_llm
ai_result = classify_text_with_llm(title)
if ai_result and ai_result.get('category'):
ai_result["source"] = ai_result.get("source", "ai_cloud")
return ai_result
except Exception as e:
print(f"AI Classification failed: {e}. Falling back to Local Rules.")
return _classify_locally(title, transaction_type)
def _classify_locally(title, transaction_type='Expense'):
"""
Internal helper for keyword-based classification without AI calls.
"""
if not title:
return {'category': 'Other', 'type': transaction_type}
title_lower = title.lower()
t_type_norm = "Income" if str(transaction_type).lower() == "income" else "Expense"
# Check keywords based on normalized type
if t_type_norm == 'Income':
for category, keywords in INCOME_KEYWORDS.items():
for kw in keywords:
if kw.lower() in title_lower:
return {'category': category, 'type': 'Income'}
else:
for category, keywords in EXPENSE_KEYWORDS.items():
for kw in keywords:
if kw.lower() in title_lower:
return {'category': category, 'type': 'Expense'}
# --- FINAL DEFAULT ---
if t_type_norm == 'Income':
return {'category': 'Other Income', 'type': 'Income'}
return {'category': 'Uncategorized', 'type': 'Expense'}
def batch_classify_transactions(transactions):
"""
Classify a list of transactions efficiently using a mix of local rules and batch AI.
"""
if not transactions:
return []
results = [None] * len(transactions)
to_ai_indices = []
to_ai_titles = []
to_ai_types = []
for idx, tx in enumerate(transactions):
title = tx.get('title', '')
t_type = tx.get('type', 'Expense')
if t_type.lower() not in ['expense', 'income']:
t_type = 'Unknown'
# 1. Try Local Rules ONLY first
local_result = _classify_locally(title, t_type)
if local_result['category'] != 'Uncategorized' and t_type != 'Unknown':
results[idx] = local_result
else:
to_ai_indices.append(idx)
to_ai_titles.append(title)
to_ai_types.append(t_type)
# 2. Batch process the remaining with AI
if to_ai_indices:
try:
from finance.ai_helper import batch_classify_transactions_with_llm
ai_results = batch_classify_transactions_with_llm(to_ai_titles, to_ai_types)
for i, res_obj in enumerate(ai_results):
if i < len(to_ai_indices):
idx = to_ai_indices[i]
results[idx] = {
'category': res_obj.get('category', 'Uncategorized'),
'type': res_obj.get('type', to_ai_types[i] if to_ai_types[i] != 'Unknown' else 'Expense')
}
except Exception as e:
print(f"Batch AI classification failed: {e}")
for idx in to_ai_indices:
if results[idx] is None:
results[idx] = {'category': 'Uncategorized', 'type': transactions[idx].get('type', 'Expense')}
# Final pass: fill any remains
for i in range(len(results)):
if results[i] is None:
results[i] = {'category': 'Uncategorized', 'type': transactions[i].get('type', 'Expense')}
return results
|