Class: Krikri::Enrichments::TimespanSplit

Inherits:
Object
  • Object
show all
Includes:
FieldEnrichment
Defined in:
lib/krikri/enrichments/timespan_split.rb

Overview

Splits edm:TimeSpan labels and populates `#begin` and `#end`. Any values generated from string values are left as strings to be normalized by another enrichment.

Ignores values that are neither a String or an edm:TimeSpan

Converts string values to a timespan with the value as providedLabel and enriches as below.

Populates the `#begin`, and `#end` properties of TimeSpan objects from `#providedLabel`. If a `#providedLabel` is present, but begin and end are missing, runs the `Krikri::Util::ExtendedDateParser`.

Once a begin and end date are recognized, the enrichment checks their validity (that the begin date comes before any existing end dates, or vice versa) and writes whichever value(s) it finds empty. The earliest/latest dates are used for begin/end, respectively.

If more than one `#providedLabel` is given, each is processed in turn and the widest possible range is used.

Returns the TimeSpan unaltered if both `#begin` and `#end` are present or valid values cannot be found for empty fields.

Constant Summary

Constants included from SoftwareAgent

SoftwareAgent::Logger

Instance Method Summary collapse

Methods included from FieldEnrichment

#enrich, #enrich_all, #enrich_field

Methods included from Krikri::Enrichment

#enrich, #enrich!, #list_fields

Methods included from SoftwareAgent

#agent_name, #log, #run

Instance Method Details

#enrich_value(value) ⇒ Object


31
32
33
34
35
# File 'lib/krikri/enrichments/timespan_split.rb', line 31

def enrich_value(value)
  value = timespan_from_string(value) if value.is_a? String
  return value unless value.is_a? DPLA::MAP::TimeSpan
  populate_timespan(value)
end

#parse_labels(labels) ⇒ Object


58
59
60
# File 'lib/krikri/enrichments/timespan_split.rb', line 58

def parse_labels(labels)
  labels.map { |l| Krikri::Util::ExtendedDateParser.parse(l, true) }.compact
end

#populate_timespan(timespan) ⇒ Object


43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/krikri/enrichments/timespan_split.rb', line 43

def populate_timespan(timespan)
  return timespan unless (timespan.begin.empty? || timespan.end.empty?) &&
    !timespan.providedLabel.empty?

  parsed = parse_labels(timespan.providedLabel)
  return timespan if parsed.empty?
  parsed.each do |date|
    begin_date, end_date = span_from_date(date)
    timespan.begin << begin_date
    timespan.end << end_date
  end
  reduce_to_largest_span(timespan)
  return timespan
end

#reduce_to_largest_span(timespan) ⇒ Object


69
70
71
72
73
# File 'lib/krikri/enrichments/timespan_split.rb', line 69

def reduce_to_largest_span(timespan)
  timespan.begin = timespan.begin.sort.first
  timespan.end = timespan.end.sort.last
  timespan
end

#span_from_date(date) ⇒ Object


62
63
64
65
66
67
# File 'lib/krikri/enrichments/timespan_split.rb', line 62

def span_from_date(date)
  return [nil, nil] if date.nil?
  return [date, date] if date.is_a? Date
  [(date.respond_to?(:first) ? date.first : date.from),
   (date.respond_to?(:last) ? date.last : date.to)]
end

#timespan_from_string(value) ⇒ Object


37
38
39
40
41
# File 'lib/krikri/enrichments/timespan_split.rb', line 37

def timespan_from_string(value)
  timespan = DPLA::MAP::TimeSpan.new
  timespan.providedLabel = value
  timespan
end