Skip to content

Commit

Permalink
Add OCRConfidence
Browse files Browse the repository at this point in the history
  • Loading branch information
hupe1980 committed Jan 6, 2024
1 parent ea0841c commit f39d320
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 0 deletions.
20 changes: 20 additions & 0 deletions table.go
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,26 @@ func (tr *TableRow) Cells() []Cell {
return tr.cells
}

// OCRConfidence returns the OCR confidence for the table row.
func (tr *TableRow) OCRConfidence() *OCRConfidence {
meanValues := make([]float64, 0, len(tr.cells))
maxValues := make([]float64, 0, len(tr.cells))
minValues := make([]float64, 0, len(tr.cells))

for _, cell := range tr.cells {
c := cell.OCRConfidence()
meanValues = append(meanValues, c.Mean())
maxValues = append(maxValues, c.Max())
minValues = append(minValues, c.Min())
}

return &OCRConfidence{
mean: internal.Mean(meanValues),
max: slices.Max(maxValues),
min: slices.Min(minValues),
}
}

type RowsOptions struct {
IgnoreMergedCells bool
}
Expand Down
43 changes: 43 additions & 0 deletions table_cell.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ type Cell interface {
Words() []*Word
Text(optFns ...func(*TextLinearizationOptions)) string
Confidence() float64
OCRConfidence() *OCRConfidence
IsColumnHeader() bool
IsTableTitle() bool
IsTableFooter() bool
Expand Down Expand Up @@ -51,6 +52,26 @@ func (tmc *TableMergedCell) Text(_ ...func(*TextLinearizationOptions)) string {
return strings.Join(texts, " ")
}

// OCRConfidence returns the OCR confidence for the merged cell.
func (tmc *TableMergedCell) OCRConfidence() *OCRConfidence {
meanValues := make([]float64, 0, len(tmc.cells))
maxValues := make([]float64, 0, len(tmc.cells))
minValues := make([]float64, 0, len(tmc.cells))

for _, cell := range tmc.cells {
c := cell.OCRConfidence()
meanValues = append(meanValues, c.Mean())
maxValues = append(maxValues, c.Max())
minValues = append(minValues, c.Min())
}

return &OCRConfidence{
mean: internal.Mean(meanValues),
max: slices.Max(maxValues),
min: slices.Min(minValues),
}
}

// TableCell represents a cell in a table.
type TableCell struct {
cell
Expand Down Expand Up @@ -82,6 +103,28 @@ func (tc *TableCell) Text(optFns ...func(*TextLinearizationOptions)) string {
return strings.Join(texts, " ")
}

// OCRConfidence returns the OCR confidence for the table cell.
func (tc *TableCell) OCRConfidence() *OCRConfidence {
if tc.selectionElement != nil {
return &OCRConfidence{
mean: tc.SelectionElement().Confidence(),
min: tc.SelectionElement().Confidence(),
max: tc.SelectionElement().Confidence(),
}
}

scores := make([]float64, len(tc.words))
for i, w := range tc.words {
scores[i] = w.Confidence()
}

return &OCRConfidence{
mean: internal.Mean(scores),
max: slices.Max(scores),
min: slices.Min(scores),
}
}

// cell represents the base information shared among different types of table cells.
type cell struct {
base
Expand Down
15 changes: 15 additions & 0 deletions table_cell_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,4 +77,19 @@ func TestTableCell(t *testing.T) {
result := cell.Text()
assert.Equal(t, "Hello World", result, "Text mismatch")
})

t.Run("OCRConfindence", func(t *testing.T) {
// Create sample words
word1 := &Word{base: base{confidence: 0.9}}
word2 := &Word{base: base{confidence: 0.1}}

// Create a TableCell with sample words
cell := &TableCell{words: []*Word{word1, word2}}

// Call the OCRConfidence method and check if it returns the expected scores
result := cell.OCRConfidence()
assert.Equal(t, 0.5, result.Mean())
assert.Equal(t, 0.1, result.Min())
assert.Equal(t, 0.9, result.Max())
})
}

0 comments on commit f39d320

Please sign in to comment.