Class: Horai::Parser

Inherits:
Object
  • Object
show all
Defined in:
lib/horai/parser.rb

Direct Known Subclasses

JaJP

Constant Summary collapse

DATE_DELIMITER =
(/[\/-]/)
TIME_DELIMITER =
(/[:]/)
AFTERNOON_PATTERN =
(/午後|ごご|夜|よる|(?<![a-z])pm(?![a-z])/i)

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.parse(text) ⇒ Object



13
14
15
16
# File 'lib/horai/parser.rb', line 13

def self.parse(text)
  @@instance ||= self.new
  @@instance.parse(text)
end

Instance Method Details

#datetime_delta(date, methods = {}) ⇒ Object



27
28
29
30
31
32
33
34
# File 'lib/horai/parser.rb', line 27

def datetime_delta(date, methods = {})
  date = date.dup
  methods.each_pair do |key, value|
    converter = 1.0.respond_to?(key) ? :to_f : :to_i;
    date += value.send(converter).send(key) - date.send(key).send(key)
  end
  date
end

#filter(pattern, type = :absolute, &block) ⇒ Object



18
19
20
21
# File 'lib/horai/parser.rb', line 18

def filter(pattern, type = :absolute, &block)
  @filters = [] if @filters.nil?
  @filters << { pattern: pattern, type => block }
end

#filtersObject



44
45
46
47
48
# File 'lib/horai/parser.rb', line 44

def filters
  return @filters if @filters
  register_filters
  @filters
end

#normalize(text) ⇒ Object



58
59
60
# File 'lib/horai/parser.rb', line 58

def normalize(text)
  text
end

#nowObject



39
40
41
42
# File 'lib/horai/parser.rb', line 39

def now
  @now ||= DateTime.now
  @now.dup
end

#parse(text) ⇒ Object



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
# File 'lib/horai/parser.rb', line 62

def parse(text)
  normalized = normalize(text)
  contexts = (normalized + "$").split(relative_keyword_patterns)
  date = now

  filtered = false

  contexts.each_with_index do |context, index|
    if contexts.size >= 2 && index + 1 != contexts.size
      mode = :relative
    else
      mode = :absolute
    end

    filters.each do |filter|
      if (matches = context.match(filter[:pattern])) && filter[mode]
        date = filter[mode].call(normalized, matches, date)
        filtered = true unless filtered
      end
    end
  end

  @now = nil

  return nil unless filtered
  return date
end

#register_filtersObject



36
37
# File 'lib/horai/parser.rb', line 36

def register_filters
end

#relative?(text) ⇒ Boolean

Returns:

  • (Boolean)


54
55
56
# File 'lib/horai/parser.rb', line 54

def relative?(text)
  text =~ relative_keyword_patterns
end

#relative_keyword_patternsObject



50
51
52
# File 'lib/horai/parser.rb', line 50

def relative_keyword_patterns
  /after/
end

#truncate_time(date) ⇒ Object



23
24
25
# File 'lib/horai/parser.rb', line 23

def truncate_time(date)
  datetime_delta(date, hour: 0, minute: 0, second: 0)
end

#year_normalize(year) ⇒ Object



90
91
92
93
94
95
96
97
98
99
100
# File 'lib/horai/parser.rb', line 90

def year_normalize(year)
  if year < 100
    year_xx = (now.year / 100).to_i
    if (year_xx - year).abs < 50
      year += year_xx * 100
    else
      year += (year_xx - 1) * 100
    end
  end
  year
end