-
Notifications
You must be signed in to change notification settings - Fork 6
/
diversity_rule_sort.go
168 lines (141 loc) · 3.7 KB
/
diversity_rule_sort.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
package sort
import (
"errors"
"time"
"github.com/alibaba/pairec/module"
"github.com/alibaba/pairec/recconf"
)
type DiversityRuleSort struct {
diversitySize int
diversityRules []recconf.DiversityRuleConfig
excludeRecallMap map[string]bool
filterParam *module.FilterParam
cloneInstances map[string]*DiversityRuleSort
name string
}
func NewDiversityRuleSort(config recconf.SortConfig) *DiversityRuleSort {
sort := DiversityRuleSort{
diversitySize: config.DiversitySize,
diversityRules: config.DiversityRules,
excludeRecallMap: make(map[string]bool, len(config.ExcludeRecalls)),
filterParam: nil,
name: config.Name,
cloneInstances: make(map[string]*DiversityRuleSort),
}
for _, recallName := range config.ExcludeRecalls {
sort.excludeRecallMap[recallName] = true
}
if len(config.Conditions) > 0 {
filterParam := module.NewFilterParamWithConfig(config.Conditions)
sort.filterParam = filterParam
}
return &sort
}
func (s *DiversityRuleSort) Sort(sortData *SortData) error {
if _, ok := sortData.Data.([]*module.Item); !ok {
return errors.New("sort data type error")
}
// if condition is empty
if s.filterParam == nil {
return s.doSort(sortData)
} else {
userProperties := sortData.User.MakeUserFeatures2()
flag, err := s.filterParam.EvaluateByDomain(userProperties, nil)
if err != nil {
return err
}
if flag {
return s.doSort(sortData)
}
}
return nil
}
func (s *DiversityRuleSort) createDiversityRules() (ret []*DiversityRule) {
for _, config := range s.diversityRules {
rule := NewDiversityRule(config)
ret = append(ret, rule)
}
return
}
func (s *DiversityRuleSort) doSort(sortData *SortData) error {
start := time.Now()
items := sortData.Data.([]*module.Item)
diversityRules := s.createDiversityRules()
if len(diversityRules) == 0 {
return nil
}
var excludeItems []*module.Item
if len(s.excludeRecallMap) > 0 {
newItems := make([]*module.Item, 0, len(items))
for _, item := range items {
if _, ok := s.excludeRecallMap[item.GetRecallName()]; ok {
excludeItems = append(excludeItems, item)
} else {
newItems = append(newItems, item)
}
}
items = newItems
}
itemLength := len(items)
//if items empty
if itemLength == 0 {
return nil
}
diversitySize := sortData.Context.Size
if s.diversitySize > 0 {
diversitySize = s.diversitySize
if diversitySize > itemLength {
diversitySize = itemLength
}
}
result := make([]*module.Item, 0, diversitySize)
alreadyMatchItems := make(map[module.ItemId]bool, diversitySize)
alreadyMatchItems[items[0].Id] = true
result = append(result, items[0])
items = items[1:]
index := 1
for len(result) <= diversitySize {
if index == itemLength {
break
}
flag := true
// if all the rest items not match diversity rule, use the first item append to the result
firstItemIndex := -1
for i, item := range items {
if _, ok := alreadyMatchItems[item.Id]; ok {
continue
}
if firstItemIndex == -1 {
firstItemIndex = i
}
flag = true
for _, rule := range diversityRules {
if flag = rule.Match(item, result); !flag {
break
}
}
// if the item match all the diversity rule, so add it to the result
if flag {
alreadyMatchItems[item.Id] = true
result = append(result, item)
index++
break
}
}
if !flag {
alreadyMatchItems[items[firstItemIndex].Id] = true
result = append(result, items[firstItemIndex])
index++
}
}
for _, item := range items {
if _, ok := alreadyMatchItems[item.Id]; ok {
continue
}
result = append(result, item)
}
result = append(result, excludeItems...)
sortData.Data = result
sortInfoLog(sortData, "DiversityRuleSort", len(result), start)
return nil
}