-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstringRank.go
More file actions
92 lines (82 loc) · 2.18 KB
/
Copy pathstringRank.go
File metadata and controls
92 lines (82 loc) · 2.18 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
// Podpurna miniknihovnicka pro vnitrni potreby Jecna API
// ktera nam usnadnuje hledani spravnych slozek pro ziskani
// momentalniho suplarchu, protoze zjevne mit sjednoceny
// format nazvu je prilis tezke pro Jecnou
package jecnaapi
import (
"strings"
)
type stringRank struct {
strings []string
rules []stringRankRule
continuous bool // pokud je true, postupne orezava string o dane stringy, kdezto pokud je false, tak pokazde orezava o puvodni string
}
type stringRankResult []stringRankResultRow
type stringRankRule struct {
stringHas string // kdyz string obsahuje tento string
scoreIfTrue int64 // tak za to dostane tento pocet bodu (lze i zaporny ci nula)
scoreIfFalse int64 // a pokud nebsahuje, tak dostane tento pocet bodu
}
type stringRankResultRow struct {
string string
score int64
}
func (sr *stringRank) do() stringRankResult {
results := make(stringRankResult, len(sr.strings))
var substr string
var score int64
if sr.continuous {
var lastsubstr string
for k, v := range sr.strings {
lastsubstr = v
substr = v
score = 0
for _, v2 := range sr.rules {
substr = strings.Replace(lastsubstr, v2.stringHas, "", -1)
if len(substr) < len(lastsubstr) {
score += v2.scoreIfTrue
} else {
score += v2.scoreIfFalse
}
lastsubstr = substr
}
results[k] = stringRankResultRow{string: v, score: score}
}
} else {
for k, v := range sr.strings {
score = 0
for _, v2 := range sr.rules {
substr = strings.Replace(v, v2.stringHas, "", -1)
if len(substr) < len(v) {
score += v2.scoreIfTrue
} else {
score += v2.scoreIfFalse
}
}
results[k] = stringRankResultRow{string: v, score: score}
}
}
return results
}
/*
func (sr stringRankResult) HighestScore() stringRankResultRow {
var highestscore int64 = -1
highestscoreresult := -1
for k, v := range sr {
if v.score > highestscore {
highestscoreresult = k
}
}
return sr[highestscoreresult]
}
func (sr stringRankResult) LowestScore() stringRankResultRow {
var lowestscore int64 = math.MaxInt64
highestscoreresult := -1
for k, v := range sr {
if v.score > lowestscore {
highestscoreresult = k
}
}
return sr[highestscoreresult]
}
*/