I have list with several million place names that come from Flickr profiles. Users provided these placenames as free text, so they look like this:

Roma, Italy
Kennesaw, USA
Saginaw, MI
Rucker, Missouri, USA
Melbourne, Australia
Madrid, Spain
live in Sarnia / work in London, Canada
Valladolid, España
West Hollywood, United States

I want to disambiguate these place names. I am aware that there is in some cases no straightforward to this solution, but I am willing to live with some false disambiguation and with "no answer" for some of the places. If a place name corresponds to the name of multiple cities, then I want to assign that place to the largest city that it corresponds to.

Yahoo's place finder api would be a good solution to this problem, but I would need to make too many API calls to get through my list, so I'd like a local solution (i.e., one that does not depend on a remote api). Does anyone know of any python libraries that do this kind of thing, or any other local solutions?

(I've also asked this question on stackoverflow.)


You could try the Python library geodict. This has datasets you can download and import to a database - you can check the lists to see if they'd work well or not with your data. It works in two steps:

  1. Extracting names
  2. Matching names to a location in the lists

More details (and another online option in the comments) here.

| improve this answer | |

I assume your best guess is to use a fuzzy algorithm.

Take your local dictionary of place names and administrative units and compare each word and each comma-separated block of text against this dictionary. Assign a score to each match. You might want to use a normalized search to account for spelling mistakes and have an "ignore list" for words like "live" and "work" and "in". Add the score for administrative units to the score of any smaller unit or place name in your matches that lie within this administrative unit.

Tune the scoring function with your results until you are happy. Take the best scoring match.

e.g.: Roma, Italy 
Roma matches 8 places (score according to size)
Roma matches 23 more places with normalization (lower score according to size)
Italy matches 4 places + 2 administrative units (COUNTRY, DISTRICT) (score acconding to size)
Italy matches 14 more places and units with normalization (lower score according to size)
One of the Romas lies in one of your units. -> combine scores

If you tuning is good, you will have given most points to the capital of Italy.

| improve this answer | |

You can use the geotext python library for the same.

pip install geotext

all it takes is to install this library. The usage is as simple as:

from geotext import GeoText
places = GeoText("London is a great city")

gives the result 'London'

The list of cities covered in this library is not extensive but it has a good list.

| improve this answer | |

A commercial offering is Polygon Analytics' geocoder, which exists as SAAS REST API as well as an on-premise, high-performance C++ API (with wrappers for Python, Java and others) to avoid network latency (or for sensitive data).

Its API also provides lat/lon output for mapping.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.