-
-
Notifications
You must be signed in to change notification settings - Fork 397
Expand file tree
/
Copy pathmetric.go
More file actions
105 lines (81 loc) · 2.29 KB
/
metric.go
File metadata and controls
105 lines (81 loc) · 2.29 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
package query
import (
"strings"
"github.com/adrg/strutil"
)
const minVotes = 30
const minPopularity = 0.5
const (
separateSourceMax = 45.0
separateSourceMin = 5.0
)
func (a *abstractResults) aurSortByMetric(pkg *abstractResult) float64 {
votesScore := 1 - (minVotes / (minVotes + float64(pkg.votes)))
if pkg.popularity <= 0 {
return votesScore
}
popularityScore := 1 - (minPopularity / (minPopularity + pkg.popularity))
return (votesScore + popularityScore) / 2
}
func (a *abstractResults) GetMetric(pkg *abstractResult) float64 {
if v, ok := a.distanceCache[pkg.name]; ok {
return v
}
if strings.EqualFold(pkg.name, a.search) {
return 1.0
}
sim := strutil.Similarity(pkg.name, a.search, a.metric)
for _, prov := range pkg.provides {
// If the package provides search, it's a perfect match
// AUR packages don't populate provides
candidate := strutil.Similarity(prov, a.search, a.metric) * 0.80
if candidate > sim {
sim = candidate
}
}
simDesc := strutil.Similarity(pkg.description, a.search, a.metric)
// slightly overweight sync sources by always giving them max popularity
popularity := 1.0
if pkg.source == "aur" {
popularity = a.aurSortByMetric(pkg)
}
sim = sim*0.5 + simDesc*0.2 + popularity*0.3
a.distanceCache[pkg.name] = sim
return sim
}
func (a *abstractResults) separateSourceScore(source string, score float64) float64 {
if !a.separateSources {
return 0
}
if score == 1.0 {
return 50
}
if v, ok := a.separateSourceCache[source]; ok {
return v
}
// AUR is always lowest priority
if source == "aur" {
return 0
}
// Score sync repositories based on pacman.conf order (as reflected by dbExecutor.Repos()).
// First repo gets max, last repo gets min, evenly distributed across the range.
for i, repo := range a.repoOrder {
if repo != source {
continue
}
n := len(a.repoOrder)
if n == 1 {
a.separateSourceCache[source] = separateSourceMax
return separateSourceMax
}
step := (separateSourceMax - separateSourceMin) / float64(n-1)
sourceScore := separateSourceMax - (float64(i) * step)
a.separateSourceCache[source] = sourceScore
return sourceScore
}
return 0
}
func (a *abstractResults) calculateMetric(pkg *abstractResult) float64 {
score := a.GetMetric(pkg)
return a.separateSourceScore(pkg.source, score) + score
}