Class: Utilities

Inherits:
Object
  • Object
show all
Defined in:
lib/timetwister/utilities.rb

Class Method Summary collapse

Class Method Details

.clean_string(string) ⇒ Object

removes sub-strings that do not contain parsable data input: freetext string output: same string, ready for the parser



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
# File 'lib/timetwister/utilities.rb', line 224

def self.clean_string(string)
	r = @regex_tokens
	# remove n.y. and variants from beginning of string
	substrings = [
		/\[n\.?y\.?\]/,
		/[\[\]\(\)]/,
		/[\.\,\)\;\:]*$/,
		/\?/,
		/approx\.?(imately)?/i,
		/\s#{regex_tokens[:circa]}\s/,
		/^#{regex_tokens[:circa]}\s/,
		Regexp.new("([\,\;\s(and)]{0,4}#{regex_tokens[:nd]})?$")
	]

	# transform seasons to months
	string = replace_seasons(string)

	# remove days of the week
	dow = [/[Ss]unday,?\s+/, /[Mm]onday,?\s+/, /[Tt]uesday,?\s+/, /[Ww]ednesday,?\s+/, /[Tt]hursday,?\s+/, /[Ff]riday,?\s+/, /[Ss]aturday,?\s+/]
	dow.each {|d| string.gsub!(d, '')}

	# remove times of day
	tod = [/[Mm]orning,?\s+/, /[Aa]fternoon,?\s+/, /[Ee]vening,?\s+/, /[Nn]ight,?\s+/]
	tod.each {|t| string.gsub!(t, '')}

	# remove single question marks
	string.gsub!(/([0-9])\?([^\?])/,'\1\2')

	substrings.each { |s| string.gsub!(s,'') }
	string.strip!
	return string
end

.datetime_comparitor(datetime) ⇒ Object

transforms a datetime object into an int input: datetime output: same datetime, transformed into an int



198
199
200
201
202
# File 'lib/timetwister/utilities.rb', line 198

def self.datetime_comparitor(datetime)
	d = datetime.to_s
	d.gsub!(/[^\d]/,'')
	return d.to_i
end

.days_in_month(month, year) ⇒ Object

returns the days in a given month input: a month and year (int, or numeric strings) output: the number of days in that month in that year



174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
# File 'lib/timetwister/utilities.rb', line 174

def self.days_in_month(month,year)
	month = month.kind_of?(String) ? month.to_i : month
	year = year.kind_of?(String) ? year.to_i : year
	days = {
		1 => 31,
		2 => leap_year?(year) ? 29 : 28,
		3 => 31,
		4 => 30,
		5 => 31,
		6 => 30,
		7 => 31,
		8 => 31,
		9 => 30,
		10 => 31,
		11 => 30,
		12 => 31
	}
	days[month]
end

.extract_year(string) ⇒ Object

Removes the first 4-digit number found in the string and returns it



280
281
282
283
284
# File 'lib/timetwister/utilities.rb', line 280

def self.extract_year(string)
	year = string.match(/\d{4}/).to_s
	string.gsub!(Regexp.new(year),'')
	year
end

.language_to_english(str) ⇒ Object

replaces non-english language months with english months input: freetext date string output: same string, but with months replaced by english months



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
# File 'lib/timetwister/utilities.rb', line 101

def self.language_to_english(str)

	work_str = str.clone

	languages = {

		# french
		'janvier' => 'January',
		'février' => 'February',
		'mars' => 'March',
		'avril' => 'April',
		'mai' => 'May',
		'juin' => 'June',
		'juillet' => 'July',
		'août' => 'August',
		'septembre' => 'September',
		'octobre' => 'October',
		'novembre' => 'November',
		'décembre' => 'December',

		# spanish
		'enero' => 'January',
		'febrero' => 'February',
		'marzo' => 'March',
		'abril' => 'April',
		'mayo' => 'May',
		'junio' => 'June',
		'julio' => 'July',
		'agosto' => 'August',
		'septiembre' => 'September',
		'octubre' => 'October',
		'noviembre' => 'November',
		'diciembre' => 'December',

		# italian
		'gennaio' => 'January',
		'febbraio' => 'February',
		# redundant date removed
		'aprile' => 'April',
		'maggio' => 'May',
		'giugno' => 'June',
		'luglio' => 'July',
		# redundant date removed
		'settembre' => 'September',
		'ottobre' => 'October',
		# redundant date removed
		'dicembre' => 'December',

		# german
		'januar[^y]' => 'January',
		'februar[^y]' => 'February',
		'märz' => 'March',
		'april' => 'April',
		# redundant date removed
		'juni' => 'June',
		'juli' => 'July',
		'august' => 'August',
		'september' => 'September',
		'oktober' => 'October',
		'november' => 'November',
		'dezember' => 'December'
	}

	languages.each do |key, value|
		work_str.gsub!(/#{key}/i, value)
	end

	return work_str
end

.leap_year?(year) ⇒ Boolean

determines if a year is leap or not input: a year as an int or string output: boolean of whether the year is leap or not

Returns:

  • (Boolean)


208
209
210
211
212
213
214
215
216
217
218
219
# File 'lib/timetwister/utilities.rb', line 208

def self.leap_year?(year)
	year = (year.kind_of? String) ? year.to_i : year
	if year % 400 == 0
		return true
	elsif year % 100 == 0
		return false
	elsif year % 4 == 0
		return true
	else
		return false
	end
end

.regex_tokensObject

regexes used by parser to detect various date forms



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
# File 'lib/timetwister/utilities.rb', line 287

def self.regex_tokens
	return {
		# 1969, [1969], c1969
		:year => '[\[\sc\(]{0,3}[0-2][0-9]{3}[\]\s\.\,;\?\)]{0,3}',
		# - or 'to'
		:range_delimiter => '\s*((\-)|(to))\s*',
		# , or ;
		:list_delimiter => '\s*[\,\;]\s*',
		# , or ;
		:range_or_list_delimiter => '\s*([\,\;]|((\-)|(to)))\s*',
		# n.d., undated, etc.
		:nd => '[\[\s]{0,2}\b([Uu]+ndated\.?)|([nN]o?\.?\s*[dD](ate)?\.?)\b[\s\]\.]{0,3}',
		# 1960s, 1960's
		:decade_s => '[\[\s]{0,2}[0-9]{3}0\'?s[\]\s]{0,2}',

		# 1970-75
		:year_range_short => '\s*[0-9]{4}\s?\-\s*(([2-9][0-9])|(1[3-9]))\s*',

		# 196-
		:decade_aacr => '[0-9]{3}\-',
		# named months, including abbreviations (case insensitive)
		:named_month => '\s*(?i)\b((jan(uary)?)|(feb(ruary)?)|(mar(ch)?)|(apr(il)?)|(may)|(jun(e)?)|(jul(y)?)|(aug(ust)?)|(sep(t|tember)?)|(oct(ober)?)|(nov(ember)?)|(dec(ember)?))\b\.?\s*',
		# circa, ca. - also matches 'c.', which is actually 'copyright', but is still not something we need to deal with
		:circa => '\s*[Cc](irc)?a?\.?\s*',
		# early, late, mid-
		:decade_qualifier => '(([Ee]arly)|([Mm]id)|([Ll]ate))\-?',
		# 06-16-1972, 6-16-1972
		:numeric_date_us => '(0?1)|(0?2)|(0?3)|(0?4)|(0?5)|(0?6)|(0?7)|(0?8)|(0?9)|1[0-2][\-\/](([0-2]?[0-9])|3[01])[\-\/])?[12][0-9]{3}',
		# 1972-06-16
		:iso8601 => '[0-9]{4}\-[0-9]{2}\-[0-9]{2}',
		:iso8601_full => '[0-9]{4}((\-[0-9]{2})(\-[0-9]{2})?)?',
		:iso8601_month => '[0-9]{4}\-[0-9]{2}',
		:anchor_start => '^[^\w\d]*',
		:anchor_end => '[^\w\d]*$',
		:optional_comma => '[\s\,]*',
		:day_of_month => '\s*(([0-2]?[0-9])|(3[0-1]))\s*'
	}
end

.replace_ordinals(str) ⇒ Object

replaces ordinal numbers in a date string with flat numbers input: freetext date string output: same string, but with ordinals replaced by numbers



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
# File 'lib/timetwister/utilities.rb', line 47

def self.replace_ordinals(str)

	work_str = str.clone

	ordinals = {
		# replace fulltext ordinals with numbers
		'first' => '1',
		'second' => '2',
		'third' => '3',
		'fourth' => '4',
		'fifth' => '5',
		'sixth' => '6',
		'seventh' => '7',
		'eighth' => '8',
		'ninth' => '9',
		'tenth' => '10',
		'eleventh' => '11',
		'twelfth' => '12',
		'thirteenth' => '13',
		'fourteenth' => '14',
		'fifteenth' => '15',
		'sixteenth' => '16',
		'seventeenth' => '17',
		'eighteenth' => '18',
		'nineteenth' => '19',
		'twentieth' => '20',
		'twenty-' => '2',
		'thirtieth' => '30',
		'thirty-' => '3',

		# replace numeric ordinals with plain numbers
		'1st' => '1',
		'2nd' => '2',
		'3rd' => '3',
		'3d' => '3',
		'4th' => '4',
		'5th' => '5',
		'6th' => '6',
		'7th' => '7',
		'8th' => '8',
		'9th' => '9',
		'0th' => '0'
	}

	ordinals.each do |key, value|
		work_str.gsub!(Regexp.new(key), value)
	end

	return work_str
end

.replace_seasons(string) ⇒ Object



257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
# File 'lib/timetwister/utilities.rb', line 257

def self.replace_seasons(string)
	seasons = {	'win[^\s]*'	=>	['January 1', 'March 20'],
				'spr[^\s]*'	=>	['March 20', 'June 21'],
				'sum[^\s]*'	=>	['June 21', 'September 23'],
				'aut[^\s]*'	=>	['September 23', 'December 31'],
				'fal[^\s]*'	=>	['September 23', 'December 31']}

	# if we're working with a range, we need to be a little careful, so that we don't create sub-ranges in a string
	is_range = string.match(/[^-]+?-[^-]+?/)

	seasons.each do |season, dates|
		regex = Regexp.new(season, Regexp::IGNORECASE)

		# is the season the beginning or the end of a range?
		new_date = (is_range ? (string.match(Regexp.new(season + '.+?-', Regexp::IGNORECASE)) ? dates[0] : dates[1]) : dates.join(' - '))
		
		string = string.gsub(regex, new_date)
	end

	return string
end

.return_certainty(str) ⇒ Object

return MODS certainty from a date string input: freetext date string output: string representing the date certainty



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/timetwister/utilities.rb', line 21

def self.return_certainty(str)

	# order of precedence, from least to most certain:
    # 1) questionable dates
    # 2) approximate dates
    # 3) inferred dates

    if str.include?('?')
      return 'questionable'
    end

    if str.downcase.include?('ca') || \
      str.downcase.include?('approx')
      return 'approximate'
    end

    if str.include?('[') || str.include?(']')
      return 'inferred'
    end

    return nil
end

.stringify_values(hash) ⇒ Object

walk through a hash and transforms all ints to strings input: a hash output: same hash, but with all Integers converted to strings



8
9
10
11
12
13
14
15
16
# File 'lib/timetwister/utilities.rb', line 8

def self.stringify_values(hash)
	hash.each do |k,v|
		if v.is_a?(Integer)
			hash[k] = v.to_s
		end
	end

	return hash
end