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)))));
        }
Esempio n. 2
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);
        }