Author Topic: Address Parsing  (Read 2269 times)

worker201

  • Global Moderator
  • Member
  • ***
  • Posts: 2,810
  • Kudos: 703
    • http://www.triple-bypass.net
Address Parsing
« on: 5 May 2010, 22:49 »
I've been trying to write an address parsing routine in PHP.  The user is prompted to input a street address.  In my area, the address is of the form:
number prefix street type (example: 101 N 37th Ave)
or
number street type suffix (example: 101 37th St NW)

Of course, the user might simply put in 101 14th, but that's okay, I think I've covered that.  What I'm not sure about is how to deal with multi-word streetnames.  What if the address is 101 SW Happy Morning Bluebird Dr?

Here's the code I've got so far, which works great for single-word streetnames, and even deals with incomplete addresses.  Am I going about this in the wrong way?  Got any ideas how I can improve my algorithm?

The address parts will eventually be used to search a street database.

Code: [Select]
<?php
$address 
$_POST['address'];
$parts_array sscanf($address"%d %s %s %s");
if (
$parts_array[0] > 0) {
$number $parts_array[0];
//echo "Number is $number <br>";
}
else {
echo "This does not seem to be a valid address";
exit;
}
$fixes = array("NW""NE""SW""SE""N""S""E""W");
if (
in_array($parts_array[1], $fixes)) {
$prefix $parts_array[1];
//echo "Prefix is $prefix <br>";
$name strtoupper($parts_array[2]);
//echo "Name is $name <br>";
if ($parts_array[3] != NULL) {
$type strtoupper($parts_array[3]);
//echo "Type is $type <br>";
}
}
elseif (
in_array($parts_array[3], $fixes)) {
$name strtoupper($parts_array[1]);
//echo "Name is $name <br>";
$type strtoupper($parts_array[2]);
//echo "Type is $type <br>";
$suffix $parts_array[3];
//echo "Suffix is $suffix <br>";
}
else {
$street strtoupper($parts_array[1]);
//echo "Street is $street <br>";
if ($parts_array[2] != NULL) {
$type strtoupper($parts_array[2]);
//echo "Type is $type <br>";
}
}
?>
« Last Edit: 5 May 2010, 23:57 by Refalm »

Calum

  • Global Moderator
  • Member
  • ***
  • Posts: 7,812
  • Kudos: 1000
    • Calum Carlyle's music
Re: Address Parsing
« Reply #1 on: 6 May 2010, 11:57 »
can you make it recognise every string that might be considered a "type" (like with your array for the prefixes and suffixes) and then assume that everything before it (with spaces included) must be the "street"?
visit these websites and make yourself happy forever:
It's my music! | My music on MySpace | Integrational Polytheism

worker201

  • Global Moderator
  • Member
  • ***
  • Posts: 2,810
  • Kudos: 703
    • http://www.triple-bypass.net
Re: Address Parsing
« Reply #2 on: 6 May 2010, 12:05 »
That might work, assuming every address has a type - I'll have to check on that.  I've already made a list of all the types that appear in the database, as part of another aspect of the project, so it wouldn't be too difficult to implement.  Thanks for the suggestion.

worker201

  • Global Moderator
  • Member
  • ***
  • Posts: 2,810
  • Kudos: 703
    • http://www.triple-bypass.net
Re: Address Parsing
« Reply #3 on: 11 May 2010, 13:00 »
Finally got this working.  Tried with a bunch of different test addresses, seems to work.  If you entered "123921 kdjslf skdsls", this would sort it just fine, but the program as a whole would fail, hard.

Code: [Select]
<?php
$address 
$_POST['address'];
$parts_array explode(" "$address);
if (
$parts_array[0] > 0) { // the first piece of the address is a number
$number $parts_array[0];
//echo "Number is $number <br>";
} else { // user has provided an address without a number
echo "This does not seem to be a valid address";
exit;
}
$fixes = array("NW""NE""SW""SE""N""S""E""W");
$types = array("ACRD""ALY""AVE""BLVD""BR""CIR""CRST""CT""DR""FRWY""HWY"
      "KY""LN""LOOP""PKWY""PL""PT""RD""ST""TER""TRL""WALK""WAY");
for (
$i=1$i count($parts_array); $i++) {
  
$parts_array[$i] = strtoupper($parts_array[$i]);
  if (
in_array($parts_array[$i], $fixes)) {
    if (
$i == 1) {
      
$prefix $parts_array[$i];
    } else {
      
$suffix $parts_array[$i];
    }
  } elseif (
in_array($parts_array[$i], $types)) {
    
$type $parts_array[$i];
  } else {
    if (isset(
$name)) {
      
$name $name " " $parts_array[$i];
    } else {
    
$name $parts_array[$i];
    }
  }
}
?>

« Last Edit: 11 May 2010, 13:31 by Refalm »

Calum

  • Global Moderator
  • Member
  • ***
  • Posts: 7,812
  • Kudos: 1000
    • Calum Carlyle's music
Re: Address Parsing
« Reply #4 on: 20 May 2010, 17:41 »
well done. by the way i know nothing about scripting or programming so the fact my suggestion even made sense is a bonus!
visit these websites and make yourself happy forever:
It's my music! | My music on MySpace | Integrational Polytheism

Kintaro

  • Member
  • **
  • Posts: 6,545
  • Kudos: 255
  • I want to get the band back together!
    • JohnTate.org
Re: Address Parsing
« Reply #5 on: 21 May 2010, 06:00 »
well done. by the way i know nothing about scripting or programming so the fact my suggestion even made sense is a bonus!

Can you become a middle manager? Because one of those that also has productive suggestions would make all our lives much easier.  ;D