被推荐给用户的物品。
Beispiel #1
0
        public IEnumerable <RecommendedItem> Recommend(string userId, int topN, ISet <string> ignoredItems)
        {
            var result   = new Dictionary <string, RecommendedItem>();
            var features = _featureBuilder.BuildFeatures(userId);

            foreach (var feature in features)
            {
                foreach (var provider in _relatedItemsProviders)
                {
                    var relatedItems = provider.GetRelatedItems(feature.Id, topN, ignoredItems);
                    foreach (var item in relatedItems)
                    {
                        RecommendedItem recommendedItem;

                        if (!result.TryGetValue(item.Key, out recommendedItem))
                        {
                            recommendedItem = new RecommendedItem
                            {
                                ItemId = item.Key
                            };
                            result.Add(item.Key, recommendedItem);
                        }

                        var weight = item.Value * feature.Weight;

                        recommendedItem.Weight += weight;

                        if (recommendedItem.Reasons.ContainsKey(feature.Id))
                        {
                            recommendedItem.Reasons[feature.Id] += weight;
                        }
                        else
                        {
                            recommendedItem.Reasons.Add(feature.Id, weight);
                        }
                    }
                }
            }

            return(result.Values.OrderByDescending(x => x.Weight).Take(topN).ToList());
        }
        public IEnumerable<RecommendedItem> Recommend(string userId, int topN, ISet<string> ignoredItems)
        {
            var result = new Dictionary<string, RecommendedItem>();
            var features = _featureBuilder.BuildFeatures(userId);
            foreach (var feature in features)
            {
                foreach (var provider in _relatedItemsProviders)
                {
                    var relatedItems = provider.GetRelatedItems(feature.Id, topN, ignoredItems);
                    foreach (var item in relatedItems)
                    {
                        RecommendedItem recommendedItem;

                        if (!result.TryGetValue(item.Key, out recommendedItem))
                        {
                            recommendedItem = new RecommendedItem
                            {
                                ItemId = item.Key
                            };
                            result.Add(item.Key, recommendedItem);
                        }

                        var weight = item.Value * feature.Weight;

                        recommendedItem.Weight += weight;

                        if (recommendedItem.Reasons.ContainsKey(feature.Id))
                        {
                            recommendedItem.Reasons[feature.Id] += weight;
                        }
                        else
                        {
                            recommendedItem.Reasons.Add(feature.Id, weight);
                        }
                    }
                }
            }

            return result.Values.OrderByDescending(x => x.Weight).Take(topN).ToList();
        }