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::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.



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_concept_dataObject



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

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



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# 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

  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



260
261
262
263
264
265
266
267
268
269
270
# File 'lib/iev/term_builder.rb', line 260

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



245
246
247
248
249
250
251
252
253
254
255
256
257
258
# File 'lib/iev/term_builder.rb', line 245

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



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

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. IEVREF format: “AAA-BB-CC” where AAA = area, AAA-BB = section. Returns a URI reference to the section concept (e.g. “section-103-01”).



107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/iev/term_builder.rb', line 107

def extract_domain
  return nil unless term_id

  section_code = term_id.split("-")[0..1].join("-")
  section = Iev.find_section(section_code)
  return SubjectAreas.section_uri(section_code) if section

  area_code = term_id.split("-")[0]
  SubjectAreas.area_uri(area_code)
rescue StandardError
  nil
end

#extract_entry_statusObject



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

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

#extract_examplesObject



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

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



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

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

#extract_notesObject



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

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

#extract_primary_designationObject



185
186
187
188
189
190
191
192
193
194
# File 'lib/iev/term_builder.rb', line 185

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



272
273
274
275
276
277
# File 'lib/iev/term_builder.rb', line 272

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



196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
# File 'lib/iev/term_builder.rb', line 196

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



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

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.



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
168
169
170
171
172
173
174
175
# File 'lib/iev/term_builder.rb', line 124

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



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

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

#term_idObject



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

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

#term_languageObject



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

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