JBAS-984 KCLS Address normalizer
authorBill Erickson <berickxx@gmail.com>
Wed, 10 Feb 2016 22:00:01 +0000 (17:00 -0500)
committerBill Erickson <berickxx@gmail.com>
Thu, 21 Mar 2019 19:46:23 +0000 (15:46 -0400)
Signed-off-by: Bill Erickson <berickxx@gmail.com>
Open-ILS/src/perlmods/lib/OpenILS/Utils/KCLSNormalize.pm [new file with mode: 0644]

diff --git a/Open-ILS/src/perlmods/lib/OpenILS/Utils/KCLSNormalize.pm b/Open-ILS/src/perlmods/lib/OpenILS/Utils/KCLSNormalize.pm
new file mode 100644 (file)
index 0000000..65834b3
--- /dev/null
@@ -0,0 +1,47 @@
+package OpenILS::Utils::KCLSNormalize;
+
+sub normalize_address_street {
+    my ($street1, $street2) = @_;
+
+    # Replace 'AV' with 'AVE', but only when "AV" is surrounded by space
+    # period, or end of line, so as not to clobber names that contain AV.
+    if (my $s1 = $street1) {
+        $s1 =~ s/\s+AV(\s|\.|$)+/ AVE /g;
+        $s1 =~ s/\s+ST\.(\s|$)+/ ST /g;
+        $s1 =~ s/(^\s*|\s*$)//g; # remove opening/trailing spaces
+        $street1 = $s1;
+    }
+
+    # Our policy is to include the apartment / unit number in the
+    # stree1 value.  If street2 starts with APT or UNIT, append it
+    # onto the end of street1 (and clear street2).
+    # We also replace any occurrence of APT or UNIT with a '#'.
+    if (my $s2 = $street2) {
+        if ($s2 =~ /^(APT|UNIT|#)/) {
+            $s2 =~ s/^(APT\.?|UNIT\.?)//g; # remove APT / UNIT
+            $s2 =~ s/^\s*//g; # trim leading space
+            if ($s2 =~ /^#/) {
+                # if the addr starts with a #, ensure it's followed by a space
+                $s2 =~ s/^#/# /g if $s2 =~ /^#[^\s]/;
+            } else {
+                # if no '#' is present to replace APT/UNIT, add it.
+                $s2 = "# $s2" unless $s2 =~ /^#/;
+            }
+
+            # remove random "," "." "-" and extra spaces that 
+            # occur after the initial "#".
+            $s2 =~ s/^#[\s,\.-]*(.*)$/# $1/g;
+
+            if ($street1) {
+                $street1 .= " $s2";
+            } else {
+                $street1 = $s2;
+            }
+            $street2 = undef;
+        }
+    }
+
+    return ($street1, $street2);
+}
+
+1;