Skip to content

Commit

Permalink
pass exclude in explore (#304)
Browse files Browse the repository at this point in the history
  • Loading branch information
zhenghaoz authored Nov 6, 2021
1 parent 84ec522 commit 6c351c7
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 6 deletions.
9 changes: 4 additions & 5 deletions worker/worker.go
Original file line number Diff line number Diff line change
Expand Up @@ -560,7 +560,7 @@ func (w *Worker) Recommend(m ranking.MatrixFactorization, users []string) {
}

// explore latest and popular
result, err = w.exploreRecommend(result)
result, err = w.exploreRecommend(result, excludeSet)
if err != nil {
base.Logger().Error("failed to explore latest and popular items", zap.Error(err))
return errors.Trace(err)
Expand Down Expand Up @@ -673,7 +673,7 @@ func mergeAndShuffle(candidates [][]string) []cache.Scored {
return recommend
}

func (w *Worker) exploreRecommend(exploitRecommend []cache.Scored) ([]cache.Scored, error) {
func (w *Worker) exploreRecommend(exploitRecommend []cache.Scored, excludeSet *strset.Set) ([]cache.Scored, error) {
// create thresholds
explorePopularThreshold := 0.0
if threshold, exist := w.cfg.Recommend.ExploreRecommend["popular"]; exist {
Expand All @@ -695,7 +695,6 @@ func (w *Worker) exploreRecommend(exploitRecommend []cache.Scored) ([]cache.Scor
}
// explore recommendation
var exploreRecommend []cache.Scored
memo := strset.New()
for range exploitRecommend {
dice := rand.Float64()
var recommendItem cache.Scored
Expand All @@ -711,8 +710,8 @@ func (w *Worker) exploreRecommend(exploitRecommend []cache.Scored) ([]cache.Scor
} else {
break
}
if !memo.Has(recommendItem.Id) {
memo.Add(recommendItem.Id)
if !excludeSet.Has(recommendItem.Id) {
excludeSet.Add(recommendItem.Id)
exploreRecommend = append(exploreRecommend, recommendItem)
}
}
Expand Down
3 changes: 2 additions & 1 deletion worker/worker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
"encoding/json"
"github.com/alicebob/miniredis/v2"
cmap "github.com/orcaman/concurrent-map"
"github.com/scylladb/go-set/strset"
"github.com/stretchr/testify/assert"
"github.com/zhenghaoz/gorse/base"
"github.com/zhenghaoz/gorse/config"
Expand Down Expand Up @@ -327,7 +328,7 @@ func TestExploreRecommend(t *testing.T) {

recommend, err := w.exploreRecommend(cache.CreateScoredItems(
[]string{"1", "2", "3", "4", "5", "6", "7", "8"},
[]float32{0, 0, 0, 0, 0, 0, 0, 0}))
[]float32{0, 0, 0, 0, 0, 0, 0, 0}), strset.New())
assert.NoError(t, err)
items := cache.RemoveScores(recommend)
assert.Contains(t, items, "latest")
Expand Down

0 comments on commit 6c351c7

Please sign in to comment.