A section of code for address cleaning

Source: Internet
Author: User

The function is to not standardize the address, for example: Beijing Haidian District West two flag xx hospital xx number; Shijiazhuang Qiaoxi District xx road xx; cleaning is the structured address data for the canonical provinces, cities, and counties + Street floor gate formats.

First on the code:

            using (var context = new entities ()) {//Use the first 2 characters to match the provincial Division var Provs                = Context.region.Where (r = R.grade = = 0); var prov = Provs. Singleordefault (P = p.alias.contains (address.                Substring (0, 2));                Region City; if (prov = = null) {///provincial zoning is unknown, try using the first four characters for city-level administrative divisions reverse Query cities = (from c i N Context.region.Where (r = R.grade = = 1 && address. Substring (0, 4). Contains (R.alias)) Let index = address.                            IndexOf (C.alias, stringcomparison.ordinalignorecase) where index >= 0 by index select c).                    FirstOrDefault (); if (city = null) return result.                    Badrequest (); Prov = Context.region.Single (r = r.id = = City.                ParentID); } else {//in a known provinceLevel zoning in matching municipal administrative divisions city = (from C in Context.region.Where (r = R.parentid = = prov.id) Let index = Address.                            IndexOf (C.alias, stringcomparison.ordinalignorecase) where index >= 0 by index select c).                FirstOrDefault ();                } Region County; if (city = null) {///municipal zoning is unknown, try to use county-level administrative divisions in the known provincial zoning to reverse query var citys = Co                    Ntext.region.Where (r = R.parentid = = prov.id); County = (from C in context.region.Where (r = = Citys. Any (c = c.id = = R.parentid))-Let index = address.                              IndexOf (C.alias, stringcomparison.ordinalignorecase) where index >= 0 by index select c).                    FirstOrDefault (); if (county = = null) return result. BadrequesT (); City = Context.region.Single (r = r.id = = County.                ParentID); } else {//in a known city-level zoning to match the county Division County = (from C in cont Ext.region.Where (r = R.parentid = = city.id) Let index = address.                              IndexOf (C.alias, stringcomparison.ordinalignorecase) where index >= 0 by index select c).                FirstOrDefault (); }//Remove the provincial and county level three administrative divisions string var idx = address. IndexOf (county = = null? city. Alias:county.                Alias, StringComparison.OrdinalIgnoreCase); var street = address. Substring (IDX). Replace (county = = null? city. Name:county.                Name, ""); var region = new Address {prov = prov. Name, city = city. Name, county = County?. Name, Street= Street}; return result.            Success (region); }

The principle is very simple, is to use short-name lookup matching, the final output standard names.

Insufficient place is: if the final County administrative divisions are not filled by the official name, the output of the street floor door will not go down this level.

such as the original string is Shijiazhuang, XX Road, No. xx, the output format is:

Province: Hebei Province

City: Shijiazhuang City

County: Qiaoxi District

Street: XI xx Road XX No.

I do not know if you have a good way to get rid of the two words of the Qiao.

A section of code for address cleaning

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.