Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 12 additions & 5 deletions financial-analysis/skills/check-deck/scripts/extract_numbers.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,23 +24,27 @@
class NumberInstance:
"""A numerical value found in the presentation."""
value: str # Original string representation
normalized: float # Normalized numeric value
normalized: Optional[float] # Normalized numeric value (None if unparseable)
unit: str # Detected unit (M, B, K, %, bps, x, etc.)
slide: int # Slide number (0 if unknown)
context: str # Surrounding text for context
line_number: int # Line number in source file
category: str # Detected category (revenue, margin, multiple, etc.)


def normalize_number(value_str: str, unit: str) -> float:
"""Convert a number string with unit to a normalized float value."""
def normalize_number(value_str: str, unit: str) -> Optional[float]:
"""Convert a number string with unit to a normalized float value.

Returns None if the value cannot be parsed, rather than 0.0,
to avoid silently converting invalid data into meaningful financial values.
"""
# Remove commas and spaces
clean = re.sub(r'[,\s]', '', value_str)

try:
base_value = float(clean)
except ValueError:
return 0.0
return None

# Apply unit multipliers
multipliers = {
Expand Down Expand Up @@ -168,6 +172,9 @@ def extract_numbers(content: str) -> list[NumberInstance]:
unit = f"USD_{unit}"

normalized = normalize_number(value_str, unit)
if normalized is None:
continue

category = detect_category(context, unit)

numbers.append(NumberInstance(
Expand Down Expand Up @@ -204,7 +211,7 @@ def find_inconsistencies(numbers: list[NumberInstance]) -> list[dict]:
placed = False
for group in value_groups:
ref_value = group[0].normalized
if ref_value > 0:
if ref_value is not None and inst.normalized is not None and ref_value > 0:
diff_pct = abs(inst.normalized - ref_value) / ref_value
if diff_pct < 0.05: # 5% tolerance
group.append(inst)
Expand Down