-
-
Notifications
You must be signed in to change notification settings - Fork 305
/
lms.go
56 lines (51 loc) · 1.32 KB
/
lms.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
// Copyright Safing ICS Technologies GmbH. Use of this source code is governed by the AGPL license that can be found in the LICENSE file.
package algs
import (
"strings"
)
func LmsScoreOfDomain(domain string) float64 {
var totalScore float64
domain = strings.ToLower(domain)
subjects := strings.Split(domain, ".")
// ignore the last two parts
if len(subjects) <= 3 {
return 100
}
subjects = subjects[:len(subjects)-3]
var totalLength int
for _, subject := range subjects {
totalLength += len(subject)
}
for _, subject := range subjects {
// calculate score, weigh it and add it
if len(subject) > 0 {
totalScore += LmsScore(subject) * (float64(len(subject)) / float64(totalLength))
}
}
return totalScore
}
func LmsScore(subject string) float64 {
lms_start := -1
lms_stop := -1
longest_lms := 0
for i, c := range subject {
if int(c) >= int('a') && int(c) <= int('z') {
if lms_start == -1 {
lms_start = i
}
} else {
if lms_start > -1 {
lms_stop = i
if lms_stop-lms_start > longest_lms {
longest_lms = lms_stop - lms_start
}
lms_start = -1
}
}
}
if lms_stop == -1 {
longest_lms = len(subject)
}
// fmt.Printf("algs: lms score of %s is %.2f\n", subject, (float64(longest_lms) * 100.0 / float64(len(subject))))
return (float64(longest_lms) * 100.0 / float64(len(subject)))
}