/
address-parser.php
312 lines (295 loc) · 10.5 KB
/
address-parser.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
<?php
//class supports UTF-8 encodings and requires MultiByte String support
if (!defined('ENCODING')) define('ENCODING','UTF-8');
//because of lack of support for UTF-8 in str_word_count function additional one is defined here
define("WORD_COUNT_MASK", "/\p{L}[\p{L}\p{Mn}\p{Pd}'\x{2019}]*/u");
function str_word_count_utf8($string, $format = 0) {
switch ($format) {
case 1:
preg_match_all(WORD_COUNT_MASK, $string, $matches);
return $matches[0];
case 2:
preg_match_all(WORD_COUNT_MASK, $string, $matches, PREG_OFFSET_CAPTURE);
$result = array();
foreach ($matches[0] as $match) {
$result[$match[1]] = $match[0];
}
return $result;
}
return preg_match_all(WORD_COUNT_MASK, $string, $matches);
}
/**
* AddressParser class
* provides params and methods for parsing textual address representation into structure of
* STREET, CITY and POSTAL_CODE
*/
class AddressParser {
public static $postal_code_masks = array( //order is important - higher priority - first
'/(\d\d-\d\d\d)/', //according to Polish standard only
'/(\d{5,11})/',//typical postal codes (5 to 11 digits)
//'/([A-Z]{1,3}\d{5,11})/',//alphanumerical postal codes (1-3 Capital Letters directly followed by 5-9 digits)
);
private static $important_separator_chars = "|\n\r\t\0\x0B,:;";
private static $trim_additional_chars = " '\"";
private static $trim_chars = null;
private static $important_separator_array = null;
//function which have to be called after class definition
public static function init() {
self::$trim_chars = self::$important_separator_chars.self::$trim_additional_chars;
self::$important_separator_array = str_split(self::$important_separator_chars);
}
static function setImportantSeparatorChars($value) {
self::$important_separator_chars = $value;
self::$trim_chars = self::$important_separator_chars.self::$trim_additional_chars;
self::$important_separator_array = str_split(self::$important_separator_chars);
}
static function getImportantSeparatorChars() {
return self::$important_separator_chars;
}
static function setTrimAdditionalChars($value) {
self::$trim_additional_chars = $value;
self::$trim_chars = self::$important_separator_chars.self::$trim_additional_chars;
}
static function getTrimAdditionalChars() {
return self::$trim_additional_chars;
}
public static $street_length = 35;
public static $city_length = 28;
public static $street_prefixes = array( //street prefixes with priorities
//Polish
'ul'=>5,
'ulica'=>6,
'al'=>3,
'aleja'=>6,
'aleje'=>6,
'oś'=>5,
'os'=>5,
'osiedle'=>5,
'pl'=>3,
'plac'=>6,
'rnd'=>3,
'rondo'=>3,
'rynek'=>5,
'bulwar'=>4,
'skwer'=>2,
//International
'rd'=>1,
'road'=>1,
'st'=>1,
'street'=>1,
'ave'=>1,
'avenue'=>1,
'blvd'=>1,
'boulevard'=>1,
'ar'=>1,
'park'=>1,
);
/*
*/
private static function streetcity_split($txt) {
//echo 'streetcity_split('.$txt.") started\n";
$result = array();
$notnice = false;
$maxlength = (self::$street_length>self::$city_length) ? self::$street_length : self::$city_length;
//echo 'DEBUG: maxlength ='.$maxlength."\n";
$maxtoken = substr($txt, 0, $maxlength);
//echo 'DEBUG: maxtoken =['.$maxtoken."]\n";
$last_separator = 0;
//important separators
foreach (self::$important_separator_array as $separator) {
$pos = strrpos($maxtoken, $separator, $last_separator);
if ($pos>$last_separator) $last_separator = $pos;
//echo 'DEBUG: separator = '.$separator."\n pos=".$pos."\nlast_separator =".$last_separator."\n";
}
//numbers as separators
if ($last_separator==0) {
for ($i=0; $i<10; $i++) {
$pos = strrpos($maxtoken, (string)$i, $last_separator);
if ($pos>$last_separator) $last_separator = $pos;
//echo 'DEBUG: separator = '.$i."\n pos=".$pos."\nlast_separator =".$last_separator."\n";
}
if ($last_separator>0) $last_separator++;
}
//roma numbers as separators
if ($last_separator==0) {
$words = str_word_count_utf8(mb_strtolower($maxtoken,ENCODING), 2);
$value = 0;
foreach($words as $key=>$word) {
if (self::is_roma_number($word)) {
$last_separator = $key + strlen($word);
}
}
}
//space as separator
if ($last_separator==0) $last_separator = strrpos($maxtoken, ' ', 0);
//echo 'DEBUG: last_separator (after space) ='.$last_separator."\n";
if ($last_separator>0) {
//echo 'DEBUG: last_separator>0';
$tokens[]=substr($txt, 0, $last_separator);
$tokens[]=substr($txt, $last_separator);
} else {//it is impossible to split in nice way so we need to remember this during crop
$notnice = true;
$tokens[]=$maxtoken;
$tokens[]=substr($txt, -$maxlength);
}
//echo 'DEBUG: notnice = '.($notnice?'true':'false')."\n";
//echo "DEBUG: tokens:\n";
//print_r($tokens);
//echo "\n";
$first_street = self::is_street($tokens[0]);
$second_street = self::is_street($tokens[1]);
if ($first_street<0) {
$tokens[0] = substr($txt,0,-$first_street);
$tokens[1] = substr($txt,-$first_street);
$second_street = 0;
//echo "DEBUG: tokens:\n";
//print_r($tokens);
//echo "\n";
}
if ($first_street >= $second_street) {
$result['STREET'] = $tokens[0];
$result['CITY'] = $tokens[1];
$result['CROPPED_STREET'] = self::crop_street($result['STREET']);
$result['CROPPED_CITY'] = self::crop_city($result['CITY'], $notnice);
} else {
$result['CITY'] = $tokens[0];
$result['STREET'] = $tokens[1];
$result['CROPPED_CITY'] = self::crop_city($result['CITY']);
$result['CROPPED_STREET'] = self::crop_street($result['STREET'],$notnice);
}
return $result;
}
private static $roma_chars = array('M','D','C','L','X','V','I','m','d','c','l','x','v','i');
/**
* Utility function to verify if string is valid roma number
* @param txt - string to check
* @result boolean
*/
public static function is_roma_number($txt) {
if (strlen($txt)>6) return false;
for($i = 0; $i<strlen($txt); $i++) {
if (!in_array($txt[$i],self::$roma_chars)) return false;
}
return true;
}
/**
* Utility function to check how much string looks like street part of address
*
* @param txt string to check
* @result number higher value if string is closer to street patterns
*/
private static function is_street($txt) {
$is_street_prefix = false;
if (strcspn($txt,'0123456789')<strlen($txt)) {//if ends with number it is probably street
//echo 'DEBUG: is_street("'.$txt."\") = 1000\n";
return 1000;
}
$txt = mb_strtolower($txt,ENCODING);
//look for street prefix
$words = str_word_count_utf8($txt,2);
//echo "DEBUG: words:\n";print_r($words); echo "\n";
$value = 0;
foreach ($words as $key=>$word) {
@$v = self::$street_prefixes[$word];
if ($v != 0) {
if ($is_street_prefix===false) {
$is_street_prefix = $key;
//echo 'DEBUG: is_street('.$txt.') street_prefix - word:'.$word.' +'.$v."\n";
}
}
if (($v==0) && (self::is_roma_number($word))) $v = 2;
$value += $v;
//if ($v!=0) echo 'DEBUG: is_street('.$txt.') - word:'.$word.' +'.$v."\n";
}
if ($is_street_prefix!==false) {
$after_prefix = trim(substr($txt, $is_street_prefix+strlen($words[$is_street_prefix])),self::$trim_chars.'.');
//echo 'DEBUG: after_prefix: ['.$after_prefix."]\n";
if (empty($after_prefix)) $value = -$is_street_prefix;
}
//echo 'DEBUG: is_street("'.$txt.'") = '.$value."\n";
return $value;
}
/*
* Utility function to crop text according to street pattern length specification
*
* @param txt - string to be cropped
* @keep_end - false (default) if keep left part as much as possible, true to keep right part of string as much as possible
*/
private static function crop_street($txt,$keep_end=false) {
if ($keep_end) {
$txt = rtrim($txt,self::$trim_chars);
$txt = substr($txt,-self::$street_length);
$txt = ltrim($txt,self::$trim_chars);
} else {
$txt = ltrim($txt,self::$trim_chars);
$txt = substr($txt,0,self::$street_length);
$txt = rtrim($txt,self::$trim_chars);
}
return $txt;
}
/*
* Utility function to crop text according to city pattern length specification
*
* @param txt - string to be cropped
* @keep_end - false (default) if keep left part as much as possible, true to keep right part of string as much as possible
*/
private static function crop_city($txt,$keep_end=false) {
if ($keep_end) {
$txt = rtrim($txt,self::$trim_chars);
$txt = substr($txt,-self::$city_length);
$txt = ltrim($txt,self::$trim_chars);
} else {
$txt = ltrim($txt,self::$trim_chars);
$txt = substr($txt,0,self::$city_length);
$txt = rtrim($txt,self::$trim_chars);
}
return $txt;
}
/**
* Parse Address
*
* @param txt - text to parse
* @result array containing structured address if parsing was successfull togather with ORIGINAL value or partial structure in case of problems
*/
public static function parseAddress($txt) {
$result = array();
$result['ORIGINAL'] = $txt;
//split by postal code
foreach (self::$postal_code_masks as $postal_code_mask) {
$tmp = preg_split($postal_code_mask, $txt, 3, PREG_SPLIT_DELIM_CAPTURE);
if (count($tmp)>=3) break;
}
$result['TMP'] = $tmp;
if (count($tmp)<3) { //postal code not found
$result['POSTAL_CODE'] = '';
//trim source text
$txt = trim($txt,self::$trim_chars);
//split $txt into street and city part
$result = array_merge($result,self::streetcity_split($txt));
} else {//postal code found
$result['POSTAL_CODE']=$tmp[1];//if found, allways in the middle
$before = trim($tmp[0],self::$trim_chars);
$after = trim($tmp[2],self::$trim_chars);
if (empty($before)) {//postal code at the beginning
//split $txt into street and city part
$result = array_merge($result,self::streetcity_split($after));
} else if (empty($after)) {//postal code at the end
//split $txt into street and city part
$result = array_merge($result,self::streetcity_split($before));
} else {//postal code in the middle
if (self::is_street($before)>=self::is_street($after)) {//check if there is number before
$result['STREET'] = $before;
$result['CITY'] = $after;
} else {//check if there is number after
$result['CITY'] = $before;
$result['STREET'] = $after;
}
$result['CROPPED_STREET'] = self::crop_street($result['STREET']);
$result['CROPPED_CITY'] = self::crop_city($result['CITY']);
}
}
return $result;
}
}
AddressParser::init();
?>