コード例 #1
0
        public List<AddressModel> Cleansing(string id)
        {
            var resT = new StoreProcedure();
            id = id.Replace("\r", string.Empty).Replace("\n", string.Empty).ToUpper();
            string[] res = new string[] { };
            string[] addressSearch = new string[] { };
            var postCode = string.Empty;
            var temporaryPostCode = string.Empty;
            var houseNumber = string.Empty;
            string street = string.Empty;
            string post = string.Empty;
            string numb = string.Empty;
            string flat = string.Empty;

            if (id.Contains(',') == true)
            {
                res = id.Split(',');
                addressSearch = id.Split(',');

                for (int i = 0; i < addressSearch.Count(); i++)
                {
                    res[i] = res[i].TrimStart().TrimEnd();
                    addressSearch[i] = addressSearch[i].TrimStart().TrimEnd();
                    if (res[i].ToUpper().Contains("FLAT"))
                    {
                        flat = res[i];
                    }
                }
            }
            else if (id.Contains(';') == true)
            {
                res = id.Split(';');
                addressSearch = id.Split(';');

                for (int i = 0; i < addressSearch.Count(); i++)
                {
                    res[i] = res[i].TrimStart().TrimEnd();
                    addressSearch[i] = addressSearch[i].TrimStart().TrimEnd();
                    if (res[i].ToUpper().Contains("FLAT"))
                    {
                        flat = res[i];
                    }
                }
            }
            else
            {
                res = id.Split(' ');
                addressSearch = id.Split(' ');
                //Слагам тука
            }
            for (int i = 0; i < addressSearch.Count(); i++)
            {
                try
                {
                    if (res[i].ToUpper().Contains("FLAT") && flat == "")
                    {
                        flat = res[i] + " " + res[i + 1];
                    }

                    if (res[i].Length > 2)
                    {
                        if (PostCodeType.postCodeType.Contains(res[i].Substring(0, 3)))
                        {
                            post = res[i];
                            if (res[i].Length < 5 && i <= addressSearch.Length - 1)
                            {
                                if (res[i + 1].Length == 3)
                                {
                                    string firstCharNext = res[i + 1].Substring(0, 1);
                                    int n;
                                    bool isNumeric = int.TryParse(firstCharNext, out n);
                                    string secondChar = res[i + 1].Substring(1, 1);
                                    int t;
                                    bool isChar = int.TryParse(secondChar, out t);
                                    if (isNumeric == true && isChar == false)
                                    {
                                        post = res[i] + " " + res[i + 1];
                                    }
                                }
                            }
                            else if (res[i].Length > 4)
                            {
                                var checkChar = res[i].Substring(res[i].Length - 3, 1);
                                if (checkChar != " ")
                                {
                                    res[i] = res[i].Insert(res[i].Length - 3, " ");
                                    Match postCo = Regex.Match(res[i], patern);
                                    if (postCo.Success)
                                        post = res[i];
                                }
                            }
                        }
                        else
                        {
                            int n;
                            bool isNumeric = int.TryParse(res[i], out n);
                            if (isNumeric)
                            {
                                numb = res[i];
                            }
                        }
                    }
                    else
                    {
                        int n;
                        bool isNumeric = int.TryParse(res[i], out n);
                        if (isNumeric)
                        {
                            numb = res[i];
                        }
                    }
                }
                catch
                {

                }
            }

            if (post.Length == 0)
            {
                for (int i = 0; i < addressSearch.Length - 1; i++)
                {
                    string tempPostCode = addressSearch[i] + addressSearch[i + 1];
                    if (tempPostCode.Length > 5)
                    {
                        tempPostCode = tempPostCode.Insert(tempPostCode.Length - 3, " ");
                        Match regPostCode = Regex.Match(tempPostCode, patern);
                        if (regPostCode.Success && PostCodeType.postCodeType.Contains(tempPostCode.Substring(0, 3)))
                        {
                            post = tempPostCode;
                            break;
                        }
                    }
                }

                for (int i = 0; i < addressSearch.Length; i++)
                {
                    string ifPostCodeOneWord = addressSearch[i].Replace(" ", "");
                    if (ifPostCodeOneWord.Length > 5)
                    {
                        ifPostCodeOneWord = ifPostCodeOneWord.Insert(ifPostCodeOneWord.Length - 3, " ");
                        Match regpostCode = Regex.Match(ifPostCodeOneWord, patern);
                        if (regpostCode.Success && PostCodeType.postCodeType.Contains(ifPostCodeOneWord.Insert(ifPostCodeOneWord.Length - 3, " ").Substring(0, 3)))
                        {
                            post = ifPostCodeOneWord;
                            break;
                        }
                    }
                }
            }

            if (post.Length >= 4 && numb.Length > 0)
            {

                var searchStreet = FoundStreet(addressSearch, post);

                if (searchStreet.Count() >= 1)
                {
                    foreach (var item in searchStreet)
                    {
                        if (item.postCode == "" || item.postCode == null)
                        {
                            street = item.street;
                            houseNumber = item.houseNumber;
                            postCode = post;

                            if (PostCodeType.postCodeType.Contains(postCode.Replace(" ", "").Insert(postCode.Length - 3, " ").Substring(0, 3)))
                                goto Address;
                        }
                        else
                        {
                            if (PostCodeType.postCodeType.Contains(item.postCode.Replace(" ", "").Insert(item.postCode.Length - 3, " ").Substring(0, 3)))
                                post = item.postCode;
                            numb = item.houseNumber;
                        }
                    }
                }

                if (flat.Length > 0)
                {

                    var resultPlusFlat = resT.FullDetailsPlusFlat(post, numb, flat);

                    if (resultPlusFlat.Count > 0)
                    {
                        return resultPlusFlat;
                    }
                }

                if (post.Replace(" ", "").Length >= 5)
                {
                    var result = resT.FullDetailsPlusStreet(post, numb);

                    return result;
                }
            }

            Match postCodeOnly = Regex.Match(post, patern);
            if (postCodeOnly.Success && PostCodeType.postCodeType.Contains(post.Substring(0, 3)))
            {
                var result = resT.FullDetails(post);

                return result;
            }
            // Махам от тука
            // }

            for (int i = 0; i < res.Count(); i++)
            {
                //Премахваме всякакви шантави знаци
                res[i] = Regex.Replace(res[i], "[^0-9A-Za-z]+", "");
                if (res[i].Count() == 6 || res[i].Count() == 7)
                {   //Слагам разделителя
                    res[i] = res[i].Insert(res[i].Count() - 3, " ");

                    //REGEX да проверя кое е пощенският код
                    Match match = Regex.Match(res[i], patern);
                    if (match.Success)
                    {   //втора проверка дали първият знак от втората част е цифра
                        var cha = res[i].Substring(res[i].Count() - 3, 1);
                        int n;
                        bool isNumeric = int.TryParse(cha, out n);
                        if (isNumeric)
                        {
                            postCode = res[i];
                        }
                    }
                }
            }

            //Проверяваме ако няма намерен пощенски код
            if (postCode.Count() == 0)
            {
                for (int i = 0; i < res.Count(); i++)
                {
                    try
                    {
                        if (PostCodeType.postCodeType.Contains(res[i].Substring(0, 3)))
                        {
                            res[i] = res[i].Replace(" ", "");

                            for (int j = 1; j < res[i].Count(); j++)
                            {
                                string cha = res[i].Substring(res[i].Count() - j, 1);
                                int n;
                                bool isNumeric = int.TryParse(cha, out n);
                                if (isNumeric)
                                {
                                    res[i] = res[i].Insert(res[i].Length - j, " ");
                                    break;
                                }

                            }
                            postCode = res[i];
                            break;
                        }
                    }
                    catch
                    {

                    }
                }
            }

            var foundStreet = FoundStreet(addressSearch, post);

            if (foundStreet.Count() == 0 && postCode != "" && postCode != null)
            {
                var streetNumber = StreetNumber(addressSearch, postCode);

                if (streetNumber.Length > 0)
                {
                    var result = resT.FullDetailsPlusStreet(postCode, streetNumber);

                    return result;

                }
            }

            foreach (var item in foundStreet)
            {
                street = item.street;
                houseNumber = item.houseNumber;
                if (item.postCode != null)
                {
                    postCode = item.postCode;
                    if (street == null)
                    {
                        //this has to be list, because of double house numbers like 42A, 42B etc..

                        var result = resT.FullDetailsPlusStreet(postCode, houseNumber);

                        return result;
                    }
                }
            }

            //Проверявам дали съм намерил пощенски код

            Address:

            //при намерен пощенски код
            if (postCode.Count() > 0 && postCode.Length > 5)
            {
                //Вземаме данните от базата, чрез процедурата GetFullDetails и ги прехвътлям в лист за да мога да ги обработя повеяе от веднъж при нужда
                var data = resT.FullDetailsFirstOrDefault(postCode);

                //ако улицата от базата съвпада с улицата от записа
                if (data.Street == street.ToUpper())
                {
                    var result = new List<AddressModel>();

                    result.Add(new AddressModel
                    {
                        AdministrativeCounty = data.AdministrativeCounty,
                        BuildingName = data.BuildingName,
                        BuildingNumber = houseNumber,
                        City = data.City.Replace("u0027", " "),
                        Flat = data.Flat,
                        Locality = data.Locality,
                        PostCode = data.PostCode,
                        Street = data.Street.Replace("u0027", " "),
                        TraditionalCounty = data.TraditionalCounty,
                        OrganisationName = data.OrganisationName.Replace("u0026", "&"),
                        IsValid = "Corrected"
                    });
                    return result;
                }

                //Ако улицата не съвпада с тази от записа в базата
                var streetFound = new List<string>();

                streetFound.Add(data.Street);

                //пускаме един Левенщаин за да видим дали не е само правописна грешка
                var foundWords = FuzzySearch.Search(street, streetFound, 0.8);

                //Ако е правописна грешка
                if (foundWords.Count > 0)
                {
                    var result = resT.FullDetailsPlusStreet(post, numb);

                    return result;

                }
                //Ако не е правописна грешка
                else
                {
                    var fuzzy = new FuzzySearch();

                    var resultPlusOne = fuzzy.MinusLetter(postCode + " ", houseNumber, street);

                    if (resultPlusOne.Count > 0)
                        return resultPlusOne;

                    //If there is white space in street
                    var resultNoWhiteSpace = fuzzy.MinusLetter(postCode + " ", houseNumber, street.Replace(" ", ""));

                    if (resultNoWhiteSpace.Count > 0)
                        return resultNoWhiteSpace;

                    //Вземаме всички улици за пощенските кодове без последната буква
                    var resultMinusOne = fuzzy.MinusLetter(postCode, houseNumber, street);

                    if (resultMinusOne.Count > 0)
                        return resultMinusOne;

                    //Ако няма съвпадение, вземаме всички улици без последните 2 букви
                    string postCodeMinusOne = postCode.Substring(0, postCode.Length - 1);

                    var resultMinusTwo = fuzzy.MinusLetter(postCodeMinusOne, houseNumber, street);

                    if (resultMinusTwo.Count > 0)
                        return resultMinusTwo;

                    //Само първата част на ПК
                    string postCodeMinusTwo = postCodeMinusOne.Substring(0, postCodeMinusOne.Length - 1);

                    var resultMinusThree = fuzzy.MinusLetter(postCodeMinusTwo, houseNumber, street);

                    if (resultMinusThree.Count > 0)
                        return resultMinusThree;

                    if (post.Length > 0 && numb.Length > 0)
                    {
                        var houseNumberPostCode = resT.FullDetailsPlusStreet(post, numb);

                        if (houseNumberPostCode.Count > 0)
                            return houseNumberPostCode;
                    }
                }
            }

            if (post.Length > 0 && street.Length > 0)
            {

                var result = resT.FullDetailsPartialStreet(post, street);

                return result;

            }

            var resultT = new List<AddressModel>();
            resultT.Add(new AddressModel()
            {
                Flat = id,
                PostCode = "No match",
                IsValid = "No match found"
            });
            return resultT;
        }