forked from openshift/origin
-
Notifications
You must be signed in to change notification settings - Fork 1
/
scorers.go
62 lines (54 loc) · 1.44 KB
/
scorers.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
package app
import (
"strings"
imageapi "github.com/openshift/origin/pkg/image/apis/image"
templateapi "github.com/openshift/origin/pkg/template/apis/template"
)
func templateScorer(template templateapi.Template, term string) (float32, bool) {
score := stringProximityScorer(template.Name, term)
return score, score < 0.3
}
func imageStreamScorer(imageStream imageapi.ImageStream, term string) (float32, bool) {
score := stringProximityScorer(imageStream.Name, term)
return score, score < 0.3
}
func stringProximityScorer(s, query string) float32 {
sLower := strings.ToLower(s)
queryLower := strings.ToLower(query)
var score float32
switch {
case query == "*":
score = 0.0
case s == query:
score = 0.0
case strings.EqualFold(s, query):
score = 0.02
case strings.HasPrefix(s, query):
score = 0.1
case strings.HasPrefix(sLower, queryLower):
score = 0.12
case strings.Contains(s, query):
score = 0.2
case strings.Contains(sLower, queryLower):
score = 0.22
default:
score = 1.0
}
return score
}
func partialScorer(a, b string, prefix bool, partial, none float32) (bool, float32) {
switch {
// If either one is empty, it's a partial match because the values do not conflict.
case len(a) == 0 && len(b) != 0, len(a) != 0 && len(b) == 0:
return true, partial
case a != b:
if prefix {
if strings.HasPrefix(a, b) || strings.HasPrefix(b, a) {
return true, partial
}
}
return false, none
default:
return true, 0.0
}
}