Exemplo n.º 1
0
        public void removeItemPref(long userID, long itemIDA, float prefValue)
        {
            PreferenceArray userPreferences = dataModel.getPreferencesFromUser(userID);

            try
            {
                buildAverageDiffsLock.EnterWriteLock();

                FastByIDMap <RunningAverage> aMap = averageDiffs.get(itemIDA);

                int length = userPreferences.length();
                for (int i = 0; i < length; i++)
                {
                    long  itemIDB = userPreferences.getItemID(i);
                    float bValue  = userPreferences.getValue(i);

                    if (itemIDA < itemIDB)
                    {
                        if (aMap != null)
                        {
                            RunningAverage average = aMap.get(itemIDB);
                            if (average != null)
                            {
                                if (average.getCount() <= 1)
                                {
                                    aMap.remove(itemIDB);
                                }
                                else
                                {
                                    average.removeDatum(bValue - prefValue);
                                }
                            }
                        }
                    }
                    else if (itemIDA > itemIDB)
                    {
                        FastByIDMap <RunningAverage> bMap = averageDiffs.get(itemIDB);
                        if (bMap != null)
                        {
                            RunningAverage average = bMap.get(itemIDA);
                            if (average != null)
                            {
                                if (average.getCount() <= 1)
                                {
                                    aMap.remove(itemIDA);
                                }
                                else
                                {
                                    average.removeDatum(prefValue - bValue);
                                }
                            }
                        }
                    }
                }
            }
            finally
            {
                buildAverageDiffsLock.ExitWriteLock();
            }
        }
Exemplo n.º 2
0
 public void setTempPrefs(PreferenceArray prefs)
 {
     this.tempPrefs = prefs;
     this.prefItemIDs.clear();
     for (int i = 0; i < prefs.length(); i++)
     {
         this.prefItemIDs.add(prefs.getItemID(i));
     }
 }
Exemplo n.º 3
0
        private long processOneUser(long averageCount, long userID)
        {
            log.debug("Processing prefs for user {}", userID);
            // Save off prefs for the life of this loop iteration
            PreferenceArray userPreferences = dataModel.getPreferencesFromUser(userID);
            int             length          = userPreferences.length();

            for (int i = 0; i < length - 1; i++)
            {
                float prefAValue = userPreferences.getValue(i);
                long  itemIDA    = userPreferences.getItemID(i);
                FastByIDMap <RunningAverage> aMap = averageDiffs.get(itemIDA);
                if (aMap == null)
                {
                    aMap = new FastByIDMap <RunningAverage>();
                    averageDiffs.put(itemIDA, aMap);
                }
                for (int j = i + 1; j < length; j++)
                {
                    // This is a performance-critical block
                    long           itemIDB = userPreferences.getItemID(j);
                    RunningAverage average = aMap.get(itemIDB);
                    if (average == null && averageCount < maxEntries)
                    {
                        average = buildRunningAverage();
                        aMap.put(itemIDB, average);
                        averageCount++;
                    }
                    if (average != null)
                    {
                        average.addDatum(userPreferences.getValue(j) - prefAValue);
                    }
                }
                RunningAverage itemAverage = averageItemPref.get(itemIDA);
                if (itemAverage == null)
                {
                    itemAverage = buildRunningAverage();
                    averageItemPref.put(itemIDA, itemAverage);
                }
                itemAverage.addDatum(prefAValue);
            }
            return(averageCount);
        }
        public void setTempPrefs(PreferenceArray prefs, long anonymousUserID)
        {
            this.tempPrefs[anonymousUserID] = prefs;
            FastIDSet set = new FastIDSet();

            for (int i = 0; i < prefs.length(); i++)
            {
                set.add(prefs.getItemID(i));
            }
            this.prefItemIDs[anonymousUserID] = set;
        }
Exemplo n.º 5
0
        public override FastIDSet getItemIDsFromUser(long userID)
        {
            PreferenceArray array = this.getPreferencesFromUser(userID);
            int             size  = array.length();
            FastIDSet       set   = new FastIDSet(size);

            for (int i = 0; i < size; i++)
            {
                set.add(array.getItemID(i));
            }
            return(set);
        }
Exemplo n.º 6
0
        public void addItemPref(long userID, long itemIDA, float prefValue)
        {
            PreferenceArray userPreferences = dataModel.getPreferencesFromUser(userID);

            try
            {
                buildAverageDiffsLock.EnterWriteLock();

                FastByIDMap <RunningAverage> aMap = averageDiffs.get(itemIDA);
                if (aMap == null)
                {
                    aMap = new FastByIDMap <RunningAverage>();
                    averageDiffs.put(itemIDA, aMap);
                }
                int length = userPreferences.length();
                for (int i = 0; i < length; i++)
                {
                    long  itemIDB = userPreferences.getItemID(i);
                    float bValue  = userPreferences.getValue(i);
                    if (itemIDA < itemIDB)
                    {
                        RunningAverage average = aMap.get(itemIDB);
                        if (average == null)
                        {
                            average = buildRunningAverage();
                            aMap.put(itemIDB, average);
                        }
                        average.addDatum(bValue - prefValue);
                    }
                    else
                    {
                        FastByIDMap <RunningAverage> bMap = averageDiffs.get(itemIDB);
                        if (bMap == null)
                        {
                            bMap = new FastByIDMap <RunningAverage>();
                            averageDiffs.put(itemIDB, bMap);
                        }
                        RunningAverage average = bMap.get(itemIDA);
                        if (average == null)
                        {
                            average = buildRunningAverage();
                            bMap.put(itemIDA, average);
                        }
                        average.addDatum(prefValue - bValue);
                    }
                }
            }
            finally
            {
                buildAverageDiffsLock.ExitWriteLock();
            }
        }
Exemplo n.º 7
0
        private static float?getPreferenceForItem(PreferenceArray preferencesFromUser, long itemID)
        {
            int num = preferencesFromUser.length();

            for (int i = 0; i < num; i++)
            {
                if (preferencesFromUser.getItemID(i) == itemID)
                {
                    return(new float?(preferencesFromUser.getValue(i)));
                }
            }
            return(null);
        }
        public override PreferenceArray getPreferencesForItem(long itemID)
        {
            int num;

            if (this.tempPrefs.Count == 0)
            {
                return(base.getDelegate().getPreferencesForItem(itemID));
            }
            PreferenceArray array = null;

            try
            {
                array = base.getDelegate().getPreferencesForItem(itemID);
            }
            catch (NoSuchItemException)
            {
            }
            List <Preference> list = new List <Preference>();

            foreach (KeyValuePair <long, PreferenceArray> pair in this.tempPrefs)
            {
                PreferenceArray array2 = pair.Value;
                num = 0;
                while (num < array2.length())
                {
                    if (array2.getItemID(num) == itemID)
                    {
                        list.Add(array2.get(num));
                    }
                    num++;
                }
            }
            int             num2   = (array == null) ? 0 : array.length();
            int             count  = list.Count;
            int             num4   = 0;
            PreferenceArray array3 = new GenericItemPreferenceArray(num2 + count);

            for (num = 0; num < num2; num++)
            {
                array3.set(num4++, array.get(num));
            }
            foreach (Preference preference in list)
            {
                array3.set(num4++, preference);
            }
            if (array3.length() == 0)
            {
                throw new NoSuchItemException(itemID);
            }
            return(array3);
        }
Exemplo n.º 9
0
        public override float?getPreferenceValue(long userID, long itemID)
        {
            PreferenceArray array = this.getPreferencesFromUser(userID);
            int             num   = array.length();

            for (int i = 0; i < num; i++)
            {
                if (array.getItemID(i) == itemID)
                {
                    return(new float?(array.getValue(i)));
                }
            }
            return(null);
        }
        public FastIDSet getRelevantItemsIDs(long userID, int at, double relevanceThreshold, DataModel dataModel)
        {
            PreferenceArray array = dataModel.getPreferencesFromUser(userID);
            FastIDSet       set   = new FastIDSet(at);

            array.sortByValueReversed();
            for (int i = 0; (i < array.length()) && (set.size() < at); i++)
            {
                if (array.getValue(i) >= relevanceThreshold)
                {
                    set.add(array.getItemID(i));
                }
            }
            return(set);
        }
        private static long setBits(FastIDSet modelSet, PreferenceArray prefs, int max)
        {
            long num = -1L;

            for (int i = 0; (i < prefs.length()) && (i < max); i++)
            {
                long key = prefs.getItemID(i);
                modelSet.add(key);
                if (key > num)
                {
                    num = key;
                }
            }
            return(num);
        }
 public override float?getPreferenceValue(long userID, long itemID)
 {
     if (this.isAnonymousUser(userID))
     {
         PreferenceArray array = this.tempPrefs[userID];
         for (int i = 0; i < array.length(); i++)
         {
             if (array.getItemID(i) == itemID)
             {
                 return(new float?(array.getValue(i)));
             }
         }
         return(null);
     }
     return(base.getDelegate().getPreferenceValue(userID, itemID));
 }
Exemplo n.º 13
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));
        }
Exemplo n.º 14
0
        public static FastByIDMap <FastIDSet> toDataMap(FastByIDMap <PreferenceArray> data)
        {
            FastByIDMap <FastIDSet> map = new FastByIDMap <FastIDSet>(data.size());

            foreach (KeyValuePair <long, PreferenceArray> pair in data.entrySet())
            {
                PreferenceArray array = pair.Value;
                int             size  = array.length();
                FastIDSet       set   = new FastIDSet(size);
                for (int i = 0; i < size; i++)
                {
                    set.add(array.getItemID(i));
                }
                map.put(pair.Key, set);
            }
            return(map);
        }
        private static long[] getCommonItems(FastIDSet commonSet, PreferenceArray prefs1, int max)
        {
            long[] numArray = new long[max];
            int    num      = 0;

            for (int i = 0; i < prefs1.length(); i++)
            {
                long key = prefs1.getItemID(i);
                if (commonSet.contains(key))
                {
                    numArray[num++] = key;
                }
                if (num == max)
                {
                    return(numArray);
                }
            }
            return(numArray);
        }
Exemplo n.º 16
0
 private void buildAverageDiffs()
 {
     lock (this)
     {
         DataModel          model      = this.getDataModel();
         IEnumerator <long> enumerator = model.getUserIDs();
         while (enumerator.MoveNext())
         {
             long            current = enumerator.Current;
             PreferenceArray array   = model.getPreferencesFromUser(current);
             int             num2    = array.length();
             for (int i = 0; i < num2; i++)
             {
                 long  itemID = array.getItemID(i);
                 float num5   = array.getValue(i);
                 addDatumAndCreateIfNeeded(itemID, num5, this.itemAverages);
                 addDatumAndCreateIfNeeded(current, num5, this.userAverages);
                 this.overallAveragePrefValue.addDatum((double)num5);
             }
         }
     }
 }
        private void splitOneUsersPrefs(double trainingPercentage, FastByIDMap <PreferenceArray> trainingPrefs, FastByIDMap <PreferenceArray> testPrefs, long userID, DataModel dataModel)
        {
            List <Preference> prefs = null;
            List <Preference> list2 = null;
            PreferenceArray   array = dataModel.getPreferencesFromUser(userID);
            int num = array.length();

            for (int i = 0; i < num; i++)
            {
                Preference item = new GenericPreference(userID, array.getItemID(i), array.getValue(i));
                if (this.random.nextDouble() < trainingPercentage)
                {
                    if (prefs == null)
                    {
                        prefs = new List <Preference>(3);
                    }
                    prefs.Add(item);
                }
                else
                {
                    if (list2 == null)
                    {
                        list2 = new List <Preference>(3);
                    }
                    list2.Add(item);
                }
            }
            if (prefs != null)
            {
                trainingPrefs.put(userID, new GenericUserPreferenceArray(prefs));
                if (list2 != null)
                {
                    testPrefs.put(userID, new GenericUserPreferenceArray(list2));
                }
            }
        }
Exemplo n.º 18
0
 private void buildAverageDiffs()
 {
     lock (this)
     {
         DataModel          model      = this.getDataModel();
         IEnumerator <long> enumerator = model.getUserIDs();
         while (enumerator.MoveNext())
         {
             PreferenceArray array = model.getPreferencesFromUser(enumerator.Current);
             int             num   = array.length();
             for (int i = 0; i < num; i++)
             {
                 long           key     = array.getItemID(i);
                 RunningAverage average = this.itemAverages.get(key);
                 if (average == null)
                 {
                     average = new FullRunningAverage();
                     this.itemAverages.put(key, average);
                 }
                 average.addDatum((double)array.getValue(i));
             }
         }
     }
 }
Exemplo n.º 19
0
        public double userSimilarity(long userID1, long userID2)
        {
            double          num18;
            DataModel       model  = base.getDataModel();
            PreferenceArray array  = model.getPreferencesFromUser(userID1);
            PreferenceArray array2 = model.getPreferencesFromUser(userID2);
            int             num    = array.length();
            int             num2   = array2.length();

            if ((num == 0) || (num2 == 0))
            {
                return(double.NaN);
            }
            long   itemID = array.getItemID(0);
            long   num4   = array2.getItemID(0);
            int    i      = 0;
            int    num6   = 0;
            double num7   = 0.0;
            double num8   = 0.0;
            double num9   = 0.0;
            double num10  = 0.0;
            double sumXY  = 0.0;
            double num12  = 0.0;
            int    n      = 0;
            bool   flag   = this.inferrer != null;

            while (true)
            {
                int num14 = (itemID < num4) ? -1 : ((itemID > num4) ? 1 : 0);
                if (flag || (num14 == 0))
                {
                    double num15;
                    double num16;
                    if (itemID == num4)
                    {
                        num15 = array.getValue(i);
                        num16 = array2.getValue(num6);
                    }
                    else if (num14 < 0)
                    {
                        num15 = array.getValue(i);
                        num16 = this.inferrer.inferPreference(userID2, itemID);
                    }
                    else
                    {
                        num15 = this.inferrer.inferPreference(userID1, num4);
                        num16 = array2.getValue(num6);
                    }
                    sumXY += num15 * num16;
                    num7  += num15;
                    num8  += num15 * num15;
                    num9  += num16;
                    num10 += num16 * num16;
                    double num17 = num15 - num16;
                    num12 += num17 * num17;
                    n++;
                }
                if (num14 <= 0)
                {
                    if (++i >= num)
                    {
                        if (!flag || (num4 == 0x7fffffffffffffffL))
                        {
                            break;
                        }
                        itemID = 0x7fffffffffffffffL;
                    }
                    else
                    {
                        itemID = array.getItemID(i);
                    }
                }
                if (num14 >= 0)
                {
                    if (++num6 >= num2)
                    {
                        if (!flag || (itemID == 0x7fffffffffffffffL))
                        {
                            break;
                        }
                        num4 = 0x7fffffffffffffffL;
                    }
                    else
                    {
                        num4 = array2.getItemID(num6);
                    }
                }
            }
            if (this.centerData)
            {
                double num19 = num7 / ((double)n);
                double num20 = num9 / ((double)n);
                double num21 = sumXY - (num20 * num7);
                double num22 = num8 - (num19 * num7);
                double num23 = num10 - (num20 * num9);
                num18 = this.computeResult(n, num21, num22, num23, num12);
            }
            else
            {
                num18 = this.computeResult(n, sumXY, num8, num10, num12);
            }
            if (!double.IsNaN(num18))
            {
                num18 = this.normalizeWeightResult(num18, n, this.cachedNumItems);
            }
            return(num18);
        }
        public double userSimilarity(long userID1, long userID2)
        {
            int             num4;
            PreferenceArray array  = this.dataModel.getPreferencesFromUser(userID1);
            PreferenceArray array2 = this.dataModel.getPreferencesFromUser(userID2);
            int             num    = array.length();
            int             num2   = array2.length();

            if ((num <= 1) || (num2 <= 1))
            {
                return(double.NaN);
            }
            array  = array.clone();
            array2 = array2.clone();
            array.sortByValue();
            array2.sortByValue();
            float num3 = 1f;

            for (num4 = 0; num4 < num; num4++)
            {
                if (array2.hasPrefWithItemID(array.getItemID(num4)))
                {
                    array.setValue(num4, num3);
                    num3++;
                }
            }
            num3 = 1f;
            for (num4 = 0; num4 < num2; num4++)
            {
                if (array.hasPrefWithItemID(array2.getItemID(num4)))
                {
                    array2.setValue(num4, num3);
                    num3++;
                }
            }
            array.sortByItem();
            array2.sortByItem();
            long   num5  = array.getItemID(0);
            long   num6  = array2.getItemID(0);
            int    i     = 0;
            int    num8  = 0;
            double num9  = 0.0;
            int    num10 = 0;

            while (true)
            {
                int num11 = (num5 < num6) ? -1 : ((num5 > num6) ? 1 : 0);
                if (num11 == 0)
                {
                    double num12 = array.getValue(i) - array2.getValue(num8);
                    num9 += num12 * num12;
                    num10++;
                }
                if (num11 <= 0)
                {
                    if (++i >= num)
                    {
                        break;
                    }
                    num5 = array.getItemID(i);
                }
                if (num11 >= 0)
                {
                    if (++num8 >= num2)
                    {
                        break;
                    }
                    num6 = array2.getItemID(num8);
                }
            }
            if (num10 <= 1)
            {
                return(double.NaN);
            }
            return(1.0 - ((6.0 * num9) / ((double)(num10 * ((num10 * num10) - 1)))));
        }
Exemplo n.º 21
0
        protected void processLine <T>(string line, FastByIDMap <T> data, FastByIDMap <FastByIDMap <DateTime?> > timestamps, bool fromPriorData)
        {
            bool            flag2;
            int             num5;
            PreferenceArray array2;
            int             num6;
            float           num7;

            if ((line.Length == 0) || (line[0] == COMMENT_CHAR))
            {
                return;
            }
            string[] strArray        = line.Split(new char[] { this.delimiter });
            string   str             = strArray[0];
            string   str2            = strArray[1];
            string   str3            = strArray[2];
            bool     flag            = strArray.Length > 3;
            string   timestampString = flag ? strArray[3] : null;
            long     key             = this.readUserIDFromString(str);
            long     itemID          = this.readItemIDFromString(str2);

            if (this.transpose)
            {
                long num3 = key;
                key    = itemID;
                itemID = num3;
            }
            T local = data.get(key);

            if (!fromPriorData)
            {
                IEnumerable <Preference> source = (IEnumerable <Preference>)local;
                if (flag || !string.IsNullOrWhiteSpace(str3))
                {
                    num7  = float.Parse(str3, CultureInfo.InvariantCulture);
                    flag2 = false;
                    if (this.uniqueUserItemCheck && (source != null))
                    {
                        foreach (Preference preference in source)
                        {
                            if (preference.getItemID() == itemID)
                            {
                                flag2 = true;
                                preference.setValue(num7);
                                break;
                            }
                        }
                    }
                    if (!flag2)
                    {
                        if (source == null)
                        {
                            source = new List <Preference>(5);
                            data.put(key, (T)source);
                        }
                        if (source is IList <Preference> )
                        {
                            ((IList <Preference>)source).Add(new GenericPreference(key, itemID, num7));
                        }
                    }
                    this.addTimestamp(key, itemID, timestampString, timestamps);
                    return;
                }
                if (source != null)
                {
                    IEnumerator <Preference> enumerator = ((IEnumerable <Preference>)source.ToArray <Preference>()).GetEnumerator();
                    while (enumerator.MoveNext())
                    {
                        Preference current = enumerator.Current;
                        if (current.getItemID() == itemID)
                        {
                            if (source is IList <Preference> )
                            {
                                ((IList <Preference>)local).Remove(current);
                            }
                            break;
                        }
                    }
                }
                removeTimestamp(key, itemID, timestamps);
                return;
            }
            PreferenceArray array = (PreferenceArray)local;

            if (flag || !string.IsNullOrWhiteSpace(str3))
            {
                num7  = float.Parse(str3, CultureInfo.InvariantCulture);
                flag2 = false;
                if (this.uniqueUserItemCheck && (array != null))
                {
                    for (num5 = 0; num5 < array.length(); num5++)
                    {
                        if (array.getItemID(num5) == itemID)
                        {
                            flag2 = true;
                            array.setValue(num5, num7);
                            break;
                        }
                    }
                }
            }
            else
            {
                if (array != null)
                {
                    flag2 = false;
                    int num4 = array.length();
                    for (num5 = 0; num5 < num4; num5++)
                    {
                        if (array.getItemID(num5) == itemID)
                        {
                            flag2 = true;
                            break;
                        }
                    }
                    if (flag2)
                    {
                        if (num4 == 1)
                        {
                            data.remove(key);
                        }
                        else
                        {
                            array2 = new GenericUserPreferenceArray(num4 - 1);
                            num5   = 0;
                            for (num6 = 0; num5 < num4; num6++)
                            {
                                if (array.getItemID(num5) == itemID)
                                {
                                    num6--;
                                }
                                else
                                {
                                    array2.set(num6, array.get(num5));
                                }
                                num5++;
                            }
                            data.put(key, (T)array2);
                        }
                    }
                }
                removeTimestamp(key, itemID, timestamps);
                goto Label_02F1;
            }
            if (!flag2)
            {
                if (array == null)
                {
                    array = new GenericUserPreferenceArray(1);
                }
                else
                {
                    array2 = new GenericUserPreferenceArray(array.length() + 1);
                    num5   = 0;
                    for (num6 = 1; num5 < array.length(); num6++)
                    {
                        array2.set(num6, array.get(num5));
                        num5++;
                    }
                    array = array2;
                }
                array.setUserID(0, key);
                array.setItemID(0, itemID);
                array.setValue(0, num7);
                data.put(key, (T)array);
            }
Label_02F1:
            this.addTimestamp(key, itemID, timestampString, timestamps);
        }
Exemplo n.º 22
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));
        }