-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsearch.go
158 lines (138 loc) · 4.02 KB
/
search.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
package main
import (
"flag"
"fmt"
"os"
"strings"
"sync"
"text/tabwriter"
"github.com/TuftsBCB/fragbag/bow"
"github.com/TuftsBCB/fragbag/bowdb"
"github.com/TuftsBCB/tools/util"
)
var (
flagSearchOpts = bowdb.SearchDefault
flagSearchOutFmt = "plain"
flagSearchSort = "cosine"
flagSearchDesc = false
)
var cmdSearch = &command{
name: "search",
positionalUsage: "bowdb-path bower-file [ bower-file ... ]",
shortHelp: "search a BOW database",
help: `
The search command searches the given BOW database for entries closest to the
bower files given. The fragment library used to compute BOWs for the queries
is the one contained inside the given BOW database.
`,
flags: flag.NewFlagSet("search", flag.ExitOnError),
run: search,
addFlags: func(c *command) {
c.flags.StringVar(&flagSearchOutFmt, "outfmt", flagSearchOutFmt,
"The output format of the search results. Valid values are\n"+
"'plain' and 'csv'.")
c.flags.IntVar(&flagSearchOpts.Limit, "limit", flagSearchOpts.Limit,
"The maximum number of search results to return.\n"+
"To specify no limit, set this to -1.")
c.flags.Float64Var(&flagSearchOpts.Min, "min", flagSearchOpts.Min,
"All search results will have at least this distance with the "+
"query.")
c.flags.Float64Var(&flagSearchOpts.Max, "max", flagSearchOpts.Max,
"All search results will have at most this distance with the "+
"query.")
c.flags.StringVar(&flagSearchSort, "sort", flagSearchSort,
"The field to sort search results by.\n"+
"Valid values are 'cosine' and 'euclid'.")
c.flags.BoolVar(&flagSearchDesc, "desc", flagSearchDesc,
"When set, results will be shown in descending order.")
},
}
func search(c *command) {
c.assertLeastNArg(2)
// Some search options don't translate directly to command line parameters
// specified by the flag package.
if flagSearchDesc {
flagSearchOpts.Order = bowdb.OrderDesc
}
switch flagSearchSort {
case "cosine":
flagSearchOpts.SortBy = bowdb.SortByCosine
case "euclid":
flagSearchOpts.SortBy = bowdb.SortByEuclid
default:
util.Fatalf("Unknown sort field '%s'.", flagSearchSort)
}
db := util.OpenBowDB(c.flags.Arg(0))
bowPaths := c.flags.Args()[1:]
_, err := db.ReadAll()
util.Assert(err, "Could not read BOW database entries")
// always hide the progress bar here.
bows := util.ProcessBowers(bowPaths, db.Lib, false, flagCpu, true)
out, outDone := outputter()
// launch goroutines to search queries in parallel
wgSearch := new(sync.WaitGroup)
for i := 0; i < flagCpu; i++ {
wgSearch.Add(1)
go func() {
defer wgSearch.Done()
for b := range bows {
sr := db.Search(flagSearchOpts, b)
out <- searchResult{b, sr}
}
}()
}
wgSearch.Wait()
close(out)
<-outDone
util.Assert(db.Close())
}
type searchResult struct {
query bow.Bowed
results []bowdb.SearchResult
}
func outputter() (chan searchResult, chan struct{}) {
out := make(chan searchResult)
done := make(chan struct{})
go func() {
if flagSearchOutFmt == "csv" {
fmt.Printf("QueryID\tHitID\tCosine\tEuclid\n")
}
first := true
for sr := range out {
switch flagSearchOutFmt {
case "plain":
outputPlain(sr, first)
case "csv":
outputCsv(sr, first)
default:
util.Fatalf("Invalid output format '%s'.", flagSearchOutFmt)
}
first = false
}
done <- struct{}{}
}()
return out, done
}
func outputPlain(sr searchResult, first bool) {
w := tabwriter.NewWriter(os.Stdout, 5, 0, 4, ' ', 0)
wf := func(format string, v ...interface{}) {
fmt.Fprintf(w, format, v...)
}
if !first {
fmt.Println(strings.Repeat("-", 80))
}
header := fmt.Sprintf("%s (%d hits)", sr.query.Id, len(sr.results))
fmt.Println(header)
fmt.Println(strings.Repeat("-", len(header)))
wf("Hit\tCosine\tEuclid\n")
for _, result := range sr.results {
wf("%s\t%0.4f\t%0.4f\n", result.Bowed.Id, result.Cosine, result.Euclid)
}
w.Flush()
}
func outputCsv(sr searchResult, first bool) {
for _, result := range sr.results {
fmt.Printf("%s\t%s\t%0.4f\t%0.4f\n",
sr.query.Id, result.Bowed.Id, result.Cosine, result.Euclid)
}
}