Dmitry Beresnev commited on
Commit ·
deaa7ee
1
Parent(s): 3036bb1
fix OCR
Browse files160 unmodified lines1611621631641651661671681691701711721731741759 unmodified lines18518618718818919019119219314 unmodified lines2082092102112122132148 unmodified lines223224225226227228229230231232233234235236237238239240241160 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 OCR9 unmodified lineswhile 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)14 unmodified linestry:amount = float(clean_value)if amount < 1: # Skip invalid amountsi += 1continueexcept ValueError:8 unmodified linescheck_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:160 unmodified lines1611621631641651661671681691701711721731741751761771781799 unmodified lines18919019119219319419519619719819920020120220320420520620720820921021121221321414 unmodified lines2292302312322332342352362378 unmodified lines246247248249250251252253254255256257258259260261262263264265266267160 unmodified lines"""Parse Revolut-specific format.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 after ticker: "-", ":", "*", "»", "«"- Numbers without decimals: "172312"- Negative values in change columnArgs:text: Extracted text from OCR9 unmodified lineswhile i < len(lines):current_line = lines[i].strip()# Skip empty linesif not current_line:i += 1continue# 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 += 1continue# Look for portfolio value line (contains amount with $, €, £)# 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 linestry:amount = float(clean_value)# Filter out very small amounts (likely percentages, share counts, or other data)# Portfolio positions are typically > 50 (even small positions)if amount < 50:i += 1continueexcept ValueError:8 unmodified linescheck_line = lines[i + lookahead].strip()# 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 textticker_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', 'CHF']:# Only add if not already present (avoid duplicates)if ticker not in portfolio:portfolio[ticker] = amountticker_found = Truei += lookahead + 1 # Skip to line after ticker linebreakif not ticker_found:
ocr_parser.py+41-15