How to normalize city name from many similar names

瘦欲@ 提交于 2019-12-24 09:41:10

问题


I'm getting city name from a 3rd party API. That API doesn't return normalized city name. For example, sometimes its San Francisco, sometime its San Francisco, CA, sometimes its San Francisco, USA. I dont need street address etc.. I just need to normalize the city name to be something uniform. I'm looking for a service that could help me with this requirement.

P.S: Its not mobile app, its web app and the location doesn't come from the browser.


回答1:


You can use the Google Maps API to get normalized address:

1) Get result from: https://maps.googleapis.com/maps/api/geocode/json?address=san_francisco

{
   "results" : [
      {
         "address_components" : [
            {
               "long_name" : "San Francisco",
               "short_name" : "SF",
               "types" : [ "locality", "political" ]
            },
            {
               "long_name" : "Condado San Francisco",
               "short_name" : "Condado San Francisco",
               "types" : [ "administrative_area_level_2", "political" ]
            },
            {
               "long_name" : "California",
               "short_name" : "CA",
               "types" : [ "administrative_area_level_1", "political" ]
            },
            {
               "long_name" : "Estados Unidos",
               "short_name" : "US",
               "types" : [ "country", "political" ]
            }
         ],
         "formatted_address" : "San Francisco, California, EE. UU.",
         "geometry" : {
            "bounds" : {
               "northeast" : {
                  "lat" : 37.9298239,
                  "lng" : -122.28178
               },
               "southwest" : {
                  "lat" : 37.6398299,
                  "lng" : -123.173825
               }
            },
            "location" : {
               "lat" : 37.7749295,
               "lng" : -122.4194155
            },
            "location_type" : "APPROXIMATE",
            "viewport" : {
               "northeast" : {
                  "lat" : 37.812,
                  "lng" : -122.3482
               },
               "southwest" : {
                  "lat" : 37.70339999999999,
                  "lng" : -122.527
               }
            }
         },
         "place_id" : "ChIJIQBpAG2ahYAR_6128GcTUEo",
         "types" : [ "locality", "political" ]
      }
   ],
   "status" : "OK"
}

2) Extract the city name from $data['results'][0]['address_components'][0]['long_name'].

You should check that the returned content contains the described fields and the $data['status'] is "OK".

Source: https://developers.google.com/maps/documentation/geocoding/intro




回答2:


If it's consistent with the city names, you could scan the string and check if there's a comma and if there is ignore anything after



来源:https://stackoverflow.com/questions/39583815/how-to-normalize-city-name-from-many-similar-names

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!