-
Notifications
You must be signed in to change notification settings - Fork 36
/
db.go
498 lines (471 loc) · 14 KB
/
db.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
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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
package internal
import (
"archive/tar"
"bytes"
"encoding/csv"
"errors"
"fmt"
"index/suffixarray"
"io"
"log"
"os"
paths "path"
"regexp"
"sort"
"strings"
minhashlsh "github.com/ekzhu/minhash-lsh"
"github.com/sergi/go-diff/diffmatchpatch"
"github.com/go-enry/go-license-detector/v4/licensedb/filer"
"github.com/go-enry/go-license-detector/v4/licensedb/internal/assets"
"github.com/go-enry/go-license-detector/v4/licensedb/internal/fastlog"
"github.com/go-enry/go-license-detector/v4/licensedb/internal/normalize"
"github.com/go-enry/go-license-detector/v4/licensedb/internal/wmh"
)
// ErrUnknownLicenseID is raised if license identifier is not known.
// Probably you need to upgrade version of the SPDX.
var ErrUnknownLicenseID = errors.New("license id is not known")
var (
licenseReadmeMentionRe = regexp.MustCompile(
fmt.Sprintf("(?i)[^\\s]+/[^/\\s]*(%s)[^\\s]*",
strings.Join(licenseFileNames, "|")))
)
// database holds the license texts, their hashes and the hashtables to query for nearest
// neighbors.
type database struct {
debug bool
// license name -> text
licenseTexts map[string]string
// minimum license text length
minLicenseLength int
// official license URL -> id
idByURL map[string]string
// id -> license URLs
urlsByID map[string][]string
// id -> license name
nameByID map[string]string
// all URLs joined
urlRe *regexp.Regexp
// first line of each license OR-ed - used to split
firstLineRe *regexp.Regexp
// unique unigrams -> index
tokens map[string]int
// document frequencies of the unigrams, indexes match with `tokens`
docfreqs []int
// Weighted MinHash hashtables
lsh *minhashlsh.MinhashLSH
// turns a license text into a hash
hasher *wmh.WeightedMinHasher
// part of license short name (e,g, BSL-1.0) -> list of containing license names
nameShortSubstrings map[string][]substring
// number of substrings per short license name
nameShortSubstringSizes map[string]int
// part of license name (e,g, Boost Software License 1.0) -> list of containing license names
nameSubstrings map[string][]substring
// number of substrings per license name
nameSubstringSizes map[string]int
}
type substring struct {
value string
count int
}
const (
numHashes = 154
similarityThreshold = 0.75
)
// Length returns the number of registered licenses.
func (db database) Length() int {
return len(db.licenseTexts)
}
// VocabularySize returns the number of unique unigrams.
func (db database) VocabularySize() int {
return len(db.tokens)
}
func loadUrls(db *database) {
urlCSVBytes, err := assets.Asset("urls.csv")
if err != nil {
log.Fatalf("failed to load urls.csv from the assets: %v", err)
}
urlReader := csv.NewReader(bytes.NewReader(urlCSVBytes))
records, err := urlReader.ReadAll()
if err != nil || len(records) == 0 {
log.Fatalf("failed to parse urls.csv from the assets: %v", err)
}
db.idByURL = map[string]string{}
db.urlsByID = map[string][]string{}
urlReWriter := &bytes.Buffer{}
for i, record := range records {
db.idByURL[record[1]] = record[0]
db.urlsByID[record[0]] = append(db.urlsByID[record[0]], record[2]+record[1]) // schema+url
urlReWriter.Write([]byte(regexp.QuoteMeta(record[1])))
if i < len(records)-1 {
urlReWriter.WriteRune('|')
}
}
db.urlRe = regexp.MustCompile(urlReWriter.String())
}
func loadNames(db *database) {
namesBytes, err := assets.Asset("names.csv")
if err != nil {
log.Fatalf("failed to load banes.csv from the assets: %v", err)
}
namesReader := csv.NewReader(bytes.NewReader(namesBytes))
records, err := namesReader.ReadAll()
if err != nil || len(records) == 0 {
log.Fatalf("failed to parse names.csv from the assets: %v", err)
}
db.nameByID = map[string]string{}
db.nameSubstringSizes = map[string]int{}
db.nameSubstrings = map[string][]substring{}
for _, record := range records {
db.nameByID[record[0]] = record[1]
registerNameSubstrings(record[1], record[0], db.nameSubstringSizes, db.nameSubstrings)
}
}
func registerNameSubstrings(
name string, key string, sizes map[string]int, substrs map[string][]substring) {
parts := splitLicenseName(name)
sizes[key] = 0
for _, part := range parts {
if licenseReadmeRe.MatchString(part.value) {
continue
}
sizes[key]++
list := substrs[part.value]
if list == nil {
list = []substring{}
}
list = append(list, substring{value: key, count: part.count})
substrs[part.value] = list
}
}
// Load takes the licenses from the embedded storage, normalizes, hashes them and builds the
// LSH hashtables.
func loadLicenses() *database {
db := &database{}
if os.Getenv("LICENSE_DEBUG") != "" {
db.debug = true
}
loadUrls(db)
loadNames(db)
tarBytes, err := assets.Asset("licenses.tar")
if err != nil {
log.Fatalf("failed to load licenses.tar from the assets: %v", err)
}
tarStream := bytes.NewBuffer(tarBytes)
archive := tar.NewReader(tarStream)
db.licenseTexts = map[string]string{}
tokenFreqs := map[string]map[string]int{}
firstLineWriter := &bytes.Buffer{}
firstLineWriter.WriteString("(^|\\n)((.*licen[cs]e\\n\\n)|(")
for header, err := archive.Next(); err != io.EOF; header, err = archive.Next() {
if len(header.Name) <= 6 {
continue
}
key := header.Name[2 : len(header.Name)-4]
text := make([]byte, header.Size)
readSize, readErr := archive.Read(text)
if readErr != nil && readErr != io.EOF {
log.Fatalf("failed to load licenses.tar from the assets: %s: %v", header.Name, readErr)
}
if int64(readSize) != header.Size {
log.Fatalf("failed to load licenses.tar from the assets: %s: incomplete read", header.Name)
}
normedText := normalize.LicenseText(string(text), normalize.Moderate)
if db.minLicenseLength == 0 || db.minLicenseLength > len(normedText) {
db.minLicenseLength = len(normedText)
}
db.licenseTexts[key] = normedText
newLinePos := strings.Index(normedText, "\n")
if newLinePos >= 0 {
firstLineWriter.WriteString(regexp.QuoteMeta(normedText[:newLinePos]))
firstLineWriter.WriteRune('|')
}
normedText = normalize.Relax(normedText)
lines := strings.Split(normedText, "\n")
myUniqueTokens := map[string]int{}
tokenFreqs[key] = myUniqueTokens
for _, line := range lines {
tokens := strings.Split(line, " ")
for _, token := range tokens {
myUniqueTokens[token]++
}
}
}
if db.debug {
log.Println("Minimum license length:", db.minLicenseLength)
log.Println("Number of supported licenses:", len(db.licenseTexts))
}
firstLineWriter.Truncate(firstLineWriter.Len() - 1)
firstLineWriter.WriteString("))")
db.firstLineRe = regexp.MustCompile(firstLineWriter.String())
docfreqs := map[string]int{}
for _, tokens := range tokenFreqs {
for token := range tokens {
docfreqs[token]++
}
}
uniqueTokens := make([]string, len(docfreqs))
{
i := 0
for token := range docfreqs {
uniqueTokens[i] = token
i++
}
}
sort.Strings(uniqueTokens)
db.tokens = map[string]int{}
db.docfreqs = make([]int, len(uniqueTokens))
for i, token := range uniqueTokens {
db.tokens[token] = i
db.docfreqs[i] = docfreqs[token]
}
db.lsh = minhashlsh.NewMinhashLSH64(numHashes, similarityThreshold, len(tokenFreqs))
if db.debug {
k, l := db.lsh.Params()
log.Println("LSH:", k, l)
}
db.hasher = wmh.NewWeightedMinHasher(len(uniqueTokens), numHashes, 7)
db.nameShortSubstrings = map[string][]substring{}
db.nameShortSubstringSizes = map[string]int{}
for key, tokens := range tokenFreqs {
indices := make([]int, len(tokens))
values := make([]float32, len(tokens))
{
i := 0
for t, freq := range tokens {
indices[i] = db.tokens[t]
values[i] = tfidf(freq, db.docfreqs[indices[i]], len(db.licenseTexts))
i++
}
}
db.lsh.Add(key, db.hasher.Hash(values, indices))
registerNameSubstrings(key, key, db.nameShortSubstringSizes, db.nameShortSubstrings)
}
db.lsh.Index()
return db
}
// QueryLicenseText returns the most similar registered licenses.
func (db *database) QueryLicenseText(text string) map[string]float32 {
parts := normalize.Split(text)
licenses := map[string]float32{}
for _, part := range parts {
for key, val := range db.queryLicenseAbstract(part) {
if licenses[key] < val {
licenses[key] = val
}
}
}
return licenses
}
func (db *database) queryLicenseAbstract(text string) map[string]float32 {
normalizedModerate := normalize.LicenseText(text, normalize.Moderate)
titlePositions := db.firstLineRe.FindAllStringIndex(normalizedModerate, -1)
candidates := db.queryLicenseAbstractNormalized(normalizedModerate)
var prevPos int
var prevMatch string
for i, titlePos := range titlePositions {
begPos := titlePos[0]
match := normalizedModerate[titlePos[0]:titlePos[1]]
if len(match) == 0 {
continue
}
if match[0] == '\n' {
match = match[1:]
}
if match == prevMatch {
begPos = prevPos
}
if normalizedModerate[begPos] == '\n' {
begPos++
}
var endPos int
if i < len(titlePositions)-1 {
endPos = titlePositions[i+1][0]
} else {
endPos = len(normalizedModerate)
}
part := normalizedModerate[begPos:endPos]
prevMatch = match
prevPos = begPos
if float64(len(part)) < float64(db.minLicenseLength)*similarityThreshold {
continue
}
newCandidates := db.queryLicenseAbstractNormalized(part)
if len(newCandidates) == 0 {
continue
}
for key, val := range newCandidates {
if candidates[key] < val {
candidates[key] = val
}
}
}
db.addURLMatches(candidates, text)
return candidates
}
func (db *database) addURLMatches(candidates map[string]float32, text string) {
for key := range db.scanForURLs(text) {
if db.debug {
println("URL:", key)
}
if conf := candidates[key]; conf < similarityThreshold {
if conf == 0 {
candidates[key] = 1
} else {
candidates[key] = similarityThreshold
}
}
}
}
func (db *database) queryLicenseAbstractNormalized(normalizedModerate string) map[string]float32 {
normalizedRelaxed := normalize.Relax(normalizedModerate)
if db.debug {
println("\nqueryAbstractNormed --------\n")
println(normalizedModerate)
println("\n========\n")
println(normalizedRelaxed)
}
tokens := map[int]int{}
for _, line := range strings.Split(normalizedRelaxed, "\n") {
for _, token := range strings.Split(line, " ") {
if index, exists := db.tokens[token]; exists {
tokens[index]++
}
}
}
indices := make([]int, len(tokens))
values := make([]float32, len(tokens))
{
i := 0
for key, val := range tokens {
indices[i] = key
values[i] = tfidf(val, db.docfreqs[key], len(db.licenseTexts))
i++
}
}
found := db.lsh.Query(db.hasher.Hash(values, indices))
candidates := map[string]float32{}
if len(found) == 0 {
return candidates
}
for _, keyint := range found {
key := keyint.(string)
licenseText := db.licenseTexts[key]
yourRunes := make([]rune, 0, len(licenseText)/6)
vocabulary := map[string]int{}
for _, line := range strings.Split(licenseText, "\n") {
for _, token := range strings.Split(line, " ") {
index, exists := vocabulary[token]
if !exists {
index = len(vocabulary)
vocabulary[token] = index
}
yourRunes = append(yourRunes, rune(index))
}
}
oovRune := rune(len(vocabulary))
myRunes := make([]rune, 0, len(normalizedModerate)/6)
for _, line := range strings.Split(normalizedModerate, "\n") {
for _, token := range strings.Split(line, " ") {
if index, exists := vocabulary[token]; exists {
myRunes = append(myRunes, rune(index))
} else if len(myRunes) == 0 || myRunes[len(myRunes)-1] != oovRune {
myRunes = append(myRunes, oovRune)
}
}
}
dmp := diffmatchpatch.New()
diff := dmp.DiffMainRunes(myRunes, yourRunes, false)
if db.debug {
tokarr := make([]string, len(db.tokens)+1)
for key, val := range vocabulary {
tokarr[val] = key
}
tokarr[len(db.tokens)] = "!"
println(dmp.DiffPrettyText(dmp.DiffCharsToLines(diff, tokarr)))
}
distance := dmp.DiffLevenshtein(diff)
candidates[key] = float32(1) - float32(distance)/float32(len(myRunes))
}
weak := make([]string, 0, len(candidates))
for key, val := range candidates {
if val < similarityThreshold {
weak = append(weak, key)
}
}
if len(weak) < len(candidates) {
for _, key := range weak {
delete(candidates, key)
}
}
return candidates
}
func (db *database) scanForURLs(text string) map[string]bool {
byteText := []byte(text)
index := suffixarray.New(byteText)
urlMatches := index.FindAllIndex(db.urlRe, -1)
licenses := map[string]bool{}
for _, match := range urlMatches {
url := byteText[match[0]:match[1]]
licenses[db.idByURL[string(url)]] = true
}
return licenses
}
// QueryReadmeText tries to detect licenses mentioned in the README.
func (db *database) QueryReadmeText(text string, fs filer.Filer) map[string]float32 {
candidates := map[string]float32{}
append := func(others map[string]float32) {
for key, val := range others {
if candidates[key] < val {
candidates[key] = val
}
}
}
for _, match := range licenseReadmeMentionRe.FindAllString(text, -1) {
match = strings.TrimRight(match, ".,:;-")
content, err := fs.ReadFile(match)
if err == nil {
if preprocessor, exists := filePreprocessors[paths.Ext(match)]; exists {
content = preprocessor(content)
}
append(db.QueryLicenseText(string(content)))
}
}
if len(candidates) == 0 {
append(investigateReadmeFile(text, db.nameSubstrings, db.nameSubstringSizes))
append(investigateReadmeFile(text, db.nameShortSubstrings, db.nameShortSubstringSizes))
}
if db.debug {
for key, val := range candidates {
println("NLP", key, val)
}
}
db.addURLMatches(candidates, text)
return candidates
}
// URLs returns the list of the URLs for the given license identifier
func (db *database) URLs(id string) ([]string, error) {
urls, found := db.urlsByID[id]
if !found {
return nil, ErrUnknownLicenseID
}
res := make([]string, len(urls))
copy(res, urls)
return urls, nil
}
// Name returns the SPDX name for the license identifier
func (db *database) Name(id string) (string, error) {
name, found := db.nameByID[id]
if !found {
return "", ErrUnknownLicenseID
}
return name, nil
}
func tfidf(freq int, docfreq int, ndocs int) float32 {
weight := fastlog.Log(1+float32(freq)) * fastlog.Log(float32(ndocs)/float32(docfreq))
if weight < 0 {
// logarithm is approximate
return 0
}
return weight
}