improve parsing 3
This commit is contained in:
@@ -25,7 +25,7 @@ class ReceiptParser {
|
||||
|
||||
/// Primary path: positioned ML Kit elements → row clustering → lookahead.
|
||||
static ReceiptParseResult parseReceiptFromOcr(OcrDocument document) =>
|
||||
ReceiptSpatialParser.parse(document);
|
||||
_parseDocument(document);
|
||||
|
||||
static ReceiptParseResult parseRecognizedText(
|
||||
RecognizedText recognized, {
|
||||
@@ -37,6 +37,50 @@ class ReceiptParser {
|
||||
imageWidth: imageWidth,
|
||||
imageHeight: imageHeight,
|
||||
);
|
||||
return parseReceiptFromOcr(document);
|
||||
return _parseDocument(document);
|
||||
}
|
||||
|
||||
static ReceiptParseResult _parseDocument(OcrDocument document) {
|
||||
if (ReceiptTextParser.looksLikeColumnOcr(document.flattenedText)) {
|
||||
return _withImageDimensions(
|
||||
ReceiptTextParser.parseReceipt(document.flattenedText),
|
||||
document,
|
||||
);
|
||||
}
|
||||
|
||||
final spatial = ReceiptSpatialParser.parse(document);
|
||||
if (_shouldFallbackToText(spatial)) {
|
||||
return _withImageDimensions(
|
||||
ReceiptTextParser.parseReceipt(document.flattenedText),
|
||||
document,
|
||||
);
|
||||
}
|
||||
return spatial;
|
||||
}
|
||||
|
||||
static bool _shouldFallbackToText(ReceiptParseResult spatial) {
|
||||
if (spatial.items.isEmpty) return true;
|
||||
final flagged =
|
||||
spatial.items.where((item) => item.needsManualReview).length;
|
||||
if (flagged > 0) return true;
|
||||
if (spatial.items.any((item) => item.price <= 0)) return true;
|
||||
if (spatial.receiptTotal != null && !spatial.validationPassed) return true;
|
||||
return false;
|
||||
}
|
||||
|
||||
static ReceiptParseResult _withImageDimensions(
|
||||
ReceiptParseResult result,
|
||||
OcrDocument document,
|
||||
) {
|
||||
return ReceiptParseResult(
|
||||
items: result.items,
|
||||
store: result.store,
|
||||
receiptTotal: result.receiptTotal,
|
||||
totalDiscount: result.totalDiscount,
|
||||
validationPassed: result.validationPassed,
|
||||
expectedItemCount: result.expectedItemCount,
|
||||
ocrImageWidth: document.imageWidth,
|
||||
ocrImageHeight: document.imageHeight,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user