Exemplo n.º 1
0
        private long processLine(String line, long averageCount)
        {
            if (string.IsNullOrEmpty(line) || line[0] == COMMENT_CHAR)
            {
                return(averageCount);
            }

            String[] tokens = SEPARATOR.Split(line);
            //Preconditions.checkArgument(tokens.length >= 3 && tokens.length != 5, "Bad line: %s", line);

            long   itemID1 = long.Parse(tokens[0]);
            long   itemID2 = long.Parse(tokens[1]);
            double diff    = double.Parse(tokens[2]);
            int    count   = tokens.Length >= 4 ? int.Parse(tokens[3]) : 1;
            bool   hasMkSk = tokens.Length >= 5;

            if (itemID1 > itemID2)
            {
                long temp = itemID1;
                itemID1 = itemID2;
                itemID2 = temp;
            }

            FastByIDMap <RunningAverage> level1Map = averageDiffs.get(itemID1);

            if (level1Map == null)
            {
                level1Map = new FastByIDMap <RunningAverage>();
                averageDiffs.put(itemID1, level1Map);
            }
            RunningAverage average = level1Map.get(itemID2);

            if (average != null)
            {
                throw new Exception("Duplicated line for item-item pair " + itemID1 + " / " + itemID2);
            }
            if (averageCount < maxEntries)
            {
                if (hasMkSk)
                {
                    double mk = Double.Parse(tokens[4]);
                    double sk = Double.Parse(tokens[5]);
                    average = new FullRunningAverageAndStdDev(count, diff, mk, sk);
                }
                else
                {
                    average = new FullRunningAverage(count, diff);
                }
                level1Map.put(itemID2, average);
                averageCount++;
            }

            allRecommendableItemIDs.add(itemID1);
            allRecommendableItemIDs.add(itemID2);

            return(averageCount);
        }
Exemplo n.º 2
0
        public double userSimilarity(long userID1, long userID2)
        {
            long num;
            long num2;

            if (userID1 == userID2)
            {
                return(1.0);
            }
            if (userID1 < userID2)
            {
                num  = userID1;
                num2 = userID2;
            }
            else
            {
                num  = userID2;
                num2 = userID1;
            }
            FastByIDMap <double> map = this.similarityMaps.get(num);

            if (map == null)
            {
                return(double.NaN);
            }
            return(map.get(num2));
        }
Exemplo n.º 3
0
        public double itemSimilarity(long itemID1, long itemID2)
        {
            long num;
            long num2;

            if (itemID1 == itemID2)
            {
                return(1.0);
            }
            if (itemID1 < itemID2)
            {
                num  = itemID1;
                num2 = itemID2;
            }
            else
            {
                num  = itemID2;
                num2 = itemID1;
            }
            FastByIDMap <double?> map = this.similarityMaps.get(num);

            if (map == null)
            {
                return(double.NaN);
            }
            double?nullable = map.get(num2);

            return(!nullable.HasValue ? double.NaN : nullable.Value);
        }
Exemplo n.º 4
0
 public String toStringID(long longID)
 {
     synchronized(longToString)
     {
         return(longToString.get(longID));
     }
 }
Exemplo n.º 5
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.º 6
0
        public GenericDataModel(FastByIDMap <PreferenceArray> userData, FastByIDMap <FastByIDMap <DateTime?> > timestamps)
        {
            this.preferenceFromUsers = userData;
            FastByIDMap <List <Preference> > data = new FastByIDMap <List <Preference> >();
            FastIDSet set = new FastIDSet();
            int       num = 0;
            float     negativeInfinity = float.NegativeInfinity;
            float     positiveInfinity = float.PositiveInfinity;

            foreach (KeyValuePair <long, PreferenceArray> pair in this.preferenceFromUsers.entrySet())
            {
                PreferenceArray array = pair.Value;
                array.sortByItem();
                foreach (Preference preference in array)
                {
                    long key = preference.getItemID();
                    set.add(key);
                    List <Preference> list = data.get(key);
                    if (list == null)
                    {
                        list = new List <Preference>(2);
                        data.put(key, list);
                    }
                    list.Add(preference);
                    float num5 = preference.getValue();
                    if (num5 > negativeInfinity)
                    {
                        negativeInfinity = num5;
                    }
                    if (num5 < positiveInfinity)
                    {
                        positiveInfinity = num5;
                    }
                }
                if ((++num % 0x2710) == 0)
                {
                    log.info("Processed {0} users", new object[] { num });
                }
            }
            log.info("Processed {0} users", new object[] { num });
            this.setMinPreference(positiveInfinity);
            this.setMaxPreference(negativeInfinity);
            this.itemIDs = set.toArray();
            set          = null;
            Array.Sort <long>(this.itemIDs);
            this.preferenceForItems = toDataMap(data, false);
            foreach (KeyValuePair <long, PreferenceArray> pair in this.preferenceForItems.entrySet())
            {
                pair.Value.sortByUser();
            }
            this.userIDs = new long[userData.size()];
            int num6 = 0;

            foreach (long num7 in userData.Keys)
            {
                this.userIDs[num6++] = num7;
            }
            Array.Sort <long>(this.userIDs);
            this.timestamps = timestamps;
        }
Exemplo n.º 7
0
        private static void removeTimestamp(long userID, long itemID, FastByIDMap <FastByIDMap <DateTime?> > timestamps)
        {
            FastByIDMap <DateTime?> map = timestamps.get(userID);

            if (map != null)
            {
                map.remove(itemID);
            }
        }
Exemplo n.º 8
0
 protected void processLineWithoutID(string line, FastByIDMap <FastIDSet> data, FastByIDMap <FastByIDMap <DateTime?> > timestamps)
 {
     if (!string.IsNullOrWhiteSpace(line) && (line[0] != COMMENT_CHAR))
     {
         FastIDSet set;
         string[]  strArray        = line.Split(new char[] { this.delimiter });
         string    str             = strArray[0];
         string    str2            = strArray[1];
         bool      flag            = strArray.Length > 2;
         string    str3            = flag ? strArray[2] : "";
         bool      flag2           = strArray.Length > 3;
         string    timestampString = flag2 ? strArray[3] : null;
         long      key             = this.readUserIDFromString(str);
         long      num2            = this.readItemIDFromString(str2);
         if (this.transpose)
         {
             long num3 = key;
             key  = num2;
             num2 = num3;
         }
         if ((flag && !flag2) && string.IsNullOrEmpty(str3))
         {
             set = data.get(key);
             if (set != null)
             {
                 set.remove(num2);
             }
             removeTimestamp(key, num2, timestamps);
         }
         else
         {
             set = data.get(key);
             if (set == null)
             {
                 set = new FastIDSet(2);
                 data.put(key, set);
             }
             set.add(num2);
             this.addTimestamp(key, num2, timestampString, timestamps);
         }
     }
 }
        private static void addDatumAndCreateIfNeeded(long itemID, float value, FastByIDMap <RunningAverage> averages)
        {
            RunningAverage average = averages.get(itemID);

            if (average == null)
            {
                average = new FullRunningAverage();
                averages.put(itemID, average);
            }
            average.addDatum((double)value);
        }
Exemplo n.º 10
0
 private void addTimestamp(long userID, long itemID, string timestampString, FastByIDMap <FastByIDMap <DateTime?> > timestamps)
 {
     if (timestampString != null)
     {
         FastByIDMap <DateTime?> map = timestamps.get(userID);
         if (map == null)
         {
             map = new FastByIDMap <DateTime?>();
             timestamps.put(userID, map);
         }
         DateTime time = this.readTimestampFromString(timestampString);
         map.put(itemID, new DateTime?(time));
     }
 }
Exemplo n.º 11
0
        public override DateTime?getPreferenceTime(long userID, long itemID)
        {
            if (this.timestamps == null)
            {
                return(null);
            }
            FastByIDMap <DateTime?> map = this.timestamps.get(userID);

            if (map == null)
            {
                throw new NoSuchUserException(userID);
            }
            return(map.get(itemID));
        }
Exemplo n.º 12
0
        public common.RunningAverage getDiff(long itemID1, long itemID2)
        {
            bool inverted = false;

            if (itemID1 > itemID2)
            {
                inverted = true;
                long temp = itemID1;
                itemID1 = itemID2;
                itemID2 = temp;
            }

            FastByIDMap <RunningAverage> level2Map;

            try
            {
                //buildAverageDiffsLock.EnterReadLock();
                lock (this)
                {
                    level2Map = averageDiffs.get(itemID1);
                }
            }
            finally
            {
                //buildAverageDiffsLock.ExitReadLock();
            }
            RunningAverage average = null;

            if (level2Map != null)
            {
                average = level2Map.get(itemID2);
            }
            if (inverted)
            {
                if (average == null)
                {
                    return(null);
                }
                else
                {
                    return(average.inverse());
                };

                //return average == null ? null : average.inverse();
            }
            else
            {
                return(average);
            }
        }
Exemplo n.º 13
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.º 14
0
 public common.RunningAverage getAverageItemPref(long itemID)
 {
     return(averageItemPref.get(itemID));
 }
Exemplo n.º 15
0
 public String toStringID(long longID)
 {
     return(longToString.get(longID));
 }