Пример #1
0
        public List <RecommendedItem> recommendedBecause(long userID, long itemID, int howMany)
        {
            DataModel model = this.getDataModel();

            TopItems.Estimator <long> estimator = new RecommendedBecauseEstimator(this, userID, itemID);
            PreferenceArray           array     = model.getPreferencesFromUser(userID);
            int       size = array.length();
            FastIDSet set  = new FastIDSet(size);

            for (int i = 0; i < size; i++)
            {
                set.add(array.getItemID(i));
            }
            set.remove(itemID);
            return(TopItems.getTopItems(howMany, set.GetEnumerator(), null, estimator));
        }
        private static int mask(FastIDSet commonSet, FastIDSet otherSet, long maxItemID)
        {
            int num = 0;

            for (int i = 0; i <= maxItemID; i++)
            {
                if (commonSet.contains((long)i))
                {
                    if (otherSet.contains((long)i))
                    {
                        num++;
                    }
                    else
                    {
                        commonSet.remove((long)i);
                    }
                }
            }
            return(num);
        }
Пример #3
0
        protected override float doEstimatePreference(long theUserID, PreferenceArray preferencesFromUser, long itemID)
        {
            DataModel dataModel       = getDataModel();
            int       size            = preferencesFromUser.length();
            FastIDSet possibleItemIDs = new FastIDSet(size);

            for (int i = 0; i < size; i++)
            {
                possibleItemIDs.add(preferencesFromUser.getItemID(i));
            }
            possibleItemIDs.remove(itemID);

            List <RecommendedItem> mostSimilar = mostSimilarItems(itemID, possibleItemIDs.GetEnumerator(), neighborhoodSize, null);

            long[] theNeighborhood = new long[mostSimilar.Count() + 1];
            theNeighborhood[0] = -1;

            List <long> usersRatedNeighborhood = new List <long>();
            int         nOffset = 0;

            foreach (RecommendedItem rec in mostSimilar)
            {
                theNeighborhood[nOffset++] = rec.getItemID();
            }

            if (mostSimilar.Count != 0)
            {
                theNeighborhood[mostSimilar.Count] = itemID;
                for (int i = 0; i < theNeighborhood.Length; i++)
                {
                    PreferenceArray usersNeighborhood = dataModel.getPreferencesForItem(theNeighborhood[i]);
                    int             size1             = usersRatedNeighborhood.Count == 0 ? usersNeighborhood.length() : usersRatedNeighborhood.Count;
                    for (int j = 0; j < size1; j++)
                    {
                        if (i == 0)
                        {
                            usersRatedNeighborhood.Add(usersNeighborhood.getUserID(j));
                        }
                        else
                        {
                            if (j >= usersRatedNeighborhood.Count)
                            {
                                break;
                            }
                            long index = usersRatedNeighborhood[j];
                            if (!usersNeighborhood.hasPrefWithUserID(index) || index == theUserID)
                            {
                                usersRatedNeighborhood.Remove(index);
                                j--;
                            }
                        }
                    }
                }
            }

            double[] weights = null;
            if (mostSimilar.Count != 0)
            {
                weights = getInterpolations(itemID, theNeighborhood, usersRatedNeighborhood);
            }

            int    n               = 0;
            double preference      = 0.0;
            double totalSimilarity = 0.0;

            foreach (long jitem in theNeighborhood)
            {
                float?pref = dataModel.getPreferenceValue(theUserID, jitem);

                if (pref != null)
                {
                    double weight = weights[n];
                    preference      += pref.Value * weight;
                    totalSimilarity += weight;
                }
                n++;
            }
            return(totalSimilarity == 0.0 ? float.NaN : (float)(preference / totalSimilarity));
        }