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::IEV_CODE_RE, Utilities::IEV_CODE_TEXT_RE, 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, error, info, progress, set_ui_tag, warn

Constructor Details

#initialize(data) ⇒ TermBuilder

Returns a new instance of TermBuilder.



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

def initialize(data)
  @data = data
end

Instance Attribute Details

#dataObject (readonly)

Returns the value of attribute data.



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

def data
  @data
end

Class Method Details

.build_from(data) ⇒ Object



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

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

Instance Method Details

#buildObject



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

def build
  build_term_object
end

#build_concept_dataObject



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
85
86
87
88
89
90
91
# File 'lib/iev/term_builder.rb', line 59

def build_concept_data
  cd = Glossarist::ConceptData.new
  cd.id = term_id
  cd.language_code = term_language

  pub_date = flesh_date(find_value_for("PUBLICATIONDATE"))
  if pub_date
    cd.dates = [
      Glossarist::ConceptDate.new(type: "accepted", date: pub_date),
      Glossarist::ConceptDate.new(type: "amended", date: pub_date),
    ]
    cd.review_date = pub_date
    cd.review_decision_date = pub_date
  end
  cd.review_decision_event = "published"

  definition = extract_definition_value
  cd.definition = [definition] if definition
  cd.examples = extract_examples
  cd.notes = extract_notes
  cd.terms = extract_terms

  domain = extract_domain
  cd.domain = domain if domain

  sources = extract_authoritative_source
  cd.sources = sources if sources&.any?

  related = extract_superseded_concepts
  cd.related = related if related&.any?

  cd
end

#build_term_objectObject



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/iev/term_builder.rb', line 42

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

  split_definition

  concept_data = build_concept_data

  concept = Glossarist::LocalizedConcept.new
  concept.data = concept_data
  concept.id = term_id
  concept.entry_status = extract_entry_status
  concept.classification = extract_classification

  concept
end

#extract_authoritative_sourceObject



265
266
267
268
269
270
271
272
273
274
275
# File 'lib/iev/term_builder.rb', line 265

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

  sources = SourceParser.new(source_val, term_domain)
    .parsed_sources
    .compact

  sources.each { |src| src.type = "authoritative" }
  sources.empty? ? nil : sources
end

#extract_classificationObject



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

def extract_classification
  classification_val = find_value_for("SYNONYM1STATUS")

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

#extract_definition_valueObject



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

def extract_definition_value
  return unless @definition

  content = convert_content(@definition)
  Glossarist::DetailedDefinition.new(content: content)
end

#extract_domainObject

Derives the domain (subject area section) from the IEVREF identifier.

Returns the section or area title text as a localized string. Per the concept model, ConceptData#domain is a LocalizedString (the domain name), not a URI. Structural membership is expressed via ManagedConceptData#domains[] with ConceptReference objects.



111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/iev/term_builder.rb', line 111

def extract_domain
  return nil unless term_id

  code = IevCode.new(term_id)
  section = Iev.find_section(code.section_code) if code.section_code
  return section.title if section

  area = Iev.find_subject_area(code.area_code)
  area&.title
rescue StandardError => e
  warn "IEV: extract_domain failed for #{term_id}: #{e.message}"
  nil
end

#extract_entry_statusObject



244
245
246
247
248
# File 'lib/iev/term_builder.rb', line 244

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

#extract_examplesObject



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

def extract_examples
  @examples.map do |str|
    content = convert_content(clean_extracted_text(str))
    Glossarist::DetailedDefinition.new(content: content)
  end
end

#extract_international_symbol_designationObject



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

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

#extract_notesObject



237
238
239
240
241
242
# File 'lib/iev/term_builder.rb', line 237

def extract_notes
  @notes.map do |str|
    content = convert_content(clean_extracted_text(str))
    Glossarist::DetailedDefinition.new(content: content)
  end
end

#extract_primary_designationObject



190
191
192
193
194
195
196
197
198
199
# File 'lib/iev/term_builder.rb', line 190

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



277
278
279
280
281
282
# File 'lib/iev/term_builder.rb', line 277

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



201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
# File 'lib/iev/term_builder.rb', line 201

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



182
183
184
185
186
187
188
# File 'lib/iev/term_builder.rb', line 182

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

#find_value_for(key) ⇒ Object



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

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

#flesh_date(incomplete_date) ⇒ Object



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

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.



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
168
169
170
171
172
173
174
175
176
177
178
179
180
# File 'lib/iev/term_builder.rb', line 129

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+[–-]\s* |
        NOTE(?:\s+-)?\s* |
        Note\s+\d+\s[–-]\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
    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



97
98
99
# File 'lib/iev/term_builder.rb', line 97

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

#term_idObject



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

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

#term_languageObject



101
102
103
# File 'lib/iev/term_builder.rb', line 101

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