public void Calculate(Profile profile, List <ProfiledItem> list, string profileName, ProfileItem profileItem) { list.ForEach(item => item.AssignContentProfile(profile, profileName)); var profiledList = list.Where(pi => pi.ContentProfile != null).ToList(); PatternSpace space = profileItem.PatternSpace; var zeroSize = space.Dimensions; var zeroDouble = new double[zeroSize]; for (int i = 0; i < space.Dimensions; i++) { zeroDouble[i] = 0.0d; } Pattern zeroPattern = new Pattern(space, zeroDouble); Pattern visitorPattern = zeroPattern; if (profile != null) { double[] numArray = new double[space.Dimensions]; bool calcPercentage = (profileItem.Type.Equals("percentage", StringComparison.InvariantCultureIgnoreCase)); if (profile.Count > 0 && calcPercentage) { double percentageMultiplier = (calcPercentage) ? 100.0d : 1.0d; double total = profile.Total; foreach (var keyValuePair in profile) { numArray[space.GetKeyIndex(keyValuePair.Key)] = (keyValuePair.Value / total) * percentageMultiplier; } var profileType = profileItem.Type; visitorPattern = new Pattern(space, numArray); } else if (profile.Count > 0) { double percentageMultiplier = calcPercentage ? 100.0d : 1.0d; double total = profile.Total; int count = profile.Count; foreach (var keyValuePair in profile) { numArray[space.GetKeyIndex(keyValuePair.Key)] = keyValuePair.Value / count; } var profileType = profileItem.Type; visitorPattern = new Pattern(space, numArray); } } profiledList.ForEach(item => CalculateProfiledItemDistance(calculator, item, profileName, profileItem, visitorPattern)); }
private void CalculateProfiledItemDistance(IPatternDistance calculator, ProfiledItem item, string profileName, ProfileItem profileItem, Pattern visitorPattern) { PatternSpace space = profileItem.PatternSpace; Pattern itemPattern = space.CreatePattern(item.ContentProfile); double distance = defaultDistance; if (visitorPattern != null) { distance = Math.Sqrt(calculator.GetDistance(visitorPattern, itemPattern)); } var correctedDistance = distance; if (correctedDistance < minDistance) { correctedDistance = minDistance; } double gravity = 1.0d / (correctedDistance * correctedDistance); item.ProfiledItemCalculation = new ProfiledItemDistance(distance, gravity); }