-
Notifications
You must be signed in to change notification settings - Fork 0
/
ParseAddress.cs
413 lines (375 loc) · 19.2 KB
/
ParseAddress.cs
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
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
// <copyright file="ParseAddress.cs" company="Air Osprey">
// MIT License (MIT). All rights reserved
// </copyright>
// <author>Larry Conklin</author>
// <summary>US Address Parsing class.</summary>
namespace USGeoAddressParser
{
using System;
using System.Globalization;
using System.Text;
using System.Text.RegularExpressions;
/// <summary>
/// Parse US Address class.
/// </summary>
public class ParseAddress
{
private static string streetTypesA = "ALLEE|ALLEY|ALLY|ALY|ANEX|ANNEX|ANNX|ANX|ARC|ARCADE|AV|AVE|AVEN|AVENU|AVENUE|AVN|AVNUE|";
private static string streetTypesB = "BAYOO|BAYOU|BCH|BEACH|BEND|BND|BLF|BLUF|BLUFF|BLUFFS|BOT|BOTTM|BOTTOM|BTM|BLVD|BOUL|BOULEVARD|BOULV|BR|BRANCH|BRNCH|BRDGE|BRG|BRIDGE|BRK|BROOK|BROOKS|BURG|BURGS|BYP|BYPABYPAS|BYPASS|BYPS|";
private static string streetTypesC = "CAMP|CMP|CP|CANYN|CANYON|CNYN|CYN|CAPE|CPE|CAUSEWAY|CAUSWAY|CSWY|CEN|CENT|CENTER|CENTR|CENTRE|CNTER|CNTR|CTR|CENTERS|CIR|CIRC|CIRCL|CIRCLE|CRCL|CRCLE|CIRCLES|CLF|CLIFF|CLFS|CLIFFS|CLB|CLUB|COMMON|COR|CORNER|CORNERS|CORS|COURSE|CRSE|COURT|CRT|CT|COURTS|CT|COVE|CV|COVES|CK|CR|CREEK|CRK|CRECENT|CRES|CRESCENT|CRESENT|CRSCNT|CRSENT|CRSNT|CREST|CROSSING|CRSSING|CRSSNG|XING|CROSSROAD|CURVE|";
private static string streetTypesD = "DALE|DL|DAM|DM|DIV|DIVIDE|DV|DVD|DR|DRIV|DRIVE|DRV|DRIVES|";
private static string streetTypesE = "EST|ESTATE|ESTATES|ESTS|EXP|EXPR|EXPRESS|EXPRESSWAY|EXPW|EXPY|EXT|EXTENSION|EXTN|EXTNSN|EXTENSIONS|EXTS|";
private static string streetTypesF = "FALL|FALLS|FLS|FERRY|FRRY|FRY|FIELD|FLD|FIELDS|FLDS|FLAT|FLT|FLATS|FLTS|FORD|FRD|FORDS|FOREST|FORESTS|FRST|FORG|FORGE|FRG|FORGES|FORK|FRK|FORKS|FRKS|FORT|FRT|FT|FREEWAY|FREEWY|FRWAY|FRWY|FWY|";
private static string streetTypesG = "GARDEN|GARDN|GDN|GRDEN|GRDN|GARDENS|GDNS|GRDNS|GATEWAY|GATEWY|GATWAY|GTWAY|GTWY|GLEN|GLN|GLENS|GREEN|GRN|GREENS|GROV|GROVE|GRV|GROVES|";
private static string streetTypesH = "HARB|HARBOR|HARBR|HBR|HRBOR|HARBORS|HAVENHAVNHVNHEIGHT|HEIGHTS|HGTS|HT|HTS|HIGHWAY|HIGHWY|HIWAY|HIWY|HWAY|HWY|HILL|HL|HILLS|HLS|HLLW|HOLLOW|HOLLOWS|HOLW|HOLWS|";
private static string streetTypesI = "INLET|INLT|IS|ISLAND|ISLND|ISLANDS|ISLNDS|ISS|ISLE|ISLES|";
private static string streetTypesJ = "JCT|JCTION|JCTN|JUNCTION|JUNCTN|JUNCTON|JCTNS|JCTS|JUNCTIONS|";
private static string streetTypesK = "KEY|KY|KEYS|KYS|KNL|KNOL|KNOLL|KNLS|KNOLLS|";
private static string streetTypesL = "LAKE|LK|LAKES|LKS|LAND|LANDING|LNDG|LNDNG|LA|LANE|LANES|LNLGT|LIGHT|LIGHTS|LF|LOAF|LCK|LOCK|LCKS|LOCKS|LDG|LDGE|LODG|LODGE|LOOP|LOOPS|";
private static string streetTypesM = "MALL|MANOR|MNR|MANORS|MNRS|MDW|MEADOW|MDWS|MEADOWS|MEDOWS|MEWS|MILL|ML|MILLS|MLS|MISSION|MISSN|MSN|MSSN|MOTORWAY|MNT|MOUNT|MT|MNTAIN|MNTN|MOUNTAIN|MOUNTIN|MTIN|MTN|MNTNS|MOUNTAINS|";
private static string streetTypesN = "NCK|NECK";
private static string streetTypesO = "ORCH|ORCHARD|ORCHRD|OVAL|OVL|OVERPASS|";
private static string streetTypesP = "PARK|PK|PRK|PARKS|PARKWAY|PARKWY|PKWAY|PKWY|PKY|PARKWAYS|PKWYS|PASS|PASSAGE|PATH|PATHS|PIKE|PIKES|PINE|PINES|PNES|PL|PLACE|PLAIN|PLN|PLAINES|PLAINS|PLNS|PLAZA|PLZ|PLZA|POINT|PT|POINTS|PTS|PORT|PRT|PORTS|PRTS|PR|PRAIRIE|PRARIE|PRR|";
private static string streetTypesR = "RAD|RADIAL|RADIEL|RADL|RAMP|RANCH|RANCHES|RNCH|RNCHS|RAPID|RPD|RAPIDS|RPDS|REST|RST|RDG|RDGE|RIDGE|RDGS|RIDGES|RIV|RIVER|RIVR|RVR|RD|ROAD|RDS|ROADS|ROUTE|ROW|RUE|RUN|";
private static string streetTypesS = "SHL|SHOAL|SHLS|SHOALS|SHOAR|SHORE|SHR|SHOARS|SHORES|SHRS|SKYWAY|SPG|SPNG|SPRING|SPRNG|SPGS|SPNGS|SPRINGS|SPRNGS|SPUR|SPURS|SQ|SQR|SQRE|SQU|SQUARE|SQRS|SQUARES|STA|STATION|STATN|STN|STRA|STRAV|STRAVE|STRAVEN|STRAVENUE|STRAVN|STRVN|STRVNUE|STREAM|STREME|STRM|ST|STR|STREET|STRT|STREETS|SMT|SUMIT|SUMITT|SUMMIT|";
private static string streetTypesT = "TER|TERR|TERRACE|THROUGHWAY|TRACE|TRACES|TRCE|TRACKTRACKS|TRAK|TRK|TRKS|TRAFFICWAY|TRFY|TR|TRAIL|TRAILS|TRL|TRLS|TUNEL|TUNL|TUNLS|TUNNEL|TUNNELS|TUNNL|TPK|TPKE|TRNPK|TRPK|TURNPIKE|TURNPK|";
private static string streetTypesU = "UNDERPASS|UN|UNION|UNIONS|";
private static string streetTypesV = "VALLEY|VALLY|VLLY|VLY|VALLEYS|VLYS|VDCT|VIA|VIADCT|VIADUCT|VIEW|VW|VIEWS|VWS|VILL|VILLAG|VILLAGE|VILLG|VILLIAGE|VLG|VILLAGES|VLGS|VILLE|VL|VIS|VIST|VISTA|VST|VSTA|";
private static string streetTypesW = "WALK|WALKS|WALL|WAY|WY|WAYS|WELL|WELLS|WLS|";
/// <summary>
/// The driver to lexical parse a us address.
/// </summary>
/// <param name="address">Input address.</param>
/// <returns>USA Street Address.</returns>
public static USAStreetAddress Parse(string address)
{
USAStreetAddress result = new USAStreetAddress();
StringBuilder sb = new StringBuilder();
CultureInfo c = CultureInfo.CurrentCulture;
ClearUSAStreetAddress();
if (string.IsNullOrEmpty(address))
{
return new USAStreetAddress();
}
try
{
var input = RemoveUnprintableChars(address.ToUpper());
if (POBoxAddress(address.ToUpper()).POBox.Equals(false))
{
var re2 = new Regex(Digits());
var re = new Regex(BuildUSAddressPattern());
if (re.IsMatch(input))
{
var m = re.Match(input);
result.OriginalAddess = address;
result.Number = m.Groups["Number"].Value;
result.Prefix = m.Groups["Prefix"].Value;
if (m.Groups["PreDirection"].Value.Length > 2)
{
result.Prefix = FormatProperCase(m.Groups["PreDirection"].Value);
}
else
{
result.Prefix = ToUpperCase(m.Groups["PreDirection"].Value);
}
if (m.Groups["PostDirection"].Value.Trim().Length > 2)
{
result.PostDirection = FormatProperCase(m.Groups["PostDirection"].Value);
}
else
{
result.PostDirection = ToUpperCase(m.Groups["PostDirection"].Value);
}
if (m.Groups["Type"].Value.Trim().Length > 2)
{
result.Type = FormatProperCase(m.Groups["Type"].Value);
}
else
{
switch (ToUpperCase(m.Groups["Type"].Value))
{
case "PL":
result.Type = "Pl";
break;
case "DR":
result.Type = "Dr";
break;
case "ST":
result.Type = "St";
break;
default:
result.Type = ToUpperCase(m.Groups["Type"].Value);
break;
}
}
if (m.Groups["Unit"].Value.Trim().Length > 2)
{
result.Unit = FormatProperCase(m.Groups["Unit"].Value);
}
else
{
result.Unit = ToUpperCase(m.Groups["Unit"].Value);
}
if (re2.IsMatch(m.Groups["Name"].Value))
{
result.Name = ToLowerCase(m.Groups["Name"].Value);
}
else
{
result.Name = FormatProperCase(m.Groups["Name"].Value);
}
if (result.POBox.Equals(false))
{
if (!result.Number.Trim().Equals(string.Empty))
{
sb.Append(" " + result.Number.Trim());
}
if (!result.Prefix.Trim().Equals(string.Empty))
{
sb.Append(" " + result.Prefix.Trim());
}
if (!result.PreDirection.Trim().Equals(string.Empty))
{
sb.Append(" " + result.PreDirection.Trim());
}
if (!result.Name.Trim().Equals(string.Empty))
{
sb.Append(" " + result.Name.Trim());
}
if (!result.PostDirection.Trim().Equals(string.Empty))
{
sb.Append(" " + result.PostDirection.Trim());
}
if (!result.Type.Trim().Equals(string.Empty))
{
sb.Append(" " + result.Type.Trim());
}
if (!result.Unit.Trim().Equals(string.Empty))
{
sb.Append(" " + result.Unit.Trim());
}
result.Addr = sb.ToString();
// result.Addr = result.Number + " " + result.Prefix + " " + result.PreDirection + " " + result.Name + " " + result.PostDirection + " " + result.Type + " " + result.Unit;
}
}
else
{
result.OriginalAddess = input;
}
}
if (result.Addr.Equals(string.Empty))
{
result.Addr = FormatProperCase(result.OriginalAddess);
}
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
return result;
}
/// <summary>
/// Build Regex Pattern for US address.
/// </summary>
/// <returns>A complete regex statement.</returns>
private static string BuildUSAddressPattern()
{
var pattern = "^" + //// beginning of string
"(?<Number>\\d+)" + //// 1 or more digits
"(?:\\s+(?<Prefix>" + GetPrefixes() + "))?" + //// whitespace + valid prefix (optional)
"(?:\\s+(?<PreDirection>" + GetDirection() + "))?" + //// whitespace + valid direction (optional)
"(?:\\s+(?<Name>.*?))" + //// whitespace + anything
"(?:" + //// group (optional) {
"(?:\\s+(?<PostDirection>" + GetDirection() + "))?" + //// whitespace + valid street direction (optional)
"(?:\\s+(?<Type>" + GetStreetTypes() + "))" + //// whitespace + valid street type
"(?:\\s+(?<Unit>.*))?" + //// whitespace + anything (optional)
")?" + //// }
"$"; //// end of string
return pattern;
}
/// <summary>
/// Create PO Box regex pattern.
/// </summary>
/// <returns>PO Box regex pattern.</returns>
private static string POBoxPattern()
{
var pattern = "^\\s*((P(OST)?.?\\s*(O(FF(ICE)?)?)?.?\\s+(B(IN|OX))?)|B(IN|OX))";
return pattern;
}
/// <summary>
/// Create PO Box number regex pattern.
/// </summary>
/// <returns>PO Box number regex pattern.</returns>
private static string POBoxNumberPattern()
{
var pattern = "(?<Number>\\d+)";
return pattern;
}
/// <summary>
/// Regex express for digits.
/// </summary>
/// <returns>Regex string expression.</returns>
private static string Digits()
{
return "^?\\d";
}
/// <summary>
/// Get US Postal Office recognize values for street types.
/// </summary>
/// <returns>A concatenated string of all street types.</returns>
private static string GetStreetTypes()
{
return streetTypesA + streetTypesB + streetTypesC + streetTypesD + streetTypesE + streetTypesF + streetTypesG + streetTypesH + streetTypesI + streetTypesJ + streetTypesK
+ streetTypesL + streetTypesM + streetTypesN + streetTypesO + streetTypesP + streetTypesR + streetTypesS + streetTypesT + streetTypesU + streetTypesV + streetTypesW;
}
/// <summary>
/// Changes input string to all lower case characters.
/// </summary>
/// <param name="value">Input value.</param>
/// <returns>Lower case value string.</returns>
private static string ToLowerCase(string value)
{
StringBuilder sb = new StringBuilder();
var s = value.ToCharArray();
for (int i = 0; i < s.Length; i++)
{
sb.Append(char.ToLower(s[i]));
}
return sb.ToString();
}
/// <summary>
/// Changes input string to all upper case characters.
/// </summary>
/// <param name="value">Input value.</param>
/// <returns>Upper case value string.</returns>
private static string ToUpperCase(string value)
{
StringBuilder sb = new StringBuilder();
var s = value.ToCharArray();
for (int i = 0; i < s.Length; i++)
{
sb.Append(char.ToUpper(s[i]));
}
return sb.ToString();
}
/// <summary>
/// Determines if address is a PO Box Address.
/// </summary>
/// <param name="address">PO Box Address.</param>
/// <returns>USA Street Address.</returns>
private static USAStreetAddress POBoxAddress(string address)
{
USAStreetAddress result = new USAStreetAddress();
ClearUSAStreetAddress();
var re = new Regex(POBoxPattern());
if (re.IsMatch(address).Equals(true))
{
re = new Regex(POBoxNumberPattern());
if (re.IsMatch(address))
{
var m = re.Match(address);
result.OriginalAddess = address;
result.POBoxNumber = "P.O. Box " + m.Groups["Number"].Value;
result.POBox = true;
result.Addr = "P.O. Box " + m.Groups["Number"].Value;
}
}
return result;
}
/// <summary>
/// Replaces commas with empty string from a string.
/// </summary>
/// <param name="input">String to search for commas.</param>
/// <returns>String with no commas.</returns>
private static string ReplaceComma(string input)
{
return input.Replace(",", string.Empty);
}
/// <summary>
/// Determines case in a string.
/// </summary>
/// <param name="value">Input string.</param>
/// <returns>Input leaving all upper case title case as is.</returns>
private static string FormatProperCase(string value)
{
TextInfo textInfo = new CultureInfo("en-US", false).TextInfo;
value = ProcessWhitespace(value);
if (!IsAllUpperOrAllLower(value))
{ // leave the CamelCase or Propercase names alone
return value;
}
else
{
return ProcessWhitespace(textInfo.ToTitleCase(value.ToLower())); // if all upper case titlecase leaves as is
}
}
/// <summary>
/// Regex expression for whitespace.
/// </summary>
/// <param name="value">Input string.</param>
/// <returns>Trims leading and ending spaces and multiple spaces to single space.</returns>
private static string ProcessWhitespace(string value)
{
string space = " ";
value = value.Trim().TrimStart().TrimEnd();
value = Regex.Replace(value, @"\s+", space);
return value;
}
/// <summary>
/// Determines in string is upper or lower case.
/// </summary>
/// <param name="input">Input string.</param>
/// <returns>True or false.</returns>
private static bool IsAllUpperOrAllLower(string input)
{
return input.ToLower().Equals(input) || input.ToUpper().Equals(input);
}
/// <summary>
/// Clears values.
/// </summary>
private static void ClearUSAStreetAddress()
{
USAStreetAddress result = new USAStreetAddress();
result.Addr = string.Empty;
result.City = string.Empty;
result.Name = string.Empty;
result.Number = string.Empty;
result.OriginalAddess = string.Empty;
result.POBox = false;
result.POBoxNumber = string.Empty;
result.PostDirection = string.Empty;
result.PreDirection = string.Empty;
result.Prefix = string.Empty;
result.PrettyAddess = string.Empty;
result.State = string.Empty;
result.Type = string.Empty;
result.Unit = string.Empty;
result.Zip = string.Empty;
}
/// <summary>
/// Create Regex expression to remove unprintable characters.
/// </summary>
/// <param name="s">Input string.</param>
/// <returns>Regex expression.</returns>
private static string RemoveUnprintableChars(string s)
{
return Regex.Replace(s, "[\u0000-\u001F]", string.Empty);
}
/// <summary>
/// Create street prefix regex pattern.
/// </summary>
/// <returns>Returns prefix regex pattern.</returns>
private static string GetPrefixes()
{
return "TE|HW|RD|MA|EI|NO|AU|GR|OL|SW|HA|JO|OV|OH|K";
}
/// <summary>
/// Create street direction regex pattern.
/// </summary>
/// <returns>Returns string direction pattern.</returns>
private static string GetDirection()
{
return "NW|E|SE|W|SW|S|NE|N|NORTHWEST|EAST|SOUTHEAST|WEST|SOUTHWEST|SOUTH|NORTHEAST|NORTH|EST";
}
} // end of class
} // end of namespace