Received: from maia.hub.org (maia-3.hub.org [200.46.204.243]) by mail.postgresql.org (Postfix) with ESMTP id B5089B5DBB6 for ; Mon, 23 May 2011 20:08:01 -0300 (ADT) Received: from mail.postgresql.org ([200.46.204.86]) by maia.hub.org (mx1.hub.org [200.46.204.243]) (amavisd-maia, port 10024) with ESMTP id 80861-02 for ; Mon, 23 May 2011 23:07:54 +0000 (UTC) X-Greylist: from auto-whitelisted by SQLgrey-1.7.6 Received: from zebedee.cranntara.net (zebedee.cranntara.net [91.186.7.79]) by mail.postgresql.org (Postfix) with ESMTP id 31B3DB5D801 for ; Mon, 23 May 2011 20:07:53 -0300 (ADT) Received: from [88.210.188.180] (helo=bolek.coachhouse) by zebedee.cranntara.net with esmtpsa (TLS1.0:RSA_AES_256_CBC_SHA1:32) (Exim 4.72) (envelope-from ) id 1QOeE3-0003j9-G2 for pgsql-sql@postgresql.org; Tue, 24 May 2011 00:07:52 +0100 Received: from dick.coachhouse ([192.168.1.32] ident=tarlika) by bolek.coachhouse with esmtp (Exim 4.69) (envelope-from ) id 1QOdmt-0001oH-63 for pgsql-sql@postgresql.org; Mon, 23 May 2011 23:39:47 +0100 Date: Mon, 23 May 2011 23:39:49 +0100 From: Tarlika Elisabeth Schmitz To: sql pgsql Subject: Re: extracting location info from string Message-ID: <20110523233949.07fb0005@dick.coachhouse> In-Reply-To: References: <20110522210526.0f8b66fa@dick.coachhouse> <20110522230041.28d1bc35@dick.coachhouse> X-Mailer: Claws Mail 3.5.0 (GTK+ 2.20.1; i486-pc-linux-gnu) Mime-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=-0.165 tagged_above=-5 required=5 tests=BAYES_00=-1.9, T_FILL_THIS_FORM_SHORT=0.01, URIBL_BLACK=1.725 X-Spam-Level: X-Archive-Number: 201105/138 X-Sequence-Number: 35422 On Mon, 23 May 2011 13:11:24 +1200 Andrej wrote: >On 23 May 2011 10:00, Tarlika Elisabeth Schmitz > wrote: >> On Sun, 22 May 2011 21:05:26 +0100 >> Tarlika Elisabeth Schmitz wrote: >> >>>A column contains location information, which may contain any of the >>>following: >>> >>>1) null >>>2) country name (e.g. "France") >>>3) city name, region name (e.g. "Bonn, Nordrhein-Westfalen") >>>4) city name, Rg. region name (e.g. "Frankfurt, Rg. Hessen") >>>5) city name, Rg region name (e.g. "Frankfurt, Rg Hessen") >> >> >> I also need to cope with variations of COUNTRY.NAME and REGION.NAME. > >sanitising that data will be tedious, - particularly with the >variations on region. Indeed. However, the situation is not quite as bleak as it appears: - I am only dealing with 50 countries (Luxemburg and Vatican are not amongst them) - Only for two countries will city/region be displayed instead of country. - Ultimately, where the only important bit of imformation is the country. - The only really important persons are those from the two countries. >Another thing of great import is whether the city can occur in the >data column all by itself; if yes, it's next to impossible to >distinguish it from a country. Unfortunately this is the case. >Specially if we assume that >typos/misspelling are feasible on top of punctuation ... and former countries like Czechoslovakia ... >If I had a task like that to perform I'd dump the data out to file >and have a good go at it w/ sed & awk, or perl, depending on >how complex & voluminous the data is. I had a quick look at the data (maybe not the most efficient SQL) SELECT id, name, CASE WHEN location is null then null WHEN location !~ '.*,.*' then (select id from country where name = location) ELSE (select country from county where name = regexp_replace(location, '.*, (Rg\\.? )?(.*)', '\\2')) END AS country, location FROM temp_person Of 17000 historical records, 4400 don't match this simple pattern. Of the 4400, 1300 are "USA" or "Usa" instead of "United States", 900 "North America" whatever that is! There are plenty of common + valid region abbreviations. I get about 1000 new records of this type per year. I presume that more recent data are more accurate. I know I won't be able to clean them all up. However, the import process has to be as automatic as possible in such a way that inconsistencies are flagged up for later manual intervention. I say later because, for instance, a person's data will have to be imported with or without location info because other new data will link to it. -- Best Regards, Tarlika Elisabeth Schmitz