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
|
# File 'lib/bpl_enrich/dates.rb', line 44
def self.standardize(value)
date_data = {} source_date_string = value.strip
original_value = value
value = convert_month_words(value)
if (value.match(/([Pp]re|[Pp]ost|[Bb]efore|[Aa]fter|[Uu]nknown|[Uu]ndated|n\.d\.)/)) ||
(value.match(/\d\d\d\d-\z/)) || (value.match(/\d\d-\d\d\/\d\d/)) || (value.match(/\d*\(\d*\)/)) || (value.scan(/\d\d\d\d/).length > 2) || (value.scan(/([Ee]arly|[Ll]ate|[Mm]id|[Ww]inter|[Ss]pring|[Ss]ummer|[Ff]all)/).length > 1) ||
(!value.match(/(\d\dth [Cc]entury|\d\d\d-\?*|\d\d\d\?|\d\d\?\?|\d\d\d\d)/))
date_data[:date_note] = source_date_string
else
if value.include? '?'
date_data[:date_qualifier] = 'questionable'
elsif value.match(/\A[Cc]/)
date_data[:date_qualifier] = 'approximate'
elsif (value.match(/[\[\]]+/)) || (value.match(/[(][A-Za-z, \d]*[\d]+[A-Za-z, \d]*[)]+/)) date_data[:date_qualifier] = 'inferred'
end
value = value.gsub(/[\[\]\(\)\.,']/,'')
value = value.gsub(/(\b[Bb]etween\b|\bcirca\b|\bca\b|\Aca|\Ac)/,'').strip
if (value.scan(/\d\d\d\d/).length == 2) ||
(value.include? '0s') || (value.include? 'entury') || (value.match(/(\A\d\d\d\?|\A\d\d\?\?|\A\d\d\d-\?*|\d\d\d\d-\d\z|\d\d\d\d\/[\d]{1,2}\z)/)) ||
(value.match(/([Ee]arly|[Ll]ate|[Mm]id|[Ww]inter|[Ss]pring|[Ss]ummer|[Ff]all)/)) ||
((value.match(/\d\d\d\d-\d\d\z/)) && (value[-2..-1].to_i > 12))
date_data[:date_range] = {}
if value.scan(/\d\d\d\d/).length == 2
value = value.gsub(/(or|and|\|\|)/,'-').gsub(/[A-Za-z\?\s]/,'')
if value.match(/\A[12][\d]{3}-[01][\d]-[12][\d]{3}-[01][\d]\z/) date_data_range_start = value[0..6]
date_data_range_end = value[-7..-1]
elsif value.match(/\A[12][\d]{3}\/[12][\d]{3}\z/) date_data_range_start = value[0..3]
date_data_range_end = value[-4..-1]
else
range_dates = value.split('-') range_dates.each_with_index do |range_date,index|
if range_date.include? '/' range_date_pieces = range_date.split('/')
if range_date_pieces.last.length == 4
range_date_piece_year = range_date_pieces.last
range_date_piece_month = range_date_pieces.first.length == 2 ? range_date_pieces.first : '0' + range_date_pieces.first
if range_date_pieces.length == 3
range_date_piece_day = range_date_pieces[1].length == 2 ? range_date_pieces[1] : '0' + range_date_pieces[1]
end
value_to_insert = range_date_piece_year + '-' + range_date_piece_month
value_to_insert << '-' + range_date_piece_day if range_date_piece_day
elsif range_date_pieces.first.length == 4
range_date_piece_year = range_date_pieces.first
range_date_piece_month = range_date_pieces[1].length == 2 ? range_date_pieces[1] : '0' + range_date_pieces[1]
if range_date_pieces.length == 3
range_date_piece_day = range_date_pieces[2].length == 2 ? range_date_pieces[2] : '0' + range_date_pieces[2]
end
value_to_insert = range_date_piece_year + '-' + range_date_piece_month
value_to_insert << '-' + range_date_piece_day if range_date_piece_day
end
elsif range_date.match(/\A[12][\d]{3}\z/)
value_to_insert = range_date
end
if value_to_insert
if index == 0
date_data_range_start = value_to_insert
else
date_data_range_end = value_to_insert
end
end
end
end
else
text_range = value.match(/([Ee]arly|[Ll]ate|[Mm]id|[Ww]inter|[Ss]pring|[Ss]ummer|[Ff]all)/).to_s
if text_range.length > 0
date_data[:date_qualifier] ||= 'approximate' value = value.gsub(/#{text_range}/,'').strip
end
if value.match(/\A1\d\?\?\z/) date_data_range_start = value[0..1] + '00'
date_data_range_end = value[0..1] + '99'
elsif value.match(/\A[12]\d\d-*\?*\z/) date_data_range_start = value[0..2] + '0'
date_data_range_end = value[0..2] + '9'
elsif value.match(/\A[12]\d\d\d[-\/][\d]{1,2}\z/) if value.length == 7 && (value[5..6].to_i > value[2..3].to_i)
date_data_range_start = value[0..3]
date_data_range_end = value[0..1] + value[5..6]
elsif value.length == 6 && (value[5].to_i > value[3].to_i)
date_data_range_start = value[0..3]
date_data_range_end = value[0..2] + value[5]
end
date_data[:date_note] = source_date_string if text_range.length > 0
end
value = value.gsub(/\?/,'').strip
if value.match(/([12][\d]{1}th [Cc]entury|[12][\d]{1}00s)/) if value.match(/[12][\d]{1}00s/)
century_prefix_date = value.match(/[12][\d]{1}/).to_s
else
century_prefix_date = (value.match(/[12][\d]{1}/).to_s.to_i-1).to_s
end
if text_range.match(/([Ee]arly|[Ll]ate|[Mm]id)/)
if text_range.match(/[Ee]arly/)
century_suffix_dates = %w[00 39]
elsif text_range.match(/[Mm]id/)
century_suffix_dates = %w[30 69]
else
century_suffix_dates = %w[60 99]
end
end
date_data_range_start = century_suffix_dates ? century_prefix_date + century_suffix_dates[0] : century_prefix_date + '00'
date_data_range_end = century_suffix_dates ? century_prefix_date + century_suffix_dates[1] : century_prefix_date + '99'
else
value.match(/[12][1-9][1-9]0s/) ? is_decade = true : is_decade = false remaining_text = value.match(/\D+/).to_s
value = value.gsub(/#{remaining_text}/,'').strip if remaining_text.length > 0
if is_decade
decade_prefix_date = value.match(/\A[12][1-9][1-9]/).to_s
if text_range.match(/([Ee]arly|[Ll]ate|[Mm]id)/)
if text_range.match(/[Ee]arly/)
decade_suffix_dates = %w[0 3]
elsif text_range.match(/[Mm]id/)
decade_suffix_dates = %w[4 6]
else
decade_suffix_dates = %w[7 9]
end
end
date_data_range_start = decade_suffix_dates ? decade_prefix_date + decade_suffix_dates[0] : decade_prefix_date + '0'
date_data_range_end = decade_suffix_dates ? decade_prefix_date + decade_suffix_dates[1] : decade_prefix_date + '9'
else
single_year_prefix = value.match(/[12][0-9]{3}/).to_s
if text_range.length > 0
if text_range.match(/[Ee]arly/)
single_year_suffixes = %w[01 04]
elsif text_range.match(/[Mm]id/)
single_year_suffixes = %w[05 08]
elsif text_range.match(/[Ll]ate/)
single_year_suffixes = %w[09 12]
elsif text_range.match(/[Ww]inter/)
single_year_suffixes = %w[01 03]
elsif text_range.match(/[Ss]pring/)
single_year_suffixes = %w[03 05]
elsif text_range.match(/[Ss]ummer/)
single_year_suffixes = %w[06 08]
else text_range.match(/[F]all/)
single_year_suffixes = %w[09 11]
end
date_data_range_start = single_year_prefix + '-' + single_year_suffixes[0]
date_data_range_end = single_year_prefix + '-' + single_year_suffixes[1]
end
end
date_data[:date_note] = source_date_string if remaining_text.length > 1
end
end
if date_data_range_start && date_data_range_end
date_data[:date_range][:start] = date_data_range_start
date_data[:date_range][:end] = date_data_range_end
else
date_data[:date_note] ||= source_date_string
date_data.delete :date_range
end
else
value = value.gsub(/\?/,'') value = value.insert(-5, ' ') if value.match(/[A-Za-z]* \d{6}/) || value.match(/[A-Za-z]* \d{5}/)
if Timeliness.parse(original_value).nil?
value.split(' ').each do |split_value|
if split_value.match(/\A[12]\d\d\d[-\/\.][01][0-9]\z/) split_value = split_value.gsub(/[,\/\.]/, '-').squeeze('-')
date_data[:single_date] = split_value
elsif split_value.match(/\A[12]\d\d\d[-\/\.][01][0-9][-\/\.][01][0-9]\z/) split_value = split_value.gsub(/[,\/\.]/, '-').squeeze('-')
date_data[:single_date] = split_value
elsif split_value.match(/\A[01]?[1-9][-\/][12]\d\d\d\z/) split_value = '0' + split_value if split_value.match(/\A[1-9][-\/\.][12]\d\d\d\z/) date_data[:single_date] = split_value[3..6] + '-' + split_value[0..1]
elsif split_value.match(/\A[12]\d\d\d\z/) date_data[:single_date] = split_value
elsif split_value.match(/\A[01]?[1-9][-\/\.][01]?[1-9][-\/\.][12]\d\d\d\z/) split_value = split_value.gsub(/[,\/\.]/, '/').squeeze('-')
date_data[:single_date] = "#{split_value.split('/')[2]}-#{split_value.split('/')[0]}-#{split_value.split('/')[1]}" if split_value.include?('/')
date_data[:single_date] = "#{split_value.split('-')[2]}-#{split_value.split('-')[0]}-#{split_value.split('-')[1]}" if split_value.include?('-')
date_data[:single_date] = "#{split_value.split('.')[2]}-#{split_value.split('.')[0]}-#{split_value.split('.')[1]}" if split_value.include?('.')
end
end
if value.split(' ').length > 1 || date_data[:single_date].blank?
date_data[:date_note] = source_date_string
end
else
date_data[:single_date] = Timeliness.parse(original_value).strftime("%Y-%m-%d")
end
end
end
date_validation_array = []
date_validation_array << date_data[:single_date] if date_data[:single_date]
date_validation_array << date_data[:date_range][:start] if date_data[:date_range]
date_validation_array << date_data[:date_range][:end] if date_data[:date_range]
date_validation_array.each do |date_to_val|
if date_to_val.length == '7'
bad_date = true unless date_to_val[-2..-1].to_i.between?(1,12) && !date_to_val.nil?
elsif
date_to_val.length == '10'
bad_date = true unless Timeliness.parse(original_value) && !date_to_val.nil?
end
if bad_date
date_data[:date_note] ||= source_date_string
date_data.delete :single_date if date_data[:single_date]
date_data.delete :date_range if date_data[:date_range]
end
end
if date_data[:single_date].nil? && date_data[:date_range].nil? && date_data[:date_note].nil?
date_data[:date_note] = source_date_string
end
date_data
end
|