/// <summary> /// Compare the specified mail address again correctMailDomainNames, and try to correct the given mailAddress. /// </summary> /// <param name="correctMailDomainNames">The collection of correct mail domain names.</param> /// <param name="mailAddress">The mail address which will be corrected. It should be a valid mail address format. Otherwise, an exception will be thrown</param> /// <returns>The corrected mail address. If empty string, it indicates the given mailAddress is already correct.</returns> public static string CorrectMailAddress(IEnumerable <string> correctMailDomainNames, string mailAddress) { var correctedMailAddress = StringUtility.RemoveWhiteSpaces(mailAddress); correctedMailAddress = StringUtility.ToDBC(correctedMailAddress); // Simply parsing the given mailAddress var atIndex = correctedMailAddress.IndexOf('@'); if ((atIndex <= 0) || (atIndex == (correctedMailAddress.Length - 1))) { throw new FormatException("The given mailAddress format is invalid. " + mailAddress); } var account = correctedMailAddress.Substring(0, atIndex); var domainName = correctedMailAddress.Substring(atIndex + 1); //var lastDotIndex = domainName.LastIndexOf('.'); //if ((lastDotIndex <= 0) || (lastDotIndex == (domainName.Length - 1))) //{ // throw new FormatException("The given mailAddress format is invalid. " + mailAddress); //} //var domainNameFirst = correctedMailAddress.Substring(0, lastDotIndex); //var domainNameSecond = correctedMailAddress.Substring(lastDotIndex + 1); var minDistance = 99; var minDistanceDomainName = string.Empty; var moreThanOne = false; foreach (var correctMailDomainName in correctMailDomainNames) { var distance = StringUtility.CalculateLevenshteinDistance(domainName, correctMailDomainName); if (distance <= 0) { // Exactly match, return return(ComputeCorrectMailAddressResult(mailAddress, account, correctMailDomainName)); } if (minDistance == distance) { // Find one more candidate moreThanOne = true; } else if (minDistance > distance) { minDistance = distance; moreThanOne = false; minDistanceDomainName = correctMailDomainName; } } if (moreThanOne) { // If there are more than one candidates which have same distance, do not correct. return(ComputeCorrectMailAddressResult(mailAddress, account, domainName)); } else { if (minDistance < 2) { // Only correct 1 difference mailAddress return(ComputeCorrectMailAddressResult(mailAddress, account, minDistanceDomainName)); } else { // Do not correct return(ComputeCorrectMailAddressResult(mailAddress, account, domainName)); } } }