private double AdjustForChildren(SecondLevelAddress s1, SecondLevelAddress s2, double weight, double score) { if (s1.Child != null && s2.Child != null) { score = score * 0.7 + MatchSecondLevelAddr(s1.Child, s2.Child, weight) * 0.3; } else if (s1.Child != null || s2.Child != null) { //Adjust score a bit as no perfect match score = score * 0.8; } return(score); }
private double MatchSecondLevelAddr(SecondLevelAddress s1, SecondLevelAddress s2, double weight) { double maxScore = 0; if (s1.Value != null && s2.Value != null) { if (s1.Prefix != null && s2.Prefix != null) { maxScore = base.Distance(s1.Value, s2.Value) * 0.8; maxScore += base.Distance(s1.Prefix, s2.Prefix) * 0.2; maxScore = AdjustForChildren(s1, s2, weight, maxScore); } if (s1.Prefix != null) { double score = TryMatch(String.Format("{0} {1}", s1.Prefix, s1.Value), s2.Value, maxScore, 1.0); maxScore = Math.Max(maxScore, AdjustForChildren(s1, s2, weight, score)); } if (s2.Prefix != null) { double score = TryMatch(String.Format("{0} {1}", s2.Prefix, s2.Value), s1.Value, maxScore, 1.0); maxScore = Math.Max(maxScore, AdjustForChildren(s1, s2, weight, score)); } maxScore = TryMatch(s1.Value, s2.Value, maxScore, (s1.Prefix == null && s2.Prefix == null) ? 1.0 : 0.7); } else { if (s1.Value != null) { maxScore = TryMatch(s1.Prefix + s1.Value, s2.Prefix, maxScore, 0.6); maxScore = TryMatch(s1.Value, s2.Prefix, maxScore, 0.6); } if (s2.Value != null) { maxScore = TryMatch(s2.Prefix + s2.Value, s1.Prefix, maxScore, 0.6); maxScore = TryMatch(s2.Value, s1.Prefix, maxScore, 0.6); } } maxScore = TryMatch(s1.ToStringWithPref(), s2.ToStringWithPref(), maxScore, 0.8); maxScore = TryMatch(s1.ToStringNoPref(), s2.ToStringNoPref(), maxScore, 0.5); return(maxScore * weight); }
internal StreetAddress Normalize(string addr) { String[] addrComponents = Regex.Split(addr.ToUpper().Trim(), "\\s+"); if (addrComponents.Length == 0) { return(null); } StreetAddress address = new StreetAddress() { Number = addrComponents[0], }; SecondLevelAddress activeAddrL2 = null; var buffer = new StringBuilder(); for (int i = 1; i < addrComponents.Length; i++) { String test = ExtractString(addrComponents[i]); String strSuffix = indexStreet.Lookup(test); String secondLevel = indexSecondary.Lookup(test); if (strSuffix != null && !IsNextSpecial(addrComponents, i + 1)) { address.Name = buffer.ToString(); address.Suffix = strSuffix; buffer = new StringBuilder(); } else if (secondLevel != null) { var s2String = buffer.ToString(); SecondLevelAddress newAddrL2 = new SecondLevelAddress(); if (address.Name == null) { address.Name = s2String.Replace("-", ""); } else { if (activeAddrL2 == null) { newAddrL2.Value = s2String.Replace("-", ""); } else { activeAddrL2.Value = s2String.Replace("-", ""); } } buffer = new StringBuilder(); newAddrL2.Prefix = secondLevel; if (address.SecondLevel == null) { address.SecondLevel = newAddrL2; } else { activeAddrL2.Child = newAddrL2; } activeAddrL2 = newAddrL2; } else { buffer.Append(addrComponents[i]); } } var bufferString = buffer.ToString(); if (!String.IsNullOrEmpty(bufferString)) { if (address.Name == null) { address.Name = bufferString.Replace("-", ""); } else { if (activeAddrL2 == null) { activeAddrL2 = new SecondLevelAddress(); address.SecondLevel = activeAddrL2; } activeAddrL2.Value = bufferString.Replace("-", ""); } } return(address); }