Ambiguity
pipelines.extraction.shared.ambiguity
Summary
Ambiguity resolution — faithful port of domain.extraction.ambiguity.
Candidate generation and three-mode dispatch, ported verbatim so the hexa PDF
path matches OG logic. The LLM-specific resolver is kept as an optional hook
(resolver); pause/CLI behaviour is preserved via PauseForResolution.
Classes
AmbiguityHandler
Three-mode ambiguity dispatcher: resolved_choices -> resolver -> pause -> CLI.
Attributes:
| Name | Type | Description |
|---|---|---|
fetch_request_id |
str
|
Identifier correlating this resolution to a fetch request. |
pause_on_ambiguity |
bool
|
Whether to raise |
resolved_choices |
dict
|
Cache of previously resolved candidate keys. |
resolver |
Optional[Callable]
|
Optional LLM/machine resolver hook. |
Initialize the ambiguity handler.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
fetch_request_id |
str
|
Identifier correlating this resolution to a
fetch request. Defaults to |
''
|
pause_on_ambiguity |
bool
|
Whether to raise |
True
|
resolved_choices |
Optional[dict]
|
Prior choices keyed by candidate
key. Defaults to |
None
|
resolver |
Optional[Callable]
|
Optional resolver hook. Defaults to
|
None
|
Functions
resolve_ambiguity
Resolve ambiguous amount/balance candidate pairs.
Validates the candidate pairs against the running balance and returns
the unique valid interpretation. When multiple valid interpretations
remain it consults resolved_choices, then the optional resolver
hook, then either raises PauseForResolution or falls back to CLI
resolution.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
line |
Raw transaction line being resolved. |
required | |
ocr_amount |
OCR-extracted amount. |
required | |
ocr_balance |
OCR-extracted balance. |
required | |
prev_balance |
Running balance from the previous transaction. |
required | |
candidates |
Collection of candidate (amount, balance) pairs. |
required |
Returns:
| Type | Description |
|---|---|
|
Set of a single (amount, balance) pair, or |
|
|
interpretation exists. |
Raises:
| Type | Description |
|---|---|
PauseForResolution
|
When multiple interpretations remain and
|
AmbiguousCandidateFound
Bases: ValueError
Raised when candidate generation produces multiple valid interpretations.
Attributes:
| Name | Type | Description |
|---|---|---|
candidates |
set
|
Ambiguous candidate (amount, balance) pairs. |
AmountBalanceNotFound
Bases: ValueError
Raised when no valid amount/balance interpretation can be found.
PauseForResolution
Bases: Exception
Signal that ambiguous candidates need human resolution.
Attributes:
| Name | Type | Description |
|---|---|---|
line |
Raw transaction line being resolved. |
|
ocr_amount |
OCR-extracted amount. |
|
ocr_balance |
OCR-extracted balance. |
|
prev_balance |
Running balance from the previous transaction. |
|
candidates |
Candidate (amount, balance) pairs. |
|
fetch_request_id |
str
|
Fetch request ID used for correlation. |
ResolutionContext
Resolves amount/balance pairs, generating candidates when direct validation fails.
Attributes:
| Name | Type | Description |
|---|---|---|
amount |
float
|
Absolute-rounded OCR amount. |
balance |
float
|
Rounded OCR balance. |
prev_balance |
float
|
Rounded running balance. |
line |
Raw transaction line being resolved. |
|
numbers |
Extracted numbers from the transaction line. |
|
tolerance |
float
|
Allowed rounding difference. |
handler |
Optional |
|
trust_fallback |
Optional fallback policy ( |
|
missing_number_candidates |
Optional candidate hook for one-number lines. |
|
adjustors |
Ordered list of candidate generator callables. |
Initialize a resolution context.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
amount |
OCR-extracted transaction amount. |
required | |
balance |
OCR-extracted closing balance. |
required | |
prev_balance |
Running balance from the previous transaction. |
required | |
line |
Raw transaction line being resolved. |
required | |
numbers |
Extracted numbers from the transaction line. |
required | |
handler |
Optional |
None
|
|
trust_fallback |
Optional fallback policy ( |
None
|
|
missing_number_candidates |
Optional callable providing candidates for one-number lines. |
None
|
Functions
resolve
Resolve to a validated (amount, balance) pair.
Applies direct sign validation first, then trust fallbacks, then walks the ordered candidate generators. Raises when nothing reconciles.
Returns:
| Type | Description |
|---|---|
|
Two-tuple of signed amount and validated balance. |
Raises:
| Type | Description |
|---|---|
AmountBalanceNotFound
|
When no candidate interpretation reconciles with the running balance. |
Functions
clean_num
Convert a numeric string to a float, handling decimals and paise.
A bare integer string is treated as paise (divided by 100); strings with a decimal point use the decimal part directly.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
s |
str
|
Numeric string to clean. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
float |
float
|
Parsed numeric value; |
get_numbers
Extract monetary numbers from OCR/text lines.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
text |
str
|
Text line to scan. |
required |
Returns:
| Type | Description |
|---|---|
List[float]
|
List[float]: Cleaned numeric values found in the text. |
validate_signs
Validate a (amount, balance) interpretation against the running balance.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
amount |
OCR-extracted transaction amount. |
required | |
balance |
OCR-extracted closing balance. |
required | |
prev_balance |
Running balance from the previous transaction. |
required | |
tolerance |
float
|
Allowed rounding difference. Defaults to 1.0. |
1.0
|
Returns:
| Type | Description |
|---|---|
|
Two-tuple of signed amount and validated balance, or |
|
|
when no sign combination reconciles with the running balance. |