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.



14
15
16
# File 'lib/iev/term_builder.rb', line 14

def initialize(data)
  @data = data
end

Instance Attribute Details

#dataObject (readonly)

Returns the value of attribute data.



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

def data
  @data
end

Class Method Details

.build_from(data) ⇒ Object



22
23
24
# File 'lib/iev/term_builder.rb', line 22

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

Instance Method Details

#buildObject



18
19
20
# File 'lib/iev/term_builder.rb', line 18

def build
  build_term_object
end

#build_term_objectObject



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

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



256
257
258
259
260
261
262
263
264
265
266
# File 'lib/iev/term_builder.rb', line 256

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



241
242
243
244
245
246
247
248
249
250
251
252
253
254
# File 'lib/iev/term_builder.rb', line 241

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



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

def extract_definition_value
  if @definition
    IEV::Converter.mathml_to_asciimath(
      replace_newlines(parse_anchor_tag(@definition, term_domain)),
    ).strip
  end
end

#extract_entry_statusObject



234
235
236
237
238
239
# File 'lib/iev/term_builder.rb', line 234

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

#extract_examplesObject



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

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



205
206
207
208
# File 'lib/iev/term_builder.rb', line 205

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

#extract_notesObject



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

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



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

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



268
269
270
271
272
273
# File 'lib/iev/term_builder.rb', line 268

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



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

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



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

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

#find_value_for(key) ⇒ Object



28
29
30
# File 'lib/iev/term_builder.rb', line 28

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

#flesh_date(incomplete_date) ⇒ Object



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

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.



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
166
167
# File 'lib/iev/term_builder.rb', line 102

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



90
91
92
# File 'lib/iev/term_builder.rb', line 90

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

#term_hashObject



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
83
84
# File 'lib/iev/term_builder.rb', line 52

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



86
87
88
# File 'lib/iev/term_builder.rb', line 86

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

#term_languageObject



94
95
96
# File 'lib/iev/term_builder.rb', line 94

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