Abid Ali Awan commited on
Commit
6bc1434
·
1 Parent(s): c317aeb

Enhance holdings extraction in FinancialTools: updated regex patterns to differentiate between percentage and share formats, improving accuracy in parsing investment holdings.

Browse files
Files changed (1) hide show
  1. agents/tools.py +20 -3
agents/tools.py CHANGED
@@ -575,11 +575,11 @@ class FinancialTools:
575
 
576
  total_investment = extract_investment_amount(input_str)
577
 
578
- # Extract holdings with percentages using improved patterns
579
  def extract_holdings(text):
580
  holdings = []
581
 
582
- # Pattern for "SYMBOL XX%" format
583
  percentage_patterns = [
584
  r"([A-Z]{2,5})\s*[:\s]*(\d+(?:\.\d+)?)%",
585
  r"([A-Z]{2,5}):\s*(\d+(?:\.\d+)?)%",
@@ -594,7 +594,24 @@ class FinancialTools:
594
  "symbol": symbol.upper(),
595
  "percentage": float(percentage)
596
  })
597
- break
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
598
 
599
  # If no percentage matches, try JSON format
600
  if not holdings:
 
575
 
576
  total_investment = extract_investment_amount(input_str)
577
 
578
+ # Extract holdings - percentages vs shares
579
  def extract_holdings(text):
580
  holdings = []
581
 
582
+ # First try percentage patterns (with % symbol)
583
  percentage_patterns = [
584
  r"([A-Z]{2,5})\s*[:\s]*(\d+(?:\.\d+)?)%",
585
  r"([A-Z]{2,5}):\s*(\d+(?:\.\d+)?)%",
 
594
  "symbol": symbol.upper(),
595
  "percentage": float(percentage)
596
  })
597
+ return holdings
598
+
599
+ # If no percentages found, try shares patterns (without % symbol)
600
+ shares_patterns = [
601
+ r"([A-Z]{2,5})\s*[:\s]*(\d+(?:\.\d+)?)\s*(?!%)",
602
+ r"([A-Z]{2,5}):\s*(\d+(?:\.\d+)?)\s*(?!%)",
603
+ r"([A-Z]{2,5})\s+(\d+(?:\.\d+)?)\s*(?!%)",
604
+ ]
605
+
606
+ for pattern in shares_patterns:
607
+ matches = re.findall(pattern, text, re.IGNORECASE)
608
+ if matches:
609
+ for symbol, shares in matches:
610
+ holdings.append({
611
+ "symbol": symbol.upper(),
612
+ "shares": float(shares)
613
+ })
614
+ return holdings
615
 
616
  # If no percentage matches, try JSON format
617
  if not holdings: