class FuzzyDate
Constants
- FORMAT_2_REGEX
Attributes
max_fuzz[RW]
Public Class Methods
new(max_fuzz=2)
click to toggle source
# File lib/fuzzy_matcher/fuzzy_date.rb, line 24 def initialize(max_fuzz=2) @fsub = FuzzySub.new FuzzySub::CHAR_2_NUM_SUB @max_fuzz = max_fuzz @scanners = [] FORMAT_2_REGEX.each do |key, value| register key, value end end
Public Instance Methods
fscan(string, fuzziness=2)
click to toggle source
allow fuzziness of 2 by default
# File lib/fuzzy_matcher/fuzzy_date.rb, line 38 def fscan(string, fuzziness=2) @scanners.map do |fdscan| matches = fdscan.fscan!(string, fuzziness) [matches, fdscan.format] if !matches.empty? end.compact end
matches_to_dates(matches)
click to toggle source
# File lib/fuzzy_matcher/fuzzy_date.rb, line 77 def matches_to_dates(matches) dates = [] matches.each do |m| # p "#{self.class.to_s} match: #{m[0]} with format #{m[1]}" strings = m[0] format = m[1] strings.each do |str| k = validaterize str, format # p "#{k[0]}, #{k[1]}" begin date = Date.strptime(k[0], k[1]) dates << date rescue ArgumentError # p "String #{k[0]} is not valide date for date format #{k[1]}" end end end dates end
register(format, regex)
click to toggle source
# File lib/fuzzy_matcher/fuzzy_date.rb, line 33 def register(format, regex) @scanners << FuzzyDateScanner.new(format, regex) end
to_date(string)
click to toggle source
iteratively find the dates, try fuzziness 1 and then 2
# File lib/fuzzy_matcher/fuzzy_date.rb, line 99 def to_date(string) dates = [] fuzz = 1 while fuzz <= @max_fuzz do matches = fscan string, fuzz dates = matches_to_dates matches break if !dates.empty? fuzz = fuzz + 1 end dates end
validaterize(m, format)
click to toggle source
# File lib/fuzzy_matcher/fuzzy_date.rb, line 45 def validaterize(m, format) str = m[0] case format when "%Y-%m-%d" date = @fsub.fsub!(m[3]) mont = @fsub.fsub!(m[2]) year = @fsub.fsub!(m[1]) str = "#{year}-#{mont}-#{date}" when "%m/%d/%Y" date = @fsub.fsub!(m[2]) mont = @fsub.fsub!(m[1]) year = @fsub.fsub!(m[3]) format = "%m/%d/%y" if year.length < 4 str = "#{mont}/#{date}/#{year}" when "%d/%m/%Y" date = @fsub.fsub!(m[1]) mont = @fsub.fsub!(m[2]) year = @fsub.fsub!(m[3]) format = "%d/%m/%y" if year.length < 4 str = "#{date}/#{mont}/#{year}" when "%d%b%Y" date = @fsub.fsub!(m[1]) mont = m[2].upcase year = @fsub.fsub!(m[3]) format = "%d%b%y" if year.length < 4 str = "#{date}#{mont}#{year}" end [str.strip, format] end