public static CatDistResult search(Category one, Category two, Dictionary <Category, Dictionary <Category, int> > distMap, int threshhold) { var result = oneIsParentOfTwo(one, two, distMap); if (result != null) { return(result); } result = oneIsParentOfTwo(two, one, distMap); if (result != null) { return(result); } var commonParents = allCommonParents(one, two, distMap, threshhold); if (commonParents.Any()) { var maxDist = commonParents.Max(r => r.dist()); result = commonParents.First(r => r.dist() == maxDist); } else { result = new CatDistResult(new Category() { Name = "Not found" }, -1, -1); } return(result); }
private static CatDistResult oneIsParentOfTwo(Category one, Category two, Dictionary <Category, Dictionary <Category, int> > distMap) { if (distMap.ContainsKey(one)) { if (distMap[one].ContainsKey(two)) { var result = new CatDistResult(one, 0, distMap[one][two]); return(result); } } return(null); }
private static List <CatDistResult> allCommonParents(Category one, Category two, Dictionary <Category, Dictionary <Category, int> > distMap, int threshhold) { var results = new List <CatDistResult>(); int size = 0; foreach (var parent in distMap.Reverse()) { if (parent.Value.ContainsKey(one) && parent.Value.ContainsKey(two)) { var r = new CatDistResult(parent.Key, parent.Value[one], parent.Value[two]); results.Add(r); if (++size >= threshhold) { break; } } } return(results); }
public static IEnumerable <CatLinkResult> groupSimilarWords(Category parent, IEnumerable <Tuple <Category, double> > similarCategories, int searchDistThreshhold, Dictionary <Category, Dictionary <Category, int> > realCatMap) { var thisGroupSimilar = similarCategories.Select(x => new CatLinkResult() { category = parent, similarCategory = x.Item1, val = x.Item2, num = 1, dist = CatDistResult.search(parent, x.Item1, realCatMap, searchDistThreshhold) }); return(thisGroupSimilar); }