Class: Iev::TermBuilder

Inherits:
Object
  • Object
show all
Includes:
Cli::Ui, Utilities
Defined in:
lib/iev/term_builder.rb

Constant Summary

Constants included from Utilities

Utilities::FIGURE_ONE_REGEX, Utilities::FIGURE_TWO_REGEX, Utilities::IMAGE_PATH_PREFIX, Utilities::SIMG_PATH_REGEX

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Utilities

#parse_anchor_tag, #replace_newlines

Methods included from Cli::Ui

debug, info, progress, set_ui_tag, warn

Constructor Details

#initialize(data) ⇒ TermBuilder

Returns a new instance of TermBuilder.



12
13
14
# File 'lib/iev/term_builder.rb', line 12

def initialize(data)
  @data = data
end

Instance Attribute Details

#dataObject (readonly)

Returns the value of attribute data.



24
25
26
# File 'lib/iev/term_builder.rb', line 24

def data
  @data
end

Class Method Details

.build_from(data) ⇒ Object



20
21
22
# File 'lib/iev/term_builder.rb', line 20

def self.build_from(data)
  new(data).build
end

Instance Method Details

#buildObject



16
17
18
# File 'lib/iev/term_builder.rb', line 16

def build
  build_term_object
end

#build_term_objectObject



41
42
43
44
45
46
47
48
# File 'lib/iev/term_builder.rb', line 41

def build_term_object
  set_ui_tag "#{term_id} (#{term_language})"
  progress "Processing term #{term_id} (#{term_language})..."

  split_definition

  Glossarist::LocalizedConcept.new(term_hash)
end

#extract_authoritative_sourceObject



253
254
255
256
257
258
259
260
261
262
263
# File 'lib/iev/term_builder.rb', line 253

def extract_authoritative_source
  source_val = find_value_for("SOURCE")
  return nil if source_val.nil?

  SourceParser.new(source_val, term_domain)
    .parsed_sources
    .compact
    .map do |source|
    source.merge({ "type" => "authoritative" })
  end
end

#extract_classificationObject



238
239
240
241
242
243
244
245
246
247
248
249
250
251
# File 'lib/iev/term_builder.rb', line 238

def extract_classification
  classification_val = find_value_for("SYNONYM1STATUS")

  case classification_val
  when ""
    "admitted"
  when "认可的", "допустимый", "admitido"
    "admitted"
  when "首选的", "suositettava", "suositeltava", "рекомендуемый", "preferente"
    "preferred"
  else
    classification_val
  end
end

#extract_definition_valueObject



208
209
210
211
212
213
214
# File 'lib/iev/term_builder.rb', line 208

def extract_definition_value
  return unless @definition

  Iev::Converter.mathml_to_asciimath(
    replace_newlines(parse_anchor_tag(@definition, term_domain)),
  ).strip
end

#extract_entry_statusObject



232
233
234
235
236
# File 'lib/iev/term_builder.rb', line 232

def extract_entry_status
  case find_value_for("STATUS").downcase
  when "standard" then "valid"
  end
end

#extract_examplesObject



216
217
218
219
220
221
222
# File 'lib/iev/term_builder.rb', line 216

def extract_examples
  @examples.map do |str|
    Iev::Converter.mathml_to_asciimath(
      replace_newlines(parse_anchor_tag(str, term_domain)),
    ).strip
  end
end

#extract_international_symbol_designationObject



203
204
205
206
# File 'lib/iev/term_builder.rb', line 203

def extract_international_symbol_designation
  raw_term = find_value_for("SYMBOLE")
  raw_term && build_symbol_designation(raw_term)
end

#extract_notesObject



224
225
226
227
228
229
230
# File 'lib/iev/term_builder.rb', line 224

def extract_notes
  @notes.map do |str|
    Iev::Converter.mathml_to_asciimath(
      replace_newlines(parse_anchor_tag(str, term_domain)),
    ).strip
  end
end

#extract_primary_designationObject



175
176
177
178
179
180
181
182
183
184
# File 'lib/iev/term_builder.rb', line 175

def extract_primary_designation
  raw_term = find_value_for("TERM")
  raw_term = "NA" if raw_term == "....."

  build_expression_designation(
    raw_term,
    attribute_data: find_value_for("TERMATTRIBUTE"),
    status: "preferred",
  )
end

#extract_superseded_conceptsObject



265
266
267
268
269
270
# File 'lib/iev/term_builder.rb', line 265

def extract_superseded_concepts
  replaces_val = find_value_for("REPLACES")
  return nil if replaces_val.nil?

  SupersessionParser.new(replaces_val).supersessions
end

#extract_synonymous_designationsObject



186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
# File 'lib/iev/term_builder.rb', line 186

def extract_synonymous_designations
  retval = (1..3).map do |num|
    designations = find_value_for("SYNONYM#{num}") || ""

    # Some synonyms have more than one entry
    designations.split(/<[pbr]+>/).map do |raw_term|
      build_expression_designation(
        raw_term,
        attribute_data: find_value_for("SYNONYM#{num}ATTRIBUTE"),
        status: find_value_for("SYNONYM#{num}STATUS")&.downcase,
      )
    end
  end

  retval.flatten.compact
end

#extract_termsObject



167
168
169
170
171
172
173
# File 'lib/iev/term_builder.rb', line 167

def extract_terms
  [
    extract_primary_designation,
    *extract_synonymous_designations,
    extract_international_symbol_designation,
  ].compact
end

#find_value_for(key) ⇒ Object



26
27
28
# File 'lib/iev/term_builder.rb', line 26

def find_value_for(key)
  data.fetch(key.to_sym, nil)&.sanitize
end

#flesh_date(incomplete_date) ⇒ Object



30
31
32
33
34
35
36
37
38
39
# File 'lib/iev/term_builder.rb', line 30

def flesh_date(incomplete_date)
  return incomplete_date if incomplete_date.nil? || incomplete_date.empty?

  year, month, day = incomplete_date.split("-")

  month ||= "01"
  day ||= "01"

  DateTime.parse("#{year}-#{month}-#{day}").to_s
end

#split_definitionObject

Splits unified definition (from the spreadsheet) into separate definition, examples, and notes strings (for YAMLs).

Sets @definition, @examples and @notes variables.



100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
# File 'lib/iev/term_builder.rb', line 100

def split_definition
  slicer_rx = %r{
    \s*
    (?:<p>\s*)?
    (
      (?<example>
        # English example
        \bEXAMPLE\b |
        ^\bExamples\s+are\b: |
        ^\bExamples\b: |
        ^\bExample\b: |
        # French examples
        \bEXEMPLE\b |
        ^\bExemples\b:
      )
      |
      (?<note>
        Note\s*\d+\sto\sentry: |
        Note&nbsp;\d+\sto\sentry: |
        Note\s*\d+\sto\sthe\sentry: |
        Note\sto\sentry\s*\d+: |
        Note\s*\d+?\sà\sl['’]article: |
        <NOTE/?>?\s*\d?\s+.*?– |
        NOTE(?:\s+-)? |
        Note\s+\d+\s– |
        Note&nbsp;\d+\s
      )
    )
    \s*
  }x

  @examples = []
  @notes = []
  definition_arr = [] # here array for consistent interface

  next_part_arr = definition_arr
  remaining_str = find_value_for("DEFINITION")

  while (md = remaining_str&.match(slicer_rx))
    next_part = md.pre_match
    next_part.sub!(/^\[:Ex(a|e)mple\]/, 'Ex\\1mple')
    next_part_arr.push(next_part)
    next_part_arr = md[:example] ? @examples : @notes
    # 112-03-17
    # supplements the name of a quantity, especially for a component in a
    # system, to indicate the quotient of that quantity by the total
    # volume
    # <NOTE – Examples: amount-of-substance volume concentration of
    # component B (or concentration of B, in particular, ion
    # concentration), molecular concentration of B, electron concentration
    # (or electron density).
    #
    # In the above case the `Example` is part of the note but the regex
    # above will capture it as an example and will add an empty `Note`
    # and put the rest in an `Example`. So In this case we will replace
    # the `Example` with `[:Example]` and revert it in the next iteration
    # so it will not be caught by the regex.
    remaining_str = md.post_match
    remaining_str.sub!(/^Ex(a|e)mple/, '[:Ex\\1mple]') if md[:note]
  end

  remaining_str&.sub!(/^\[:Ex(a|e)mple\]/, 'Ex\\1mple')
  next_part_arr.push(remaining_str)
  @definition = definition_arr.first
  @definition = nil if @definition&.empty?
end

#term_domainObject



88
89
90
# File 'lib/iev/term_builder.rb', line 88

def term_domain
  @term_domain ||= term_id.slice(0, 3)
end

#term_hashObject



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/iev/term_builder.rb', line 50

def term_hash
  dates = nil

  if flesh_date(find_value_for("PUBLICATIONDATE"))
    dates = [
      {
        type: :accepted,
        date: flesh_date(find_value_for("PUBLICATIONDATE")),
      },
      {
        type: :amended,
        date: flesh_date(find_value_for("PUBLICATIONDATE")),
      },
    ]
  end

  {
    id: term_id,
    entry_status: extract_entry_status,
    classification: extract_classification,
    dates: dates,
    review_date: flesh_date(find_value_for("PUBLICATIONDATE")),
    review_decision_date: flesh_date(find_value_for("PUBLICATIONDATE")),
    review_decision_event: "published",
    terms: extract_terms,
    notes: extract_notes,
    examples: extract_examples,
    definition: [{ "content" => extract_definition_value }],
    sources: extract_authoritative_source,
    language_code: term_language,
    related: extract_superseded_concepts,
  }.compact
end

#term_idObject



84
85
86
# File 'lib/iev/term_builder.rb', line 84

def term_id
  @term_id ||= find_value_for("IEVREF")
end

#term_languageObject



92
93
94
# File 'lib/iev/term_builder.rb', line 92

def term_language
  @term_language ||= find_value_for("LANGUAGE").to_three_char_code
end