Dmitry Beresnev commited on
Commit ·
c226f41
1
Parent(s): 634a20f
fix OCR
Browse files160 unmodified lines1611621631641651661671681691701714 unmodified lines176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216160 unmodified lines"""Parse Revolut-specific format.Revolut format (2 lines per stock):Line 1: @ Company Name 3420,14$Line 2: 8,31 MU - 411,50'$ 4123,26%We need to extract the portfolio value from line 1 and ticker from line 2.Args:text: Extracted text from OCR4 unmodified linesportfolio = {}lines = text.split('\n')# Process lines in pairsi = 0while i < len(lines):current_line = lines[i].strip()# Look for portfolio value line (contains amount with $, €, £)value_match = re.search(r'([\d,]+[.,]\d{1,2})\s*[\$€£]', current_line)if value_match and i + 1 < len(lines):portfolio_value_str = value_match.group(1)# Look ahead for ticker in next linenext_line = lines[i + 1].strip()# Match pattern like: "8,31 MU - 411,50"# Ticker is 2-5 uppercase letters after some numbersticker_match = re.search(r'[\d,]+[.,]?\d*\s+([A-Z]{2,5})\s*[-–]', next_line)if ticker_match:ticker = ticker_match.group(1)# Clean portfolio value (replace , with . for European format)clean_value = portfolio_value_str.replace(',', '.')try:amount = float(clean_value)if amount > 1: # Valid amountportfolio[ticker] = amountexcept ValueError:pass# Skip the next line since we've processed iti += 2continuei += 1return portfolio160 unmodified lines1611621631641651661671681691701711721731741754 unmodified lines180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247160 unmodified lines"""Parse Revolut-specific format.Revolut format (typically 2-3 lines per stock):Line 1: @ Company Name 3420,14$Line 2: 8,31 MU - 411,50'$ 4123,26%(Sometimes line 3 if company name is long)Handles variations:- Spaces in numbers: "3 256,40"- Different separators: "-", ":", "*"- Numbers without decimals: "172312"Args:text: Extracted text from OCR4 unmodified linesportfolio = {}lines = text.split('\n')# Process linesi = 0while i < len(lines):current_line = lines[i].strip()# Look for portfolio value line (contains amount with $, €, £)# Handle spaces in numbers: "3 256,40" or "172312" or "3420,14"value_match = re.search(r'([\d\s,]+(?:[.,]\d{1,2})?)\s*[\$€£]', current_line)if value_match:portfolio_value_str = value_match.group(1)# Clean portfolio value:# 1. Remove spaces: "3 256,40" -> "3256,40"clean_value = portfolio_value_str.replace(' ', '')# 2. Handle numbers without decimal separators# If no decimal (. or ,) and more than 2 digits, assume last 2 are cents# Example: "172312" -> "1723.12"if not re.search(r'[.,]', clean_value) and len(clean_value) > 2:# Insert decimal before last 2 digitsclean_value = clean_value[:-2] + '.' + clean_value[-2:]else:# 3. Replace comma with dot for European format: "3256,40" -> "3256.40"clean_value = clean_value.replace(',', '.')try:amount = float(clean_value)if amount < 1: # Skip invalid amountsi += 1continueexcept ValueError:i += 1continue# Look ahead 1-2 lines for tickerticker_found = Falsefor lookahead in range(1, 3): # Check next 1-2 linesif i + lookahead >= len(lines):breakcheck_line = lines[i + lookahead].strip()# Match ticker patterns (more flexible):# "8,31 MU - 411,50" or "52,03 AMKR: 51$" or "GOOGL* 335,15"# Ticker can be followed by: -, :, *, space, or «ticker_match = re.search(r'[\d,]+[.,]?\d*\s+([A-Z]{2,5})[\s\-–:*«]', check_line)if ticker_match:ticker = ticker_match.group(1)# Validate ticker (not a word fragment or common false positive)if len(ticker) >= 2 and ticker not in ['AM', 'PM', 'USD', 'EUR', 'GBP', 'JPY']:portfolio[ticker] = amountticker_found = Truei += lookahead + 1 # Skip processed linesbreakif not ticker_found:i += 1else:i += 1return portfolio
ocr_parser.py+55-24