Class: Treat::Workers::Extractors::Time::Ruby
- Inherits:
-
Object
- Object
- Treat::Workers::Extractors::Time::Ruby
- Defined in:
- lib/treat/workers/extractors/time/ruby.rb
Overview
Date extraction using Ruby’s standard library DateTime.parse() method.
Class Method Summary collapse
-
.remove_time_from_ancestors(entity, time) ⇒ Object
Keeps the lowest-level time annotations that do not conflict with a higher time annotation.
-
.time(entity, options = {}) ⇒ Object
Return a DateTime object representing the date/time contained within the entity, using Ruby’s native date/time parser.
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.
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/treat/workers/extractors/time/ruby.rb', line 36 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 a DateTime object representing the date/time contained within the entity, using Ruby’s native date/time parser. This extractor is suitable for the detection of well-structured dates and times, such as 2011/02/03 5:00.
Options: none.
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
# File 'lib/treat/workers/extractors/time/ruby.rb', line 15 def self.time(entity, = {}) s = entity.to_s return if s =~ /^[0-9]+$/ begin time = ::DateTime.parse(s) if entity.has_parent? && remove_time_from_ancestors(entity, time) nil else time end rescue nil end end |