/
NormalizedLocation.cs
249 lines (217 loc) · 8.07 KB
/
NormalizedLocation.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
// -----------------------------------------------------------------------
// <copyright file="NormalizedLocation.cs" company="Microsoft">
// Copyright (c) Microsoft. All rights reserved.
// </copyright>
// -----------------------------------------------------------------------
namespace Microsoft.Geolocation.Whois.Normalization
{
using System;
using System.Collections.Generic;
using System.Text;
using Parsers;
public class NormalizedLocation
{
private static HashSet<string> allBlacklistedValues;
private static HashSet<string> blacklistedValuesExceptSimilarToCountries = new HashSet<string>(StringComparer.OrdinalIgnoreCase)
{
".",
"0",
"00000",
"99999",
"private",
"private residence",
"P.R.",
"Private Address",
"Private Addr",
"Private Addr.",
"Private Resident",
"Private Res.",
"PRIVATE CUSTOMER",
"private-address",
"Unavailable Street",
"1 Unavailable Street",
"Unavailable St",
"Unavailable Str",
"Unavailable St.",
"Unavailable Str.",
"Unavailable Address",
"Unavailable",
"Street Not Available",
"Address Not Available",
"Street N/A",
"Address N/A",
"N/A Street",
"N/A Addr",
"N/A Addr.",
"N/A Address",
"N/A St",
"N/A St.",
"N/A Str",
"N/A Str.",
"N/A",
"n.a.",
"n.a",
"1 na",
"Unknown",
"Postal Address",
"No info",
"Not Defined",
"Undefined",
"null",
"_None",
"None",
"Address",
"Country",
"City",
"Postal Code",
"Street",
"PostalCode",
"Private Data",
"SERVER",
"fake st",
"fake st.",
"FakePostalCode",
"Fake",
"FakeTown",
"Fakeville",
"FakeSuburb",
"Fake_State",
"Fake City",
"Fakeplace"
};
private static HashSet<string> blacklistedValuesSimilarToCountries = new HashSet<string>(StringComparer.OrdinalIgnoreCase)
{
"NA",
"NA NA",
"n",
"no_no"
};
private static HashSet<string> addressFields = new HashSet<string>(StringComparer.OrdinalIgnoreCase)
{
"Address",
"Org-Address",
"Customer Address",
"customer-address",
"Address-1"
};
private static HashSet<string> streetFields = new HashSet<string>(StringComparer.OrdinalIgnoreCase)
{
"Street",
"Street-Address"
};
private static HashSet<string> geolocationFields = new HashSet<string>(StringComparer.OrdinalIgnoreCase)
{
"GeoLocation",
"geoloc"
};
private static HashSet<string> cityFields = new HashSet<string>(StringComparer.OrdinalIgnoreCase)
{
"City",
"Org-City",
"Organization-City",
"Customer City"
};
private static HashSet<string> stateProvinceFields = new HashSet<string>(StringComparer.OrdinalIgnoreCase)
{
"State",
"Org-State",
"State-Province",
"StateProv",
"Organization-State",
"Customer State/Province",
"State/Prov"
};
private static HashSet<string> postalCodeFields = new HashSet<string>(StringComparer.OrdinalIgnoreCase)
{
"Postal-Code",
"Org-Zip",
"Customer Postal Code",
"Organization-Zip",
"PostalCode"
};
private static HashSet<string> countryFields = new HashSet<string>(StringComparer.OrdinalIgnoreCase)
{
"Country",
"Country-Code",
"Org-Country",
"Organization-Country",
"Customer Country Code"
};
static NormalizedLocation()
{
allBlacklistedValues = new HashSet<string>(StringComparer.OrdinalIgnoreCase);
allBlacklistedValues.UnionWith(blacklistedValuesSimilarToCountries);
allBlacklistedValues.UnionWith(blacklistedValuesExceptSimilarToCountries);
}
public string Address { get; set; }
public string Street { get; set; }
public string City { get; set; }
public string StateProvince { get; set; }
public string PostalCode { get; set; }
public string Country { get; set; }
public string Geolocation { get; set; }
public static NormalizedLocation TryParseFromSection(RawWhoisSection section)
{
var location = new NormalizedLocation()
{
Address = NormalizationUtils.FindFirstMatchingFieldValueInRecords(section, addressFields, allBlacklistedValues),
Street = NormalizationUtils.FindFirstMatchingFieldValueInRecords(section, streetFields, allBlacklistedValues),
City = NormalizationUtils.FindFirstMatchingFieldValueInRecords(section, cityFields, allBlacklistedValues),
StateProvince = NormalizationUtils.FindFirstMatchingFieldValueInRecords(section, stateProvinceFields, allBlacklistedValues),
PostalCode = RemoveExtraPrefix(NormalizationUtils.FindFirstMatchingFieldValueInRecords(section, postalCodeFields, allBlacklistedValues)),
Country = NormalizationUtils.FindFirstMatchingFieldValueInRecords(section, countryFields, blacklistedValuesExceptSimilarToCountries), // Not using blacklisted values because NA is a valid country
Geolocation = NormalizationUtils.FindFirstMatchingFieldValueInRecords(section, geolocationFields)
};
return location;
}
public bool AddressSeemsValid()
{
if (!string.IsNullOrWhiteSpace(this.Address))
{
return true;
}
// Note the Geolocation field is most likely longitude, latitude in this order
if (!string.IsNullOrWhiteSpace(this.Geolocation))
{
return true;
}
if (!string.IsNullOrWhiteSpace(this.Country))
{
if (!string.IsNullOrWhiteSpace(this.City))
{
return true;
}
if (!string.IsNullOrWhiteSpace(this.PostalCode))
{
return true;
}
}
return false;
}
public override string ToString()
{
var ret = new StringBuilder();
NormalizationUtils.AddToBuilderWithComma(ret, this.Address);
NormalizationUtils.AddToBuilderWithComma(ret, this.Street);
NormalizationUtils.AddToBuilderWithComma(ret, this.City);
NormalizationUtils.AddToBuilderWithComma(ret, this.StateProvince);
NormalizationUtils.AddToBuilderWithComma(ret, this.PostalCode);
NormalizationUtils.AddToBuilderWithComma(ret, this.Country);
return ret.ToString();
}
// Some postal codes look like this: Postal-Code: Code: W1J 6HL
// We want to remove the extra Code: prefix
private static string RemoveExtraPrefix(string text)
{
if (text != null)
{
var parts = text.Split(new char[] { ':' });
if (parts.Length == 2)
{
return parts[1].Trim();
}
}
return text;
}
}
}