Exemple #1
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();
            }
        }
Exemple #2
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;
        }
Exemple #3
0
 public void storeMapping(long longID, String stringID)
 {
     synchronized(longToString)
     {
         longToString.put(longID, stringID);
     }
 }
Exemple #4
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);
        }
Exemple #5
0
        private FastByIDMap <String> buildMapping()
        {
            FastByIDMap <String> mapping = new FastByIDMap <String>();

            for (String line : new FileLineIterable(dataFile))
            {
                mapping.put(tolongID(line), line);
            }
            lastModified = dataFile.lastModified();
            return(mapping);
        }
        private static FastByIDMap <int?> createIDMapping(int size, IEnumerator <long> idIterator)
        {
            FastByIDMap <int?> map = new FastByIDMap <int?>(size);
            int num = 0;

            while (idIterator.MoveNext())
            {
                map.put(idIterator.Current, new int?(num++));
            }
            return(map);
        }
        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);
        }
Exemple #8
0
        public static FastByIDMap <PreferenceArray> toDataMap(FastByIDMap <List <Preference> > data, bool byUser)
        {
            FastByIDMap <PreferenceArray> map = new FastByIDMap <PreferenceArray>(data.size());

            foreach (KeyValuePair <long, List <Preference> > pair in data.entrySet())
            {
                List <Preference> prefs = pair.Value;
                map.put(pair.Key, byUser ? ((PreferenceArray) new GenericUserPreferenceArray(prefs)) : ((PreferenceArray) new GenericItemPreferenceArray(prefs)));
            }
            return(map);
        }
Exemple #9
0
        public static FastByIDMap <PreferenceArray> toDataMap(DataModel dataModel)
        {
            FastByIDMap <PreferenceArray> map        = new FastByIDMap <PreferenceArray>(dataModel.getNumUsers());
            IEnumerator <long>            enumerator = dataModel.getUserIDs();

            while (enumerator.MoveNext())
            {
                long current = enumerator.Current;
                map.put(current, dataModel.getPreferencesFromUser(current));
            }
            return(map);
        }
        private static FastByIDMap <FastIDSet> computeClustersPerUserID(IList <FastIDSet> clusters)
        {
            FastByIDMap <FastIDSet> clustersPerUser = new FastByIDMap <FastIDSet>(clusters.Count);

            foreach (FastIDSet cluster in clusters)
            {
                var it = cluster.GetEnumerator();
                while (it.MoveNext())
                {
                    clustersPerUser.put(it.Current, cluster);
                }
            }
            return(clustersPerUser);
        }
Exemple #11
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));
     }
 }
        private FastByIDMap <List <RecommendedItem> > computeTopRecsPerUserID(List <FastIDSet> clusters)
        {
            FastByIDMap <List <RecommendedItem> > recsPerUser = new FastByIDMap <List <RecommendedItem> >();

            foreach (FastIDSet cluster in clusters)
            {
                List <RecommendedItem> recs = computeTopRecsForCluster(cluster);
                var it = cluster.GetEnumerator();
                while (it.MoveNext())
                {
                    recsPerUser.put(it.Current, recs);
                }
            }
            return(recsPerUser);
        }
        public void processOtherUser(long userID, FastIDSet relevantItemIDs, FastByIDMap <PreferenceArray> trainingUsers, long otherUserID, DataModel dataModel)
        {
            PreferenceArray array = dataModel.getPreferencesFromUser(otherUserID);

            if (userID == otherUserID)
            {
                List <Preference> prefs = new List <Preference>(array.length());
                foreach (Preference preference in array)
                {
                    if (!relevantItemIDs.contains(preference.getItemID()))
                    {
                        prefs.Add(preference);
                    }
                }
                if (prefs.Count > 0)
                {
                    trainingUsers.put(otherUserID, new GenericUserPreferenceArray(prefs));
                }
            }
            else
            {
                trainingUsers.put(otherUserID, array);
            }
        }
        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);
        }
Exemple #15
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);
        }
Exemple #16
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);
         }
     }
 }
        public static Factorization readBinary(Stream inFile)
        {
            int                num4;
            int                num7;
            BinaryReader       reader        = new BinaryReader(inFile);
            int                num           = reader.ReadInt32();
            int                size          = reader.ReadInt32();
            int                num3          = reader.ReadInt32();
            FastByIDMap <int?> userIDMapping = new FastByIDMap <int?>(size);

            double[][] userFeatures = new double[size][];
            for (num4 = 0; num4 < size; num4++)
            {
                int  index = reader.ReadInt32();
                long key   = reader.ReadInt64();
                userFeatures[index] = new double[num];
                userIDMapping.put(key, new int?(index));
                num7 = 0;
                while (num7 < num)
                {
                    userFeatures[index][num7] = reader.ReadDouble();
                    num7++;
                }
            }
            FastByIDMap <int?> itemIDMapping = new FastByIDMap <int?>(num3);

            double[][] itemFeatures = new double[num3][];
            for (num4 = 0; num4 < num3; num4++)
            {
                int  num8 = reader.ReadInt32();
                long num9 = reader.ReadInt64();
                itemFeatures[num8] = new double[num];
                itemIDMapping.put(num9, new int?(num8));
                for (num7 = 0; num7 < num; num7++)
                {
                    itemFeatures[num8][num7] = reader.ReadDouble();
                }
            }
            return(new Factorization(userIDMapping, itemIDMapping, userFeatures, itemFeatures));
        }
        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));
                }
            }
        }
Exemple #19
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);
        }
 private FastByIDMap<String> buildMapping() {
   FastByIDMap<String> mapping = new FastByIDMap<String>();
   for (String line : new FileLineIterable(dataFile)) {
     mapping.put(tolongID(line), line);
   }
   lastModified = dataFile.lastModified();
   return mapping;
 }