-
Notifications
You must be signed in to change notification settings - Fork 4
/
pdf_search_demo.go
194 lines (168 loc) · 6.25 KB
/
pdf_search_demo.go
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
// Copyright 2019 PaperCut Software International Pty Ltd. All rights reserved.
package main
import (
"flag"
"fmt"
"os"
"path/filepath"
"strings"
"time"
"github.com/papercutsoftware/pdfsearch"
"github.com/papercutsoftware/pdfsearch/examples/cmd_utils"
)
const usage = `Usage: go run pdf_search_demo.go [OPTIONS] -f "pcng-manual*.pdf" PaperCut NG
Performs a full text search for "PaperCut NG" in PDFs that match "pcng-manual*.pdf".
`
func main() {
var pathPattern string
persistDir := filepath.Join(pdfsearch.DefaultPersistRoot, "pdf_search_demo")
var reuse bool
var nameOnly bool
maxSearchResults := 10
outPath := "search.results.pdf"
outDir := "search.history"
flag.StringVar(&pathPattern, "f", pathPattern, "PDF(s) to index.")
flag.StringVar(&outPath, "o", outPath, "Name of PDF that will show marked up results.")
flag.StringVar(&persistDir, "s", persistDir, "The on-disk index is stored here.")
flag.BoolVar(&reuse, "r", reuse, "Reused stored index on disk for the last -p run.")
flag.BoolVar(&nameOnly, "l", nameOnly, "Show matching file names only.")
flag.IntVar(&maxSearchResults, "n", maxSearchResults, "Max number of search results to return.")
cmd_utils.MakeUsage(usage)
flag.Parse()
pdfsearch.InitLogging()
if len(flag.Args()) < 1 {
flag.Usage()
os.Exit(1)
}
// We always want to see all errors in our testing.
pdfsearch.ExposeErrors()
// The term to search for.
term := strings.Join(flag.Args(), " ")
// File extension based on term.
termExt := strings.Join(flag.Args(), ".")
maxResults := maxSearchResults
if nameOnly {
maxResults = 1e9
}
// Read the files to index into `pathList`.
var err error
var pathList []string
if !reuse {
pathList, err = cmd_utils.PatternsToPaths([]string{pathPattern})
if err != nil {
fmt.Fprintf(os.Stderr, "PatternsToPaths failed. args=%#q err=%v\n", flag.Args(), err)
os.Exit(1)
}
if len(pathList) < 1 {
fmt.Fprintf(os.Stderr, "No files matching %q.\n", pathPattern)
os.Exit(1)
}
}
pathList = cmd_utils.PartShuffle(pathList)
// Run the tests.
if err := runIndexSearchShow(pathList, term, persistDir, reuse, nameOnly, maxResults, outPath); err != nil {
fmt.Fprintf(os.Stderr, "runIndexSearchShow failed. err=%v\n", err)
os.Exit(1)
}
// Save a copy of the marked up file for posterity.
if err := pdfsearch.CopyMarkedupResults(outDir, outPath, pathPattern, termExt); err != nil {
fmt.Fprintf(os.Stderr, "copyMarkedupResults failed. err=%v\n", err)
os.Exit(1)
}
}
// runIndexSearchShow creates a pdfsearch.PdfIndex for the PDFs in `pathList`, searches for
// `term` in this index, and shows the results.
// It also creates a marked-up PDF containing the original PDF pages with the matched terms marked
// and saves it to `outPath`.
//
// `persistDir`: The directory the pdfsearch.PdfIndex is saved in.
// `reuse`: Don't create a pdfsearch.PdfIndex. Reuse one that was previously persisted to disk.
// `nameOnly`: Show matching file names only.
// `maxResults`: Max number of search results to return.
func runIndexSearchShow(pathList []string, term, persistDir string, reuse, nameOnly bool,
maxResults int, outPath string) error {
pdfIndex, results, dt, dtIndex, err := runIndexSearch(pathList, term, persistDir, reuse, maxResults)
if err != nil {
return err
}
return showResults(pathList, pdfIndex, results, dt, dtIndex, nameOnly, maxResults, outPath)
}
// runIndexSearch creates a pdfsearch.PdfIndex for the PDFs in `pathList`, searches for `term`
// in this index and returns the pdfsearch.PdfIndex, the search results and the indexing and search
// durations.
// This is the main function. It shows you how to create an index annd search it.
//
// `persistDir`: The directory the pdfsearch.PdfIndex is saved.
// `reuse`: Don't create a pdfsearch.PdfIndex. Reuse one that was previously persisted to disk.
// `maxResults`: Max number of search results to return.
func runIndexSearch(pathList []string, term, persistDir string, reuse bool, maxResults int) (
pdfIndex pdfsearch.PdfIndex, results pdfsearch.PdfMatchSet, dt, dtIndex time.Duration, err error) {
t0 := time.Now()
if reuse {
pdfIndex = pdfsearch.ReuseIndex(persistDir)
} else {
pdfIndex, err = pdfsearch.IndexPdfFiles(pathList, persistDir, report)
if err != nil {
return pdfIndex, results, dt, dtIndex, err
}
}
dtIndex = time.Since(t0)
results, err = pdfIndex.Search(term, maxResults)
if err != nil {
return pdfIndex, results, dt, dtIndex, err
}
dt = time.Since(t0)
return pdfIndex, results, dt, dtIndex, nil
}
// showResults writes a report on `results`, some search results (for a term that we don't show
// here) on `pdfIndex` that was build from the PDFs in `pathList`.
// It also creates a marked-up PDF containing the original PDF pages with the matched terms marked
// and saves it to `outPath`.
//
// `dt` and `dtSearch` are the durations of the indexing + search, and for indexing only.
// `reuse`: Don't create a pdfsearch.PdfIndex. Reuse one that was previously persisted to disk.
// `nameOnly`: Show matching file names only.
// `maxResults`: Max number of search results to return.
func showResults(pathList []string, pdfIndex pdfsearch.PdfIndex, results pdfsearch.PdfMatchSet,
dt, dtIndex time.Duration, nameOnly bool, maxResults int, outPath string) error {
if nameOnly {
files := results.Files()
if len(files) > maxResults {
files = files[:maxResults]
}
for i, fn := range files {
fmt.Printf("%4d: %q\n", i, fn)
}
} else {
fmt.Printf("%+v\n", results)
}
if err := pdfsearch.MarkupPdfResults(results, outPath); err != nil {
return err
}
numPages := pdfIndex.NumPages()
pagesSec := 0.0
if dt.Seconds() >= 0.01 {
pagesSec = float64(numPages) / dt.Seconds()
}
showList := pathList
if len(showList) > 10 {
showList = showList[:10]
for i, fn := range showList {
showList[i] = filepath.Base(fn)
}
}
dtSearch := dt - dtIndex
fmt.Fprintf(os.Stderr, "Duration=%.1f sec (%.3f index + %.3f search) (%.1f pages/sec) "+
"%d pages in %d files %+v\n"+
"Index duration=%s\n"+
"Marked up search results in %q\n",
dt.Seconds(), dtIndex.Seconds(), dtSearch.Seconds(), pagesSec,
numPages, len(pathList), showList,
pdfIndex.Duration(),
outPath)
return nil
}
// `report` is called by IndexPdfMem to report progress.
func report(msg string) {
fmt.Fprintf(os.Stderr, ">> %s\n", msg)
}