mirror of
https://github.com/infiniflow/ragflow.git
synced 2026-07-14 17:08:31 +08:00
Go: format code (#16813)
### Summary Aligned to EE Signed-off-by: Jin Hai <haijin.chn@gmail.com>
This commit is contained in:
@@ -66,8 +66,10 @@ func (p *Parser) ocrDetectAndRecognize(ctx context.Context, pageImg image.Image,
|
||||
for _, t := range texts {
|
||||
if strings.TrimSpace(t.Text) != "" {
|
||||
result = append(result, pdf.TextBox{
|
||||
X0: px0, X1: px1,
|
||||
Top: py0, Bottom: py1,
|
||||
X0: px0,
|
||||
X1: px1,
|
||||
Top: py0,
|
||||
Bottom: py1,
|
||||
Text: t.Text,
|
||||
PageNumber: pageNum,
|
||||
})
|
||||
|
||||
@@ -1,4 +1,3 @@
|
||||
|
||||
package table
|
||||
|
||||
import (
|
||||
@@ -17,7 +16,7 @@ func TestGroupBoxesByRC_RDiffSplitsRows(t *testing.T) {
|
||||
{X0: 210, X1: 290, Top: 0, Bottom: 30, Text: "C", R: 2, C: 2},
|
||||
{X0: 10, X1: 90, Top: 35, Bottom: 65, Text: "D", R: 3, C: 0},
|
||||
{X0: 110, X1: 190, Top: 35, Bottom: 65, Text: "E", R: 4, C: 1},
|
||||
{X0: 210, X1: 290, Top: 35, Bottom: 65, Text: "F", R: 5, C: 2},
|
||||
{X0: 210, X1: 290, Top: 35, Bottom: 65, Text: "F", R: 5, C: 2},
|
||||
}
|
||||
rows := GroupBoxesByRC(boxes)
|
||||
// R=0,1,2,3,4,5 → 6 rows (Python: R differs → new row).
|
||||
|
||||
@@ -19,9 +19,13 @@ func RowsToHTML(rows [][]pdf.TSRCell, caption string, headerRows map[int]bool, s
|
||||
for ri, row := range rows {
|
||||
b.WriteString("<tr>")
|
||||
for ci, cell := range row {
|
||||
if covered[[2]int{ri, ci}] { continue }
|
||||
if covered[[2]int{ri, ci}] {
|
||||
continue
|
||||
}
|
||||
tag := "td"
|
||||
if headerRows[ri] { tag = "th" }
|
||||
if headerRows[ri] {
|
||||
tag = "th"
|
||||
}
|
||||
b.WriteString("<")
|
||||
b.WriteString(tag)
|
||||
sp := ""
|
||||
@@ -30,7 +34,9 @@ func RowsToHTML(rows [][]pdf.TSRCell, caption string, headerRows map[int]bool, s
|
||||
sp = fmt.Sprintf("colspan=%d", s[0])
|
||||
}
|
||||
if s[1] > 1 {
|
||||
if sp != "" { sp += " " }
|
||||
if sp != "" {
|
||||
sp += " "
|
||||
}
|
||||
sp += fmt.Sprintf("rowspan=%d", s[1])
|
||||
}
|
||||
}
|
||||
@@ -59,17 +65,23 @@ func SimpleRowsToHTML(rows [][]string) string {
|
||||
}
|
||||
nCols := 0
|
||||
for _, row := range rows {
|
||||
if len(row) > nCols { nCols = len(row) }
|
||||
if len(row) > nCols {
|
||||
nCols = len(row)
|
||||
}
|
||||
}
|
||||
var b strings.Builder
|
||||
b.WriteString("<table>")
|
||||
for ri, row := range rows {
|
||||
b.WriteString("<tr>")
|
||||
tag := "td"
|
||||
if ri == 0 { tag = "th" }
|
||||
if ri == 0 {
|
||||
tag = "th"
|
||||
}
|
||||
for ci := 0; ci < nCols; ci++ {
|
||||
text := ""
|
||||
if ci < len(row) { text = row[ci] }
|
||||
if ci < len(row) {
|
||||
text = row[ci]
|
||||
}
|
||||
b.WriteString("<")
|
||||
b.WriteString(tag)
|
||||
b.WriteString(" >")
|
||||
@@ -98,7 +110,9 @@ func RowsToStrings(rows [][]pdf.TSRCell) [][]string {
|
||||
func HasText(rows [][]pdf.TSRCell) bool {
|
||||
for _, row := range rows {
|
||||
for _, c := range row {
|
||||
if strings.TrimSpace(c.Text) != "" { return true }
|
||||
if strings.TrimSpace(c.Text) != "" {
|
||||
return true
|
||||
}
|
||||
}
|
||||
}
|
||||
return false
|
||||
@@ -106,7 +120,9 @@ func HasText(rows [][]pdf.TSRCell) bool {
|
||||
|
||||
func HasAnyText(cells []pdf.TSRCell) bool {
|
||||
for _, c := range cells {
|
||||
if strings.TrimSpace(c.Text) != "" { return true }
|
||||
if strings.TrimSpace(c.Text) != "" {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
@@ -1,4 +1,3 @@
|
||||
|
||||
package table
|
||||
|
||||
import (
|
||||
|
||||
@@ -1,4 +1,3 @@
|
||||
|
||||
package table
|
||||
|
||||
import (
|
||||
@@ -125,4 +124,3 @@ func MergeTablesAcrossPages(tables []pdf.TableItem, medianHeights map[int]float6
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
|
||||
@@ -12,7 +12,9 @@ import (
|
||||
func CalSpans(rows [][]pdf.TSRCell) (map[[2]int][2]int, map[[2]int]bool) {
|
||||
spanInfo := make(map[[2]int][2]int)
|
||||
covered := make(map[[2]int]bool)
|
||||
if len(rows) == 0 || len(rows[0]) == 0 { return spanInfo, covered }
|
||||
if len(rows) == 0 || len(rows[0]) == 0 {
|
||||
return spanInfo, covered
|
||||
}
|
||||
|
||||
// Compute column center positions.
|
||||
nCols := len(rows[0])
|
||||
@@ -32,40 +34,64 @@ func CalSpans(rows [][]pdf.TSRCell) (map[[2]int][2]int, map[[2]int]bool) {
|
||||
|
||||
for i, row := range rows {
|
||||
for j, cell := range row {
|
||||
if j >= nCols { continue }
|
||||
if j >= nCols {
|
||||
continue
|
||||
}
|
||||
// Exclude spanning cells from column/row boundary calculations.
|
||||
// Use label-based detection (O(1), no dependency on column midpoints).
|
||||
if strings.Contains(cell.Label, "spanning") { continue }
|
||||
if cell.X0 < colLeft[j] { colLeft[j] = cell.X0 }
|
||||
if cell.X1 > colRight[j] { colRight[j] = cell.X1 }
|
||||
if cell.Y0 < rowTop[i] { rowTop[i] = cell.Y0 }
|
||||
if cell.Y1 > rowBott[i] { rowBott[i] = cell.Y1 }
|
||||
if strings.Contains(cell.Label, "spanning") {
|
||||
continue
|
||||
}
|
||||
if cell.X0 < colLeft[j] {
|
||||
colLeft[j] = cell.X0
|
||||
}
|
||||
if cell.X1 > colRight[j] {
|
||||
colRight[j] = cell.X1
|
||||
}
|
||||
if cell.Y0 < rowTop[i] {
|
||||
rowTop[i] = cell.Y0
|
||||
}
|
||||
if cell.Y1 > rowBott[i] {
|
||||
rowBott[i] = cell.Y1
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// For each spanning cell, compute how many cols/rows it covers.
|
||||
for i, row := range rows {
|
||||
for j, cell := range row {
|
||||
if j >= nCols || covered[[2]int{i,j}] { continue }
|
||||
if j >= nCols || covered[[2]int{i, j}] {
|
||||
continue
|
||||
}
|
||||
// Skip cells without position data (they can't span).
|
||||
if cell.X0 == 0 && cell.X1 == 0 && cell.Y0 == 0 && cell.Y1 == 0 { continue }
|
||||
if cell.X0 == 0 && cell.X1 == 0 && cell.Y0 == 0 && cell.Y1 == 0 {
|
||||
continue
|
||||
}
|
||||
cs, rs := 1, 1
|
||||
// Count columns whose center is inside this cell's X range.
|
||||
for k := j+1; k < nCols; k++ {
|
||||
for k := j + 1; k < nCols; k++ {
|
||||
// Skip columns with no non-spanning cells (initial values unchanged).
|
||||
if colLeft[k] == 1e9 && colRight[k] == -1e9 { continue }
|
||||
if colLeft[k] == 1e9 && colRight[k] == -1e9 {
|
||||
continue
|
||||
}
|
||||
colCenter := (colLeft[k] + colRight[k]) / 2
|
||||
if colCenter >= cell.X0 && colCenter <= cell.X1 { cs++ }
|
||||
if colCenter >= cell.X0 && colCenter <= cell.X1 {
|
||||
cs++
|
||||
}
|
||||
}
|
||||
// Count rows whose center is inside this cell's Y range.
|
||||
for k := i+1; k < nRows; k++ {
|
||||
for k := i + 1; k < nRows; k++ {
|
||||
// Skip rows with no non-spanning cells.
|
||||
if rowTop[k] == 1e9 && rowBott[k] == -1e9 { continue }
|
||||
if rowTop[k] == 1e9 && rowBott[k] == -1e9 {
|
||||
continue
|
||||
}
|
||||
rowCenter := (rowTop[k] + rowBott[k]) / 2
|
||||
if rowCenter >= cell.Y0 && rowCenter <= cell.Y1 { rs++ }
|
||||
if rowCenter >= cell.Y0 && rowCenter <= cell.Y1 {
|
||||
rs++
|
||||
}
|
||||
}
|
||||
if cs > 1 || rs > 1 {
|
||||
spanInfo[[2]int{i,j}] = [2]int{cs, rs}
|
||||
spanInfo[[2]int{i, j}] = [2]int{cs, rs}
|
||||
// Mark covered cells.
|
||||
for ri := i; ri < i+rs && ri < nRows; ri++ {
|
||||
for cj := j; cj < j+cs && cj < nCols; cj++ {
|
||||
@@ -83,8 +109,12 @@ func CalSpans(rows [][]pdf.TSRCell) (map[[2]int][2]int, map[[2]int]bool) {
|
||||
// flattenGrid flattens a 2D grid into a 1D slice for fillCellTextFromBoxes.
|
||||
func FlattenGrid(grid [][]pdf.TSRCell) []pdf.TSRCell {
|
||||
n := 0
|
||||
for _, row := range grid { n += len(row) }
|
||||
for _, row := range grid {
|
||||
n += len(row)
|
||||
}
|
||||
flat := make([]pdf.TSRCell, 0, n)
|
||||
for _, row := range grid { flat = append(flat, row...) }
|
||||
for _, row := range grid {
|
||||
flat = append(flat, row...)
|
||||
}
|
||||
return flat
|
||||
}
|
||||
|
||||
@@ -1,4 +1,3 @@
|
||||
|
||||
package table
|
||||
|
||||
import (
|
||||
|
||||
Reference in New Issue
Block a user