Ejemplo n.º 1
0
        private static string TestAddress(Data data, ref Address address, string stringToTest, int maxEditDistance)
        {
            int           distance;
            List <string> closestNeighbors = BKTreeEngine.LeastEditDistanceWithDistance(stringToTest, data.StreetNameBKTree, out distance);

            if (distance <= maxEditDistance)
            {
                return(CorrectAddress(data, ref address, closestNeighbors[0]));
            }
            else
            {
                return(null);
            }
        }
Ejemplo n.º 2
0
        private static Address LucasAddressMatch(string line, Data data)
        {
            string matched = line;

            Address address = AddressUtility.InitializeAddress(line, data);

            bool matchFound = (address.MatchQuality != MatchQuality.MatchNotYetDetermined);

            if (matchFound)
            {
                level1Match.Add(address.FullStreetName);
            }

            if (!matchFound)
            {
                matched    = TestAddress(data, ref address, address.FullStreetName, 0);
                matchFound = (matched != null);
                if (matchFound)
                {
                    level2Match.Add(address.FullStreetName);
                }
            }

            if (!matchFound)
            {
                matched    = TestAddress(data, ref address, address.FullStreetName + " ST", 0);
                matchFound = (matched != null);
                if (matchFound)
                {
                    level3Match.Add(address.FullStreetName);
                }
            }
            if (!matchFound)
            {
                matched    = TestAddress(data, ref address, address.FullStreetName + " AVE", 0);
                matchFound = (matched != null);
                if (matchFound)
                {
                    level3Match.Add(address.FullStreetName);
                }
            }
            if (!matchFound)
            {
                matched    = TestAddress(data, ref address, address.FullStreetName + " BLVD", 0);
                matchFound = (matched != null);
                if (matchFound)
                {
                    level3Match.Add(address.FullStreetName);
                }
            }

            if (!matchFound)
            {
                List <string> closestNeighbors;
                int           distance;
                // Search by ZIP
                if (address.Zip != null && !string.IsNullOrEmpty(address.StreetName) && !string.IsNullOrEmpty(address.StreetNumber))
                {
                    StateOfNewYorkAddressRange[] streetsWithZip = data.NYCityStreets.Where(n => n.StreetNumber.IsInRange(address.StreetNumber) && n.ZipCode == address.Zip.Value).ToArray();

                    List <string> streetsWithZipStrings = streetsWithZip.Select(s => s.FullStreetName).Distinct().ToList();
                    BKTree        bkTreeLocal           = BKTreeEngine.CreateBKTree(streetsWithZipStrings);

                    closestNeighbors = BKTreeEngine.LeastEditDistanceWithDistance(address.FullStreetName, bkTreeLocal, out distance);

                    if (closestNeighbors.Count == 1 && distance <= 1)
                    {
                        matched    = CorrectAddress(data, ref address, closestNeighbors[0]);
                        matchFound = true;
                        level5Match.Add(address.FullStreetName);
                    }
                    else if (closestNeighbors.Count == 1 && distance <= 2)
                    {
                        matched    = CorrectAddress(data, ref address, closestNeighbors[0]);
                        matchFound = true;
                        level6Match.Add(address.FullStreetName);
                    }
                }
            }

            if (!matchFound)
            {
                matched    = TestAddress(data, ref address, address.FullStreetName, 2);
                matchFound = (matched != null);
                if (matchFound)
                {
                    level7Match.Add(address.FullStreetName);
                }
            }
            if (!matchFound)
            {
                matched    = TestAddress(data, ref address, address.FullStreetName + " ST", 2);
                matchFound = (matched != null);
                if (matchFound)
                {
                    level8Match.Add(address.FullStreetName);
                }
            }
            if (!matchFound)
            {
                matched    = TestAddress(data, ref address, address.FullStreetName + " AVE", 2);
                matchFound = (matched != null);
                if (matchFound)
                {
                    level8Match.Add(address.FullStreetName);
                }
            }
            if (!matchFound)
            {
                matched    = TestAddress(data, ref address, address.FullStreetName + " BLVD", 2);
                matchFound = (matched != null);
                if (matchFound)
                {
                    level8Match.Add(address.FullStreetName);
                }
            }

            // Debug
            if (!matchFound)
            {
                if (false)
                {
                    string addressRaw     = $"{address.RawAddress1} / {address.RawAddress2}";
                    string addressCleaned = $"{ address.StreetNumber } / { address.StreetName} / { address.Suffix}";
                    if (!string.IsNullOrEmpty(address.ApartmentNumber))
                    {
                        addressCleaned += $" / {address.ApartmentNumber}";
                    }
                    string closestNeighborsConcatenated = string.Join(" OR ", BKTreeEngine.LeastEditDistance(address.FullStreetName, data.StreetNameBKTree));

                    Console.WriteLine($"{addressRaw} => {addressCleaned} => {closestNeighborsConcatenated}");
                }
            }


            if (address.MatchQuality == MatchQuality.Unknown)
            {
                lock (unknown)
                {
                    unknown.Add(AddressUtility.CreateLineFromAddress(address, "UNKNOWN"));
                }
            }
            else if (address.MatchQuality == MatchQuality.Homeless)
            {
                lock (homeless)
                {
                    homeless.Add(AddressUtility.CreateLineFromAddress(address, "HOMELESS"));
                }
            }
            else if (address.MatchQuality == MatchQuality.Alternate)
            {
                lock (alternate)
                {
                    alternate.Add(address.RawAddress1);
                }
            }
            else if (address.MatchQuality == MatchQuality.LeaveAlone)
            {
                lock (leaveAlone)
                {
                    leaveAlone.Add(address.RawAddress1);
                }
            }
            else if (address.MatchQuality == MatchQuality.MatchNotYetDetermined)
            {
                lock (matchNotYetDetermined)
                {
                    matchNotYetDetermined.Add($"{address.RawAddress1}=>{address.FullStreetName}");
                }
            }
            return(address);
        }