/// <summary> /// Compare 2 integer /// </summary> /// <param name="a">distance a</param> /// <param name="b">distance b</param> /// <returns></returns> private static int CompareByDistance(MobileResource a, MobileResource b) { if (a.Distance < b.Distance) { return(-1); } else if (a.Distance < b.Distance) { return(1); } else { return(0); } }
/// <summary> /// Create and filter the suggestion link in a list /// </summary> /// <param name="intendedPage"></param> private void MakeSuggestions(string intendedPage) { //Set it to desire likeness of words using Levenshtein Distance Algorithm //.60 to .70 is the suggested distance, default is .60 double showPageNotFoundDistance = AppLogic.AppConfigNativeDouble("404.ComparisonDistance"); // Default value is 5 int numberOfSuggestedLinks = AppLogic.AppConfigNativeInt("404.NumberOfSuggestedLinks"); intendedPage = intendedPage.ToLowerInvariant(); List <MobileResource> matchingResources = new List <MobileResource>(); for (int ctr = 0; ctr < this._resources.Count; ctr++) { MobileResource resource = _resources[ctr]; //Compute for the distance using Levenshtein Distance Algorithm double distance = MobileLevenshteinDistance.CalcDistance(resource.Name, intendedPage); // both in lower case //Divide it by resource name length for more accuracy double meanDistancePerLetter = (distance / resource.Name.Length); resource.Distance = meanDistancePerLetter; if (meanDistancePerLetter <= showPageNotFoundDistance) { matchingResources.Add(resource); } } // Sort to get the best matchingResources.Sort(CompareByDistance); int count = 0; if (matchingResources.Count > numberOfSuggestedLinks) { count = matchingResources.Count - numberOfSuggestedLinks; } // remove the excess base on the set limit in 404.NumberOfSuggestedLinks appconfig matchingResources.RemoveRange(0, count); if (matchingResources.Count > 0) { DisplaySuggestions(matchingResources); } }