private void BuildRelatedCategories(IOrderedEnumerable<RelationInfo> categories, GridPanel categoriesPanel, RelationInfo originCategory) { IEnumerable<RelationInfo> calculatedCategories = (from category in categories where ( (category.RelatedCategory.IndexOf(originCategory.Category) > -1)) select category); foreach (RelationInfo calculatedCategory in calculatedCategories) { categoriesPanel.Controls.Add(GetSeparator()); categoriesPanel.Controls.Add(GetCategoryPanel(calculatedCategory)); BuildRelatedCategories(categories, categoriesPanel, calculatedCategory); } }
private static GridPanel GetCategoryPanel(RelationInfo link) { CategoryItem categoryItem = new CategoryItem(Client.ContentDatabase.GetItem(link.Category)); GridPanel categoryPanel = new GridPanel(); string categoryFullName = categoryItem.CategoryName; string categoryName = categoryItem.DisplayName; categoryPanel.Controls.Add(new Literal(string.Format("{0} ({1:F0}%)", categoryName, link.Weight)) {Class = "categoryName"}); categoryPanel.Controls.Add( new Literal(string.Format("{0}", categoryFullName.Substring(0, categoryFullName.Length - categoryName.Length - 1))) {Class = "categoryPath"}); categoryPanel.Attributes["class"] = "categoryPanel"; return categoryPanel; }
public static Dictionary<ID, RelationInfo> GetAllCategories([NotNull] Item item, [NotNull] string value) { Assert.ArgumentNotNull(item, "item"); Assert.ArgumentNotNull(value, "value"); Dictionary<ID, RelationInfo> result; if (string.IsNullOrEmpty(value)) { result = GetRecommendedCategories(item).ToDictionary( pair => pair.Key, pair => new RelationInfo { Category = pair.Key, RelatedCategory = new List<ID> { ID.Null }, Calculated = true, Weight = Convert.ToSingle(item.Database.GetItem(pair.Value)["Weight"]), ConflictCategory = GetConflictIDs(item.Database.GetItem(pair.Value)["ConflictTags"]).ToList() }); } else { result = ClassificationField.Parse(value).ToDictionary( pair => pair.Key, pair => { return new RelationInfo { Category = pair.Key, RelatedCategory = new List<ID> { ID.Null }, Calculated = false, Weight = Convert.ToSingle(item.Database.GetItem(pair.Value)["Weight"]), ConflictCategory = GetConflictIDs(item.Database.GetItem(pair.Value)["ConflictTags"]).ToList() }; }); } var queue = new Queue<RelationInfo>(result.Values); while (queue.Count > 0) { RelationInfo info = queue.Dequeue(); var innerItem = item.Database.GetItem(info.Category); if (innerItem == null) { continue; } var category = new CategoryItem(innerItem); var parentCategory = category.Parent; if (parentCategory == null) { continue; } RelationInfo parentInfo; if (result.TryGetValue(parentCategory.ID, out parentInfo)) { int entry = parentInfo.RelatedCategory.Count; float nWeight = (float)Math.Sqrt(parentCategory.RelatedWithChildren * category.RelatedToParent) * info.Weight; parentInfo.Weight = (float)Math.Pow(parentInfo.Weight, entry); parentInfo.Weight = (float)Math.Pow(parentInfo.Weight * nWeight, 1.0 / (entry + 1)); parentInfo.RelatedCategory.Add(category.ID); } else { parentInfo = new RelationInfo { Category = parentCategory.ID, RelatedCategory = new List<ID>{category.ID}, Calculated = true, Weight = (float) Math.Sqrt(parentCategory.RelatedWithChildren*category.RelatedToParent)*info.Weight }; result[parentInfo.Category] = parentInfo; } queue.Enqueue(parentInfo); } return result; }
public static Dictionary <ID, RelationInfo> GetAllCategories([NotNull] Item item, [NotNull] string value) { Assert.ArgumentNotNull(item, "item"); Assert.ArgumentNotNull(value, "value"); Dictionary <ID, RelationInfo> result; if (string.IsNullOrEmpty(value)) { result = GetRecommendedCategories(item).ToDictionary( pair => pair.Key, pair => new RelationInfo { Category = pair.Key, RelatedCategory = new List <ID> { ID.Null }, Calculated = true, Weight = Convert.ToSingle(item.Database.GetItem(pair.Value)["Weight"]), ConflictCategory = GetConflictIDs(item.Database.GetItem(pair.Value)["ConflictTags"]).ToList() }); } else { result = ClassificationField.Parse(value).ToDictionary( pair => pair.Key, pair => { return(new RelationInfo { Category = pair.Key, RelatedCategory = new List <ID> { ID.Null }, Calculated = false, Weight = Convert.ToSingle(item.Database.GetItem(pair.Value)["Weight"]), ConflictCategory = GetConflictIDs(item.Database.GetItem(pair.Value)["ConflictTags"]).ToList() }); }); } var queue = new Queue <RelationInfo>(result.Values); while (queue.Count > 0) { RelationInfo info = queue.Dequeue(); var innerItem = item.Database.GetItem(info.Category); if (innerItem == null) { continue; } var category = new CategoryItem(innerItem); var parentCategory = category.Parent; if (parentCategory == null) { continue; } RelationInfo parentInfo; if (result.TryGetValue(parentCategory.ID, out parentInfo)) { int entry = parentInfo.RelatedCategory.Count; float nWeight = (float)Math.Sqrt(parentCategory.RelatedWithChildren * category.RelatedToParent) * info.Weight; parentInfo.Weight = (float)Math.Pow(parentInfo.Weight, entry); parentInfo.Weight = (float)Math.Pow(parentInfo.Weight * nWeight, 1.0 / (entry + 1)); parentInfo.RelatedCategory.Add(category.ID); } else { parentInfo = new RelationInfo { Category = parentCategory.ID, RelatedCategory = new List <ID> { category.ID }, Calculated = true, Weight = (float)Math.Sqrt(parentCategory.RelatedWithChildren * category.RelatedToParent) * info.Weight }; result[parentInfo.Category] = parentInfo; } queue.Enqueue(parentInfo); } return(result); }