MyMediaLite Java推荐方法

时间:2015-09-07 11:18:49

标签: java c# recommendation-engine

我在MyMediaLiteJava中找到了jcnewell/MyMediaLiteJava  在gitHub上)没有推荐方法,因为它在这个推荐者的C#版本中。 例如,在C#MyMediaLite docs中随机推荐是方法Recommend (int user_id, int n=-1, ICollection< int > ignore_items=null, ICollection< int > candidate_items=null),但在Java版本中没有这样的方法..或者我可能错过了什么?我只是想尝试至少得到随机的推荐项目。
我也尝试重新编写从C#到JAVA的推荐方法,但对我而言,它有点太难了,因为我不了解Java的一些C#类似物。我将离开这里在C#上推荐方法代码,也许我会得到一些建议。

public virtual System.Collections.Generic.IList<Tuple<int, float>> Recommend(
        int user_id, int n = -1,
        System.Collections.Generic.ICollection<int> ignore_items = null,
        System.Collections.Generic.ICollection<int> candidate_items = null)
    {
        if (candidate_items == null)
            candidate_items = Enumerable.Range(0, MaxItemID - 1).ToList();
        if (ignore_items == null)
            ignore_items = new int[0];

        System.Collections.Generic.IList<Tuple<int, float>> ordered_items;

        if (n == -1)
        {
            var scored_items = new List<Tuple<int, float>>();
            foreach (int item_id in candidate_items)
                if (!ignore_items.Contains(item_id))
                {
                    float score = Predict(user_id, item_id);
                    if (score > float.MinValue)
                        scored_items.Add(Tuple.Create(item_id, score));
                }
            ordered_items = scored_items.OrderByDescending(x => x.Item2).ToArray();
        }
        else
        {
            var comparer = new DelegateComparer<Tuple<int, float>>( (a, b) => a.Item2.CompareTo(b.Item2) );
            var heap = new IntervalHeap<Tuple<int, float>>(n, comparer);
            float min_relevant_score = float.MinValue;

            foreach (int item_id in candidate_items)
                if (!ignore_items.Contains(item_id))
                {
                    float score = Predict(user_id, item_id);
                    if (score > min_relevant_score)
                    {
                        heap.Add(Tuple.Create(item_id, score));
                        if (heap.Count > n)
                        {
                            heap.DeleteMin();
                            min_relevant_score = heap.FindMin().Item2;
                        }
                    }
                }

            ordered_items = new Tuple<int, float>[heap.Count];
            for (int i = 0; i < ordered_items.Count; i++)
                ordered_items[i] = heap.DeleteMax();
        }

        return ordered_items;
    }

0 个答案:

没有答案