Class: Treat::Workers::Extractors::Time::Chronic

Inherits:
Object
  • Object
show all
Defined in:
lib/treat/workers/extractors/time/chronic.rb

Overview

Time/date extraction using a rule-based, pure Ruby natural language date parser.

Class Method Summary collapse

Class Method Details

.remove_time_from_ancestors(entity, time) ⇒ Object

Keeps the lowest-level time annotations that do not conflict with a higher time annotation. Returns true if the entity conflicts with a higher-level time annotation.



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/treat/workers/extractors/time/chronic.rb', line 37

def self.remove_time_from_ancestors(entity, time)
  
  entity.ancestors_with_type(:phrase).each do |a|
    
    next if !a.has?(:time)
    unless a.get(:time) == time
      return true
    end
    a.unset(:time)
    
  end
  
  false
  
end

.time(entity, options = {}) ⇒ Object

Return the date information contained within the entity by parsing it with the ‘chronic’ gem.

Options: none.



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/treat/workers/extractors/time/chronic.rb', line 15

def self.time(entity, options = {})

  s = entity.to_s
  return if s =~ /^[0-9]+$/
  time = nil
  
  silence_warnings do
    time = ::Chronic.parse(s, {:guess => true})
  end
  
  if entity.has_parent? && remove_time_from_ancestors(entity, time)
    nil
  else
    time
  end
  
end