NAME

Lingua::EN::AddressParse - manipulate geographical addresses

SYNOPSIS

use Lingua::EN::AddressParse;

my %args = 
(
	country     => 'Australia',
   auto_clean  => 1,
   force_case  => 1
);

my $address = new Lingua::EN::AddressParse(%args); 

$error = $address->parse("14A MAIN RD. ST JOHNS WOOD NSW 2000");

%my_address = $address->components;
$suburb = $my_address{suuburb};

$correct_casing = $address->case_all;

REQUIRES

Perl, version 5.001 or higher, Lingua::EN::NameParse and Parse::RecDescent

DESCRIPTION

This module takes as input an address or post box in free format text such as,

	12/3-5 AUBREY ST VERMONT VIC 3133
	"OLD REGRET" WENTWORTH FALLS NSW 2782 AUSTRALIA
	2A LOW ST. KEW NSW 2123
   GPO Box K318, HAYMARKET, NSW 2000

and attempts to parse it. If successful, the address is broken down into components and useful functions can be performed such as :

converting upper or lower case values to name case (2 Low St. Kew NSW 2123 )
extracting the addresses individual components     (2,Low St.,KEW,NSW,2123 )
determining the type of format the address is in   ('suburban')

If the address cannot be parsed you have the option of cleaning the address of bad characters, or extracting any portion that was parsed and the portion that failed.

This module can be used for analysing and improving the quality of lists of addresses.

DEFINITIONS

The following terms are used by AddressParse to define the components that can make up an address or post box.

	Post Box - 	GP0 Box K123, LPO 2345, RMS 23 ...
   
   Property Identifier
   	Sub property description  -  Level, Unit, Apartment, Lot ...
	   Property number           - 12/66A, 24-34, 2A, 23B/12C, 12/42-44
      
   Property name - "Old Regret"

   Street
	   Street name   - O'Hare, New South Head, The Causeway
	   Street type   - Road, Rd., St, Lane, Highway, Crescent, Circuit ...
      
   Suburb	     - Dee Why, St. John's Wood ...
   State			  - NSW, ACT, NY, AZ ...
   Post code	  - 2062, 34532, SG12A 9ET 
   Country		  - Australia, UK, Papua New Guinea ....
   

Refer to the component grammar defined within the code for a complete list of combinations.

The following address formats are currently supported :

'suburban' - property_identifier street suburb state post_code country(?) 
'post_box' - post_box suburb_subcountry post_code country(?) 
'rural'    - property_name suburb_subcountry post_code country(?) 

METHODS

new

The new method creates an instance of an address object and sets up the grammar used to parse addresses. This must be called before any of the following methods are invoked. Note that the object only needs to be created once, and can be reused with new input data.

Various setup options may be defined in a hash that is passed as an optional argument to the new method.

my %args = 
(
	country     => 'Australia',
   auto_clean  => 1,
   force_case  => 1
);

my $address = new Lingua::EN::AddressParse(%args); 

country

The country argument must be specified. It determines the possible list of valid subcountries (states, counties etc, defined in the Locale::SubCountry module) and post code formats.The current list of coutries is

Australia
Brazil
Canada
Netherlands
UK
USA

All forms of upper/lower case are acceptable in the country's spelling. If a country name is supplied that the module doesn't recognise, it will die.

force_case

This option will force the case_all method to address case the entire input string, including any unmatched sections that failed parsing. This option is useful when you know you data has invalid addresses, but you cannot filter out or reject them.

auto_clean

When this option is set to a positive value, any call to the parse method that fails will attempt to 'clean' the address and then reparse it. See the clean method for details. This is useful for dirty data with embedded unprintable or non alphabetic characters.

parse

$error = $address->parse("12/3-5 AUBREY ST VERMONT VIC 3133");

The parse method takes a single parameter of a text string containing a address. It attempts to parse the address and break it down into the components described above. If the address was parsed successfully, a 0 is returned, otherwise a 1. This step is a prerequisite for the following functions.

case_all

$correct_casing = $address->case_all;

The case_all method converts the first letter of each component to capitals and the remainder to lower case, with the following exceptions-

Proper names capitalisation such  as MacNay and O'Brien are observed

The method returns the entire cased address as text.

case_components

%my_address = $address->components;
$cased_suburb = $my_address{suburb};

The case_components method does the same thing as the case_all method, but returns the addresses cased components in a hash. The following keys are used for each component-

	post_box
	property_identifier
   property_name
   street
   suburb
   post_code
   country

Entries only occur in the hash for each component that the currently parsed address contains, meaning there are no keys with undefined values.

components

%address = $address->components;
$surburb = $address{suburb};

The components method does the same thing as the case_components method, but each component is returned as it appears in the input string, with no case conversion.

properties

The properties method return several properties of the address as a hash.

type

The type of format a name is in, as one of the following strings:

suburban
rural
post_box
unknown

non_matching

Returns any unmatched section that was found.

LIMITATIONS

The huge number of character combinations that can form a valid address makes it is impossible to correctly identify them all.

Valid addresses must contain a suburb, subcountry (state) and post code, in that order. This format is widely accepted in Australia and the US. UK addresses will often include suburb, town, city and county, formats that are very difficult to parse.

Subcountries must be in abbreviated form such as NSW, VIC, NY, CA, LEICS. I could add a pre-parse step to detect and abbreivate subcountry names, so data sych as "New South Wales" can be parsed.

Property names must be enclosed in quotes like "Old Regret"

Because of the large combination of possible addresses defined in the grammar, the program is not very fast.

REFERENCES

"The Wordsworth Dictionary of Abbreviations & Acronyms" (1997)

Australian Standard AS4212-1994 "Geographic Information Systems - Data Dictionary for transfer of street addressing information"

FUTURE DIRECTIONS

Define grammar for other languages. Hopefully, all that would be needed is to specify a new module with its own grammar, and inherit all the existing methods. I don't have the knowledge of the naming conventions for non-english languages.

SEE ALSO

Locale::Subcountry Lingua::EN::NameParse Parse::RecDescent

TO DO

BUGS

CHANGES

0.01 28 Dec 1999: First Release

COPYRIGHT

Copyright (c) 2000 Kim Ryan. All rights reserved. This program is free software; you can redistribute it and/or modify it under the terms of the Perl Artistic License (see http://www.perl.com/perl/misc/Artistic.html).

AUTHOR

AddressParse was written by Kim Ryan <kimaryan@ozemail.com.au> in 2000.