Exemple #1
0
//
// Print ranking with only lifter Id and sinclair score
//
func PrintSimpleRanking() {

	conn := client.MustConn("localhost:9191")

	fmt.Println("Womens youth ranking")
	for _, scoreBox := range conn.MirrorSliceIndex(youthWomenRankingKey, nil, nil) {
		fmt.Println(string(scoreBox.Value), common.MustDecodeFloat64(scoreBox.Key))
	}

	fmt.Println("\nWomens junior ranking")
	for _, scoreBox := range conn.MirrorSliceIndex(juniorWomenRankingKey, nil, nil) {
		fmt.Println(string(scoreBox.Value), common.MustDecodeFloat64(scoreBox.Key))
	}

	fmt.Println("\nWomens senior ranking")
	for _, scoreBox := range conn.MirrorSliceIndex(seniorWomenRankingKey, nil, nil) {
		fmt.Println(string(scoreBox.Value), common.MustDecodeFloat64(scoreBox.Key))
	}

	fmt.Println("\nWomens senior ranking")
	for _, scoreBox := range conn.MirrorSliceIndex(veteranWomenRankingKey, nil, nil) {
		fmt.Println(string(scoreBox.Value), common.MustDecodeFloat64(scoreBox.Key))
	}

	fmt.Println("\nMens youth ranking")
	for _, scoreBox := range conn.MirrorSliceIndex(youthMenRankingKey, nil, nil) {
		fmt.Println(string(scoreBox.Value), common.MustDecodeFloat64(scoreBox.Key))
	}

	fmt.Println("\nMens junior ranking")
	for _, scoreBox := range conn.MirrorSliceIndex(juniorMenRankingKey, nil, nil) {
		fmt.Println(string(scoreBox.Value), common.MustDecodeFloat64(scoreBox.Key))
	}

	fmt.Println("\nMens senior ranking")
	for _, scoreBox := range conn.MirrorSliceIndex(seniorMenRankingKey, nil, nil) {
		fmt.Println(string(scoreBox.Value), common.MustDecodeFloat64(scoreBox.Key))
	}

	fmt.Println("\nMens senior ranking")
	for _, scoreBox := range conn.MirrorSliceIndex(veteranMenRankingKey, nil, nil) {
		fmt.Println(string(scoreBox.Value), common.MustDecodeFloat64(scoreBox.Key))
	}
}
func getLikes(w http.ResponseWriter, r *http.Request, c *client.Conn) {
	uid := mux.Vars(r)["user_id"]
	lKey := uLikesKey(uid)
	var result []common.Message
	for _, item := range c.Slice(lKey, nil, nil, true, true) {
		result = append(result, common.Message{
			Type:   common.Like,
			User:   uid,
			Object: string(item.Key),
			Weight: godCommon.MustDecodeFloat64(item.Value),
		})
	}
	w.Header().Set("Content-Type", "application/json; charset=UTF-8")
	if err := json.NewEncoder(w).Encode(result); err != nil {
		panic(err)
	}
}
Exemple #3
0
func ExampleSetExpression() {
	conn := MustConn("127.0.0.1:9191")
	conn.Kill()
	conn.SubPut([]byte("myfriends"), []byte("alice"), common.EncodeFloat64(10))
	conn.SubPut([]byte("myfriends"), []byte("bob"), common.EncodeFloat64(5))
	conn.SubPut([]byte("yourfriends"), []byte("bob"), common.EncodeFloat64(6))
	conn.SubPut([]byte("yourfriends"), []byte("charlie"), common.EncodeFloat64(4))
	fmt.Printf("name score\n")
	for _, friend := range conn.SetExpression(setop.SetExpression{
		Code: "(U:FloatSum myfriends yourfriends)",
	}) {
		fmt.Printf("%v %v\n", string(friend.Key), common.MustDecodeFloat64(friend.Values[0]))
	}
	// Output:
	// name score
	// alice 10
	// bob 11
	// charlie 4
}
Exemple #4
0
func ExampleTreeMirror() {
	conn := MustConn("127.0.0.1:9191")
	conn.Kill()
	conn.SubAddConfiguration([]byte("myfriends"), "mirrored", "yes")
	conn.SubPut([]byte("myfriends"), []byte("alice"), common.EncodeFloat64(10))
	conn.SubPut([]byte("myfriends"), []byte("bob"), common.EncodeFloat64(5))
	conn.SubPut([]byte("myfriends"), []byte("charlie"), common.EncodeFloat64(6))
	conn.SubPut([]byte("myfriends"), []byte("denise"), common.EncodeFloat64(4))
	fmt.Printf("name score\n")
	for _, friend := range conn.MirrorReverseSlice([]byte("myfriends"), nil, nil, true, true) {
		fmt.Printf("%v %v\n", common.MustDecodeFloat64(friend.Key), string(friend.Value))
	}
	// Output:
	// name score
	// 10 alice
	// 6 charlie
	// 5 bob
	// 4 denise
}
func getRecommendations(w http.ResponseWriter, r *http.Request, c *client.Conn) {
	uid := mux.Vars(r)["user_id"]
	var request common.RecommendationsRequest
	if err := json.NewDecoder(r.Body).Decode(&request); err != nil {
		panic(err)
	}
	// Create a set operation that returns the union of the likers of all objects we have liked, just returning the user key
	likersOp := &setop.SetOp{
		Merge: setop.First,
		Type:  setop.Union,
	}
	// For each object we have liked, add the likers of that flavor as a source to the union of likers
	for _, obj := range c.Slice(uLikesKey(uid), nil, nil, true, true) {
		likersOp.Sources = append(likersOp.Sources, setop.SetOpSource{Key: oLikesKey(string(obj.Key))})
	}
	// Create a set operation that returns the union of all things liked by all likers of objects we liked, returning the sum of the likes for each object
	objectsOp := &setop.SetOp{
		Merge: setop.FloatSum,
		Type:  setop.Union,
	}
	// For each user in the union of users having liked something we like
	for _, user := range c.SetExpression(setop.SetExpression{
		Op: likersOp,
	}) {
		// If the user is not us
		if string(user.Key) != uid {
			// Fetch the number of objects we have both liked
			similarity := len(c.SetExpression(setop.SetExpression{
				Code: fmt.Sprintf("(I:First %v %v)", string(uLikesKey(string(user.Key))), string(uLikesKey(uid))),
			}))
			// And weight the user according to how many commonalities we have
			weight := math.Log(float64(similarity + 1))
			// Add the objects liked by this user, weighed this much, as a source
			objectsOp.Sources = append(objectsOp.Sources, setop.SetOpSource{Key: uLikesKey(string(user.Key)), Weight: &weight})
		}
	}
	// If we want to filter on active objects
	if request.Actives != "" {
		// Create an operation on the simple Union and the active objects
		objectsOp = &setop.SetOp{
			Merge: setop.First,
			Sources: []setop.SetOpSource{
				setop.SetOpSource{
					SetOp: objectsOp,
				},
				setop.SetOpSource{
					Key: activeObjectsKey,
				},
			},
		}
		// And make it of the correct type
		if request.Actives == common.Reject {
			objectsOp.Type = setop.Difference
		} else if request.Actives == common.Intersect {
			objectsOp.Type = setop.Intersection
		}
	}
	// If we want to filter on viewed objects
	if request.Viewed != "" {
		// Create an operation on the previous operation and the viewed objects
		objectsOp = &setop.SetOp{
			Merge: setop.First,
			Sources: []setop.SetOpSource{
				setop.SetOpSource{
					SetOp: objectsOp,
				},
				setop.SetOpSource{
					Key: uViewsKey(uid),
				},
			},
		}
		// And make it of the correct type
		if request.Viewed == common.Reject {
			objectsOp.Type = setop.Difference
		} else if request.Viewed == common.Intersect {
			objectsOp.Type = setop.Intersection
		}
	}
	// Finally, fetch the wanted number of recommendations
	var result []common.Message
	for _, item := range c.SetExpression(setop.SetExpression{
		Op: objectsOp,
	}) {
		w := godCommon.MustDecodeFloat64(item.Values[0])
		i := sort.Search(len(result), func(i int) bool {
			return w > result[i].Weight
		})
		if i < len(result) {
			if len(result) < request.Num {
				result = append(result[:i], append([]common.Message{common.Message{
					Object: string(item.Key),
					Weight: w,
				}}, result[i:]...)...)
			} else {
				if i > 0 {
					result = append(result[:i], append([]common.Message{common.Message{
						Object: string(item.Key),
						Weight: w,
					}}, result[i:len(result)-1]...)...)
				} else {
					result = append([]common.Message{common.Message{
						Object: string(item.Key),
						Weight: w,
					}}, result[i:len(result)-1]...)
				}
			}
		} else {
			if len(result) < request.Num {
				result = append(result, common.Message{
					Object: string(item.Key),
					Weight: w,
				})
			}
		}
	}
	w.Header().Set("Content-Type", "application/json; charset=UTF-8")
	if err := json.NewEncoder(w).Encode(result); err != nil {
		panic(err)
	}
}