-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparser.go
More file actions
229 lines (202 loc) · 7.08 KB
/
Copy pathparser.go
File metadata and controls
229 lines (202 loc) · 7.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
package parserails
import (
"context"
"fmt"
"strings"
"time"
pdfium "github.com/klippa-app/go-pdfium"
"github.com/klippa-app/go-pdfium/references"
"github.com/klippa-app/go-pdfium/requests"
"github.com/klippa-app/go-pdfium/responses"
)
// Granularity controls how text is segmented into Words.
type Granularity int
const (
// GranularityWord groups individual characters into words split on
// whitespace; each Word's box is the union of its characters' boxes. This is
// the most precise mode and the default.
GranularityWord Granularity = iota
// GranularityLine returns PDFium's text rectangles (typically line or run
// fragments) directly. It crosses the Go↔WASM boundary far fewer times, so
// it is dramatically faster and lighter — at the cost of per-word precision.
// Use it when line-level boxes are enough (e.g. RAG chunking, search).
GranularityLine
)
// Parser extracts spatial text from documents using a pooled PDFium runtime.
//
// It is safe for concurrent use: each Parse call borrows an instance from the
// pool and returns it when done. Create one Parser per process and reuse it.
type Parser struct {
pool pdfium.Pool
ocr OCR
granularity Granularity
fontInfo bool
sofficeBin string
}
// Option configures a Parser.
type Option func(*config)
type config struct {
minIdle, maxIdle, maxTotal int
ocr OCR
granularity Granularity
fontInfo bool
sofficeBin string
}
// WithOCR sets the OCR backend used as a fallback for pages with no extractable
// text. By default no OCR is performed.
func WithOCR(o OCR) Option { return func(c *config) { c.ocr = o } }
// WithGranularity selects how text is segmented into Words (default
// GranularityWord).
func WithGranularity(g Granularity) Option { return func(c *config) { c.granularity = g } }
// WithFontInfo collects per-character font size. It is off by default because it
// roughly doubles extraction cost. Only meaningful with GranularityWord.
func WithFontInfo() Option { return func(c *config) { c.fontInfo = true } }
// WithPoolSize tunes the underlying PDFium worker pool.
func WithPoolSize(minIdle, maxIdle, maxTotal int) Option {
return func(c *config) { c.minIdle, c.maxIdle, c.maxTotal = minIdle, maxIdle, maxTotal }
}
// WithLibreOffice sets an explicit LibreOffice binary path used to convert
// office documents (DOCX/PPTX/XLSX/...) to PDF. By default ParseRails looks for
// the PARSERAILS_SOFFICE env var, then "soffice"/"libreoffice" on PATH.
func WithLibreOffice(path string) Option { return func(c *config) { c.sofficeBin = path } }
// New initializes a Parser backed by a pure-Go PDFium WebAssembly runtime.
// No cgo and no system libraries are required. Call Close when finished.
func New(opts ...Option) (*Parser, error) {
cfg := config{minIdle: 1, maxIdle: 2, maxTotal: 4, ocr: noOCR{}}
for _, opt := range opts {
opt(&cfg)
}
pool, err := newPool(poolConfig{minIdle: cfg.minIdle, maxIdle: cfg.maxIdle, maxTotal: cfg.maxTotal})
if err != nil {
return nil, fmt.Errorf("parserails: init pdfium pool: %w", err)
}
return &Parser{
pool: pool,
ocr: cfg.ocr,
granularity: cfg.granularity,
fontInfo: cfg.fontInfo,
sofficeBin: cfg.sofficeBin,
}, nil
}
// Close releases the PDFium runtime and all pooled workers.
func (p *Parser) Close() error { return p.pool.Close() }
// Parse extracts every page's words with bounding boxes from the given PDF data.
func (p *Parser) Parse(ctx context.Context, data []byte) (*Document, error) {
inst, err := p.pool.GetInstance(30 * time.Second)
if err != nil {
return nil, fmt.Errorf("parserails: acquire instance: %w", err)
}
defer func() { _ = inst.Close() }()
doc, err := inst.OpenDocument(&requests.OpenDocument{File: &data})
if err != nil {
return nil, fmt.Errorf("parserails: open document: %w", err)
}
defer func() {
_, _ = inst.FPDF_CloseDocument(&requests.FPDF_CloseDocument{Document: doc.Document})
}()
count, err := inst.FPDF_GetPageCount(&requests.FPDF_GetPageCount{Document: doc.Document})
if err != nil {
return nil, fmt.Errorf("parserails: page count: %w", err)
}
out := &Document{Pages: make([]Page, 0, count.PageCount)}
for i := 0; i < count.PageCount; i++ {
if err := ctx.Err(); err != nil {
return nil, err
}
page, err := p.parsePage(ctx, inst, doc.Document, i)
if err != nil {
return nil, err
}
out.Pages = append(out.Pages, page)
}
return out, nil
}
func (p *Parser) parsePage(ctx context.Context, inst pdfium.Pdfium, docRef references.FPDF_DOCUMENT, index int) (Page, error) {
pageReq := requests.Page{ByIndex: &requests.PageByIndex{Document: docRef, Index: index}}
size, err := inst.GetPageSize(&requests.GetPageSize{Page: pageReq})
if err != nil {
return Page{}, fmt.Errorf("parserails: page %d size: %w", index, err)
}
page := Page{Index: index, Width: size.Width, Height: size.Height}
mode := requests.GetPageTextStructuredModeChars
if p.granularity == GranularityLine {
mode = requests.GetPageTextStructuredModeRects
}
text, err := inst.GetPageTextStructured(&requests.GetPageTextStructured{
Page: pageReq,
Mode: mode,
CollectFontInformation: p.fontInfo && p.granularity == GranularityWord,
})
if err != nil {
return Page{}, fmt.Errorf("parserails: page %d text: %w", index, err)
}
if p.granularity == GranularityLine {
page.Words = wordsFromRects(text.Rects, index)
} else {
page.Words = wordsFromChars(text.Chars, index)
}
// Scanned/image-only page: no extractable text. Fall back to OCR if set.
if len(page.Words) == 0 {
ocrWords, err := p.ocrPage(ctx, inst, pageReq, pageSize{Width: size.Width, Height: size.Height})
if err != nil {
return Page{}, err
}
page.Words = ocrWords
}
return page, nil
}
// wordsFromChars groups characters into words split on whitespace; each word's
// box is the union of its characters' boxes.
func wordsFromChars(chars []*responses.GetPageTextStructuredChar, page int) []Word {
words := make([]Word, 0, len(chars)/4+1)
var (
buf strings.Builder
cur Word
open bool
)
flush := func() {
if open && buf.Len() > 0 {
cur.Text = buf.String()
words = append(words, cur)
}
buf.Reset()
open = false
}
for _, c := range chars {
if strings.TrimSpace(c.Text) == "" {
flush()
continue
}
pos := c.PointPosition
if !open {
cur = Word{Page: page, X0: pos.Left, Y0: pos.Bottom, X1: pos.Right, Y1: pos.Top}
if c.FontInformation != nil {
cur.FontSize = c.FontInformation.Size
}
open = true
} else {
cur.X0 = min(cur.X0, pos.Left)
cur.Y0 = min(cur.Y0, pos.Bottom)
cur.X1 = max(cur.X1, pos.Right)
cur.Y1 = max(cur.Y1, pos.Top)
}
buf.WriteString(c.Text)
}
flush()
return words
}
// wordsFromRects maps PDFium text rectangles directly to Words.
func wordsFromRects(rects []*responses.GetPageTextStructuredRect, page int) []Word {
out := make([]Word, 0, len(rects))
for _, r := range rects {
if strings.TrimSpace(r.Text) == "" {
continue
}
p := r.PointPosition
out = append(out, Word{
Text: r.Text, Page: page,
X0: p.Left, Y0: p.Bottom, X1: p.Right, Y1: p.Top,
})
}
return out
}