Dmitry Beresnev commited on
Commit ·
deaa7ee
1
Parent(s): 3036bb1
fix OCR
Browse files160 unmodified lines1611621631641651661671641651661671681691701711721731711741751761771781799 unmodified lines18919019119219319419519619719819920020120220320420520620718919020820921021121221321414 unmodified lines2292302312112322332342352362378 unmodified lines246247248226227228229249250251252253254255256257258235236259260261262263238264265266267160 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) Revolut format (typically 2 lines per stock): Line 1: [icon] Company Name [portfolio_value]$ Line 2: [shares] TICKER[separator] [price_per_share]$ [change%]
Examples: Line 1: "@ Micron Technology 3 212,85 $" Line 2: "8,31 MU» 386,56 $ 4 109,73%"
Handles variations: - Spaces in numbers: "3 256,40" - Different separators: "-", ":", "*" - Different separators after ticker: "-", ":", "*", "»", "«" - Numbers without decimals: "172312" - Negative values in change column
Args: text: Extracted text from OCR9 unmodified lines while i < len(lines): current_line = lines[i].strip()
# Skip empty lines if not current_line: i += 1 continue
# Check if this is a TICKER line (not a value line) # Ticker lines start with: [shares] [TICKER][separator] # Example: "8,31 MU» 386,56 $" or "52,03 AMKR: 51$" or "0,94LLY -1080" is_ticker_line = re.match(r'^[\d,]+[.,]?\d*\s*[A-Z]{2,5}[\s\-–:*«»]', current_line)
if is_ticker_line: # This is a ticker line, skip it (it's already been processed as lookahead) i += 1 continue
# 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) # IMPORTANT: Match dollar amounts that are NOT preceded by a negative sign # Avoid matching negative change values like "-1080,46$" # Allow optional colon/apostrophe before currency: "3 120,52: $" or "240,92'$" value_match = re.search(r'(?<![\-–])([\d\s,]+(?:[.,]\d{1,2})?)[:\']?\s*[\$€£]', current_line)
if value_match: portfolio_value_str = value_match.group(1)14 unmodified lines
try: amount = float(clean_value) if amount < 1: # Skip invalid amounts # Filter out very small amounts (likely percentages, share counts, or other data) # Portfolio positions are typically > 50 (even small positions) if amount < 50: i += 1 continue except ValueError:8 unmodified lines
check_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) # Match ticker patterns: [shares] [TICKER][separator] # Examples: "8,31 MU -" or "52,03 AMKR:" or "5,06 GOOGL*" or "5,06 TSM «" # Also handles OCR errors with missing space: "0,94LLY" # Ticker can be followed by: -, :, *, », «, space, or end of significant text 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] = amount if len(ticker) >= 2 and ticker not in ['AM', 'PM', 'USD', 'EUR', 'GBP', 'JPY', 'CHF']: # Only add if not already present (avoid duplicates) if ticker not in portfolio: portfolio[ticker] = amount ticker_found = True i += lookahead + 1 # Skip processed lines i += lookahead + 1 # Skip to line after ticker line break
if not ticker_found:ocr_parser.py+41-15