-
Notifications
You must be signed in to change notification settings - Fork 1
/
scp.go
173 lines (149 loc) · 4.19 KB
/
scp.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
/*
Package scp provides access to the Super Check Partial (http://www.supercheckpartial.com)
database stored in the SCP format. The package also provides functions to download,
store and update a MASTER.SCP file. The default remote location for the MASTER.SCP file
is http://www.supercheckpartial.com/MASTER.SCP.
File Format Description
1. The file is in plain text format (ASCII).
2. Each line contains one callsign.
3. Lines that begin with # are comments that can be ignored.
*/
package scp
import (
"bufio"
"io"
"sort"
"strings"
"sync"
)
// DefaultURL is the original URL of the MASTER.SCP file: http://www.supercheckpartial.com/MASTER.SCP
const DefaultURL = "http://www.supercheckpartial.com/MASTER.SCP"
// DefaultLocalFilename is the default name for the file that is used to store the contents of MASTER.SCP locally in the user's home directory.
const DefaultLocalFilename = ".config/hamradio/MASTER.SCP"
// Database represents the SCP database.
type Database struct {
items map[byte]entrySet
}
var SCPFormat = EntryParserFunc(func(line string) (Entry, bool) {
if strings.HasPrefix(line, "#") {
return Entry{}, false
}
return newEntry(line, nil), true
})
type Match struct {
Entry
distance distance
accuracy accuracy
Assembly MatchingAssembly
}
// LessThan returns true if this match is less than the other based on the default ordering for matches (the better the lesser).
func (m Match) LessThan(o Match) bool {
if m.accuracy != o.accuracy {
return m.accuracy > o.accuracy
}
mLongestPart := m.Assembly.LongestPart()
oLongestPart := o.Assembly.LongestPart()
if mLongestPart != oLongestPart {
return mLongestPart > oLongestPart
}
if len(m.key) != len(o.key) {
return len(m.key) < len(o.key)
}
return m.key < o.key
}
func (m Match) Accuracy() float64 {
return float64(m.accuracy)
}
// Read the database from a reader using the SCP format.
func ReadSCP(r io.Reader) (*Database, error) {
return Read(r, SCPFormat)
}
// Read the database from a reader unsing the given entry parser.
func Read(r io.Reader, parser EntryParser) (*Database, error) {
database := &Database{make(map[byte]entrySet)}
lines := bufio.NewScanner(r)
for lines.Scan() {
line := strings.TrimSpace(lines.Text())
if len(line) == 0 {
continue
}
entry, ok := parser.ParseEntry(line)
if !ok {
continue
}
for _, b := range entry.fingerprint {
es, ok := database.items[b]
if !ok {
es = entrySet{}
}
es.Add(entry)
database.items[b] = es
}
}
return database, nil
}
// FindStrings returns all strings in database that partially match the given string
func (database Database) FindStrings(s string) ([]string, error) {
allMatches, err := database.Find(s)
if err != nil {
return nil, err
}
result := make([]string, len(allMatches))
for i, m := range allMatches {
result[i] = m.key
}
return result, nil
}
// Find returns all entries in database that are similar to the given string.
func (database Database) Find(s string) ([]Match, error) {
if len(s) < 3 {
return nil, nil
}
source := newEntry(s, nil)
matches := make(chan Match, 100)
merged := make(chan []Match)
waiter := &sync.WaitGroup{}
byteMap := make(map[byte]bool)
for _, b := range source.fingerprint {
if byteMap[b] {
continue
}
byteMap[b] = true
entrySet, ok := database.items[b]
if !ok {
continue
}
waiter.Add(1)
go findMatches(matches, source, entrySet, waiter)
}
go collectMatches(merged, matches)
waiter.Wait()
close(matches)
result := <-merged
close(merged)
return result, nil
}
func findMatches(matches chan<- Match, input Entry, entries entrySet, waiter *sync.WaitGroup) {
defer waiter.Done()
const accuracyThreshold = 0.65
entries.Do(func(e Entry) {
distance, accuracy, assembly := input.EditTo(e)
if accuracy >= accuracyThreshold {
matches <- Match{e, distance, accuracy, assembly}
}
})
}
func collectMatches(result chan<- []Match, matches <-chan Match) {
allMatches := make([]Match, 0)
matchSet := make(map[string]Match)
for match := range matches {
if _, ok := matchSet[match.key]; !ok {
matchSet[match.key] = match
allMatches = append(allMatches, match)
}
}
sort.Slice(allMatches, func(i, j int) bool {
return allMatches[i].LessThan(allMatches[j])
})
result <- allMatches
}