/// <summary>
        /// Generates the negative ratings.
        /// </summary>
        /// <param name="itemHistogram">The item histogram.</param>
        private void GenerateNegativeRatings(IDictionary <TItem, int> itemHistogram)
        {
            // Create the binary sum tree.
            var items            = itemHistogram.Keys.Select(key => key).ToList();
            var histogramSampler = new HistogramSampler(
                itemHistogram.Values.Select(value => Convert.ToInt32(Math.Pow(value, this.itemHistogramAdjustment))));

            foreach (var userToRatedItems in this.ratedInstances)
            {
                var ratedItems = userToRatedItems.Value;

                // In case the user has liked more than half of the items
                int samplesCount = Math.Min(ratedItems.Count, items.Count - ratedItems.Count);

                for (int i = 0; i < samplesCount; ++i)
                {
                    // This is required not to run into an infinite loop.
                    for (int sampleAttempts = 0; sampleAttempts < SampleAttemptsCount; ++sampleAttempts)
                    {
                        var itemIndex = histogramSampler.Sample();
                        var ratedItem = new RatedItem(items[itemIndex], NegativeRatingValue);

                        if (!ratedItems.Contains(ratedItem))
                        {
                            histogramSampler.Take(itemIndex);
                            ratedItems.Add(ratedItem);
                            break;
                        }
                    }
                }
            }
        }
Exemplo n.º 2
0
        public static RatedItem FromViewModel(RatedItemBaseViewModel vm)
        {
            var entity = new RatedItem();

            FromViewModel(vm, entity);
            return(entity);
        }
Exemplo n.º 3
0
 public static RatedItemViewModel ToViewModel(RatedItem entity)
 {
     return(new RatedItemViewModel()
     {
         Id = entity.Id,
         Name = entity.Name,
         Rating = entity.Rating
     });
 }
        /// <summary>
        /// Generates the positive ratings and the item histogram.
        /// </summary>
        /// <param name="instanceSource">The source of positive instances.</param>
        /// <param name="itemHistogram">The item histogram.</param>
        /// <remarks>
        /// The generation of these two entities is implemented in the same method for performance reasons.
        /// By doing this, iteration over the positive observations is performed only once.
        /// </remarks>
        private void GeneratePositiveRatingsAndItemHistogram(
            TInstanceSource instanceSource, out IDictionary <TItem, int> itemHistogram)
        {
            itemHistogram = new Dictionary <TItem, int>();
            var instances = this.mapping.GetInstances(instanceSource);

            foreach (TInstance instance in instances)
            {
                TUser user = this.mapping.GetUser(instanceSource, instance);
                TItem item = this.mapping.GetItem(instanceSource, instance);

                // Update the item histogram
                if (!itemHistogram.ContainsKey(item))
                {
                    itemHistogram.Add(item, 0);
                }

                ++itemHistogram[item];

                // Update the rated instances
                HashSet <RatedItem> ratedItems;
                if (!this.ratedInstances.TryGetValue(user, out ratedItems))
                {
                    ratedItems = new HashSet <RatedItem>();
                    this.ratedInstances.Add(user, ratedItems);
                }

                var ratedItem = new RatedItem(item, PositiveRatingValue);
                if (ratedItems.Contains(ratedItem))
                {
                    throw new NotSupportedException(
                              string.Format(
                                  "Multiple occurances of the same user-item pair are not supported. User: {0}, Item: {1}.",
                                  user,
                                  item));
                }

                ratedItems.Add(ratedItem);
            }
        }
Exemplo n.º 5
0
 protected bool Equals(RatedItem other)
 {
     return(string.Equals(Name, other.Name) && AvgRating.Equals(other.AvgRating) && Ratings == other.Ratings);
 }
Exemplo n.º 6
0
 public static void FromViewModel(RatedItemBaseViewModel vm, RatedItem entity)
 {
     entity.Name   = vm.Name;
     entity.Rating = vm.Rating;
 }