unitsdb 2.0.1 → 2.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,301 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module Unitsdb
4
- module Commands
5
- module Ucum
6
- # Matcher for UCUM and UnitsDB entities
7
- module Matcher
8
- module_function
9
-
10
- # Match UCUM entities to UnitsDB entities (UCUM → UnitsDB)
11
- def match_ucum_to_db(entity_type, ucum_entities, db_entities)
12
- puts "Matching UCUM #{entity_type} to UnitsDB #{entity_type}..."
13
-
14
- # Initialize result arrays
15
- matches = []
16
- missing_matches = []
17
- unmatched_ucum = []
18
-
19
- # Process each UCUM entity
20
- ucum_entities.each do |ucum_entity|
21
- match_data = find_db_match_for_ucum(ucum_entity, db_entities, entity_type)
22
-
23
- if match_data[:match]
24
- matches << { ucum_entity: ucum_entity, db_entity: match_data[:match] }
25
- elsif match_data[:potential_match]
26
- missing_matches << { ucum_entity: ucum_entity, db_entity: match_data[:potential_match] }
27
- else
28
- unmatched_ucum << ucum_entity
29
- end
30
- end
31
-
32
- [matches, missing_matches, unmatched_ucum]
33
- end
34
-
35
- # Match UnitsDB entities to UCUM entities (UnitsDB → UCUM)
36
- def match_db_to_ucum(entity_type, ucum_entities, db_entities)
37
- puts "Matching UnitsDB #{entity_type} to UCUM #{entity_type}..."
38
-
39
- # Initialize result arrays
40
- matches = []
41
- missing_refs = []
42
- unmatched_db = []
43
-
44
- # Process each UnitsDB entity
45
- db_entities.send(entity_type).each do |db_entity|
46
- # Skip entities that already have UCUM references
47
- if has_ucum_reference?(db_entity)
48
- matches << { db_entity: db_entity, ucum_entity: find_referenced_ucum_entity(db_entity, ucum_entities) }
49
- next
50
- end
51
-
52
- match_data = find_ucum_match_for_db(db_entity, ucum_entities, entity_type)
53
-
54
- if match_data[:match]
55
- missing_refs << { db_entity: db_entity, ucum_entity: match_data[:match] }
56
- else
57
- unmatched_db << db_entity
58
- end
59
- end
60
-
61
- [matches, missing_refs, unmatched_db]
62
- end
63
-
64
- # Check if a UnitsDB entity already has a UCUM reference
65
- def has_ucum_reference?(entity)
66
- return false unless entity.respond_to?(:references) && entity.references
67
-
68
- entity.references.any? { |ref| ref.authority == "ucum" }
69
- end
70
-
71
- # Find the referenced UCUM entity based on the reference URI
72
- def find_referenced_ucum_entity(db_entity, ucum_entities)
73
- return nil unless db_entity.respond_to?(:references) && db_entity.references
74
-
75
- ucum_ref = db_entity.references.find { |ref| ref.authority == "ucum" }
76
- return nil unless ucum_ref
77
-
78
- ref_uri = ucum_ref.uri
79
- ucum_entities.find { |ucum_entity| ucum_entity.identifier == ref_uri }
80
- end
81
-
82
- # Get the ID of a UnitsDB entity
83
- def get_entity_id(entity)
84
- entity.respond_to?(:id) ? entity.id : nil
85
- end
86
-
87
- # Find a matching UnitsDB entity for a UCUM entity
88
- def find_db_match_for_ucum(ucum_entity, db_entities, entity_type)
89
- result = { match: nil, potential_match: nil }
90
-
91
- # Different matching logic based on entity type
92
- case entity_type
93
- when "prefixes"
94
- result = match_prefix_ucum_to_db(ucum_entity, db_entities)
95
- when "units"
96
- result = match_unit_ucum_to_db(ucum_entity, db_entities)
97
- end
98
-
99
- result
100
- end
101
-
102
- # Find a matching UCUM entity for a UnitsDB entity
103
- def find_ucum_match_for_db(db_entity, ucum_entities, entity_type)
104
- result = { match: nil }
105
-
106
- # Different matching logic based on entity type
107
- case entity_type
108
- when "prefixes"
109
- result = match_prefix_db_to_ucum(db_entity, ucum_entities)
110
- when "units"
111
- result = match_unit_db_to_ucum(db_entity, ucum_entities)
112
- end
113
-
114
- result
115
- end
116
-
117
- # Match UCUM prefix to UnitsDB prefix
118
- def match_prefix_ucum_to_db(ucum_prefix, db_prefixes)
119
- result = { match: nil, potential_match: nil }
120
-
121
- # Try exact name match first
122
- name_match = db_prefixes.find do |db_prefix|
123
- db_prefix.names&.any? do |name_obj|
124
- name_obj.value.downcase == ucum_prefix.name.downcase
125
- end
126
- end
127
-
128
- if name_match
129
- result[:match] = name_match
130
- return result
131
- end
132
-
133
- # Try symbol match
134
- symbol_match = db_prefixes.find do |db_prefix|
135
- db_prefix.symbols&.any? do |symbol|
136
- symbol.ascii == ucum_prefix.print_symbol ||
137
- symbol.unicode == ucum_prefix.print_symbol
138
- end
139
- end
140
-
141
- if symbol_match
142
- result[:match] = symbol_match
143
- return result
144
- end
145
-
146
- # Try value match if available (using base^power)
147
- if ucum_prefix.value&.value
148
- value_match = db_prefixes.find do |db_prefix|
149
- if db_prefix.base && db_prefix.power
150
- calculated_value = db_prefix.base**db_prefix.power
151
- calculated_value.to_s == ucum_prefix.value.value
152
- else
153
- false
154
- end
155
- end
156
-
157
- result[:potential_match] = value_match if value_match
158
- end
159
-
160
- result
161
- end
162
-
163
- # Match UnitsDB prefix to UCUM prefix
164
- def match_prefix_db_to_ucum(db_prefix, ucum_prefixes)
165
- result = { match: nil }
166
-
167
- # Try exact name match first
168
- if db_prefix.names && !db_prefix.names.empty?
169
- db_prefix_names = db_prefix.names.map { |name_obj| name_obj.value.downcase }
170
-
171
- name_match = ucum_prefixes.find do |ucum_prefix|
172
- db_prefix_names.include?(ucum_prefix.name.downcase)
173
- end
174
-
175
- if name_match
176
- result[:match] = name_match
177
- return result
178
- end
179
- end
180
-
181
- # Try symbol match
182
- if db_prefix.symbols && !db_prefix.symbols.empty?
183
- symbol_match = ucum_prefixes.find do |ucum_prefix|
184
- db_prefix.symbols.any? do |symbol|
185
- ucum_prefix.print_symbol == symbol.ascii ||
186
- ucum_prefix.print_symbol == symbol.unicode
187
- end
188
- end
189
-
190
- result[:match] = symbol_match if symbol_match
191
- end
192
-
193
- result
194
- end
195
-
196
- # Match UCUM unit to UnitsDB unit
197
- def match_unit_ucum_to_db(ucum_unit, db_units)
198
- result = { match: nil, potential_match: nil }
199
-
200
- # Get UCUM unit name(s)
201
- ucum_names = case ucum_unit
202
- when Unitsdb::UcumBaseUnit
203
- [ucum_unit.name]
204
- when Unitsdb::UcumUnit
205
- ucum_unit.name.is_a?(Array) ? ucum_unit.name : [ucum_unit.name]
206
- else
207
- []
208
- end
209
-
210
- # Try name match
211
- ucum_names.each do |ucum_name|
212
- name_match = db_units.find do |db_unit|
213
- db_unit.names&.any? do |name_obj|
214
- name_obj.value.downcase == ucum_name.downcase
215
- end
216
- end
217
-
218
- if name_match
219
- result[:match] = name_match
220
- return result
221
- end
222
- end
223
-
224
- # Try symbol match
225
- symbol_match = db_units.find do |db_unit|
226
- db_unit.symbols&.any? do |symbol|
227
- symbol.ascii == ucum_unit.print_symbol ||
228
- symbol.unicode == ucum_unit.print_symbol
229
- end
230
- end
231
-
232
- if symbol_match
233
- result[:match] = symbol_match
234
- return result
235
- end
236
-
237
- # Try property/dimension match for potential matches
238
- property = case ucum_unit
239
- when Unitsdb::UcumBaseUnit
240
- ucum_unit.property
241
- when Unitsdb::UcumUnit
242
- ucum_unit.property
243
- end
244
-
245
- if property
246
- property_matches = db_units.select do |db_unit|
247
- db_unit.quantity_references&.any? do |qref|
248
- qref.id&.downcase&.include?(property.downcase)
249
- end
250
- end
251
-
252
- result[:potential_match] = property_matches.first if property_matches.any?
253
- end
254
-
255
- result
256
- end
257
-
258
- # Match UnitsDB unit to UCUM unit
259
- def match_unit_db_to_ucum(db_unit, ucum_units)
260
- result = { match: nil }
261
-
262
- # Try name match first
263
- if db_unit.names && !db_unit.names.empty?
264
- db_unit_names = db_unit.names.map { |name_obj| name_obj.value.downcase }
265
-
266
- name_match = ucum_units.find do |ucum_unit|
267
- case ucum_unit
268
- when Unitsdb::UcumBaseUnit
269
- db_unit_names.include?(ucum_unit.name.downcase)
270
- when Unitsdb::UcumUnit
271
- ucum_names = ucum_unit.name.is_a?(Array) ? ucum_unit.name : [ucum_unit.name]
272
- ucum_names.any? { |name| db_unit_names.include?(name.downcase) }
273
- else
274
- false
275
- end
276
- end
277
-
278
- if name_match
279
- result[:match] = name_match
280
- return result
281
- end
282
- end
283
-
284
- # Try symbol match
285
- if db_unit.symbols && !db_unit.symbols.empty?
286
- symbol_match = ucum_units.find do |ucum_unit|
287
- db_unit.symbols.any? do |symbol|
288
- ucum_unit.print_symbol == symbol.ascii ||
289
- ucum_unit.print_symbol == symbol.unicode
290
- end
291
- end
292
-
293
- result[:match] = symbol_match if symbol_match
294
- end
295
-
296
- result
297
- end
298
- end
299
- end
300
- end
301
- end
@@ -1,84 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require_relative "../base"
4
- require_relative "../../database"
5
- require_relative "xml_parser"
6
- require_relative "matcher"
7
- require_relative "updater"
8
- require "fileutils"
9
-
10
- module Unitsdb
11
- module Commands
12
- module Ucum
13
- # Command to update UnitsDB with UCUM references
14
- class Update < Base
15
- # Constants
16
- ENTITY_TYPES = %w[units prefixes].freeze
17
-
18
- def run
19
- # Get options
20
- entity_type = @options[:entity_type]&.downcase
21
- database_path = @options[:database]
22
- ucum_file = @options[:ucum_file]
23
- output_dir = @options[:output_dir] || database_path
24
- include_potential = @options[:include_potential_matches] || false
25
-
26
- # Validate database path
27
- unless File.exist?(database_path) && Dir.exist?(database_path)
28
- puts "Database directory path: #{database_path}"
29
- puts "ERROR: Database directory not found: #{database_path}"
30
- return 1
31
- end
32
- puts "Using database directory: #{database_path}"
33
-
34
- # Validate UCUM file
35
- unless File.exist?(ucum_file)
36
- puts "ERROR: UCUM file not found: #{ucum_file}"
37
- return 1
38
- end
39
- puts "Using UCUM file: #{ucum_file}"
40
- puts "Include potential matches: #{include_potential ? "Yes" : "No"}"
41
-
42
- # Parse UCUM XML file
43
- ucum_data = XmlParser.parse_ucum_file(ucum_file)
44
-
45
- # Process entity types
46
- if entity_type && ENTITY_TYPES.include?(entity_type)
47
- process_entity_type(entity_type, ucum_data, output_dir, include_potential)
48
- else
49
- ENTITY_TYPES.each do |type|
50
- process_entity_type(type, ucum_data, output_dir, include_potential)
51
- end
52
- end
53
-
54
- 0
55
- end
56
-
57
- private
58
-
59
- def process_entity_type(entity_type, ucum_data, output_dir, include_potential)
60
- puts "\n========== Processing #{entity_type.upcase} References =========="
61
-
62
- # Get entities
63
- klass = Unitsdb.const_get(entity_type.capitalize)
64
- yaml_path = File.join(@options[:database], "#{entity_type}.yaml")
65
- entity_collection = klass.from_yaml(File.read(yaml_path))
66
-
67
- ucum_entities = XmlParser.get_entities_from_ucum(entity_type, ucum_data)
68
-
69
- return if ucum_entities.nil? || ucum_entities.empty?
70
-
71
- # Match entities
72
- _, missing_refs, = Matcher.match_db_to_ucum(entity_type, ucum_entities, entity_collection)
73
-
74
- # Create output directory if it doesn't exist
75
- FileUtils.mkdir_p(output_dir) unless Dir.exist?(output_dir)
76
-
77
- # Update references in UnitsDB entities
78
- output_file = File.join(output_dir, "#{entity_type}.yaml")
79
- Updater.update_references(entity_type, missing_refs, entity_collection, output_file, include_potential)
80
- end
81
- end
82
- end
83
- end
84
- end
@@ -1,98 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require "yaml"
4
- require "fileutils"
5
-
6
- module Unitsdb
7
- module Commands
8
- module Ucum
9
- # Updater for adding UCUM references to UnitsDB entities
10
- module Updater
11
- UCUM_AUTHORITY = "ucum"
12
-
13
- module_function
14
-
15
- # Update references in UnitsDB entities with UCUM references
16
- def update_references(entity_type, matches, db_entities, output_file, include_potential = false)
17
- puts "Updating UCUM references for #{entity_type}..."
18
-
19
- # Create a map of entity IDs to their UCUM references
20
- entity_references = {}
21
-
22
- # Process each match
23
- matches.each do |match|
24
- db_entity = match[:db_entity]
25
- ucum_entity = match[:ucum_entity]
26
-
27
- # Skip potential matches unless specified
28
- next if match[:potential] && !include_potential
29
-
30
- # Get entity ID
31
- entity_id = get_entity_id(db_entity)
32
- next unless entity_id
33
-
34
- # Initialize references for this entity
35
- entity_references[entity_id] = ExternalReference.new(
36
- uri: ucum_entity.identifier,
37
- type: "informative",
38
- authority: UCUM_AUTHORITY
39
- )
40
- end
41
-
42
- # Update the YAML content
43
- db_entities.send(entity_type).each do |entity|
44
- # Find entity by ID
45
- entity_id = if entity.identifiers
46
- begin
47
- entity.identifiers.first.id
48
- rescue StandardError
49
- nil
50
- end
51
- end
52
-
53
- next unless entity_id && entity_references.key?(entity_id)
54
-
55
- # Initialize references array if it doesn't exist
56
- entity.references ||= []
57
-
58
- # Add new references
59
- if (ext_ref = entity_references[entity_id])
60
- if entity.references.detect { |ref| ref.uri == ext_ref.uri && ref.authority == ext_ref.authority }
61
- # Skip if reference already exists
62
- puts "Reference already exists for entity ID: #{entity_id}"
63
- else
64
- # Add the reference
65
- puts "Adding reference for entity ID: #{entity_id}, URI: #{ext_ref.uri}, Authority: #{ext_ref.authority}"
66
- entity.references << ext_ref
67
- end
68
- end
69
- end
70
-
71
- # Write to YAML file
72
- write_yaml_file(output_file, db_entities)
73
-
74
- puts "Added #{entity_references.values.flatten.size} UCUM references to #{entity_type}"
75
- end
76
-
77
- # Helper to write YAML file
78
- def write_yaml_file(output_file, output_data)
79
- # Ensure the output directory exists
80
- output_dir = File.dirname(output_file)
81
- FileUtils.mkdir_p(output_dir) unless Dir.exist?(output_dir)
82
-
83
- # Write to YAML file
84
- File.write(output_file, output_data.to_yaml)
85
- end
86
-
87
- # Get entity ID (either from identifiers array or directly)
88
- def get_entity_id(entity)
89
- if entity.respond_to?(:identifiers) && entity.identifiers && !entity.identifiers.empty?
90
- entity.identifiers.first.id
91
- elsif entity.respond_to?(:id)
92
- entity.id
93
- end
94
- end
95
- end
96
- end
97
- end
98
- end
@@ -1,34 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require_relative "../../ucum"
4
-
5
- module Unitsdb
6
- module Commands
7
- module Ucum
8
- # Parser for UCUM XML files
9
- module XmlParser
10
- module_function
11
-
12
- # Parse UCUM XML file and return parsed data
13
- def parse_ucum_file(file_path)
14
- puts "Parsing UCUM XML file: #{file_path}..."
15
- content = File.read(file_path)
16
- Unitsdb::UcumFile.from_xml(content)
17
- end
18
-
19
- # Get entities from parsed UCUM data based on entity type
20
- def get_entities_from_ucum(entity_type, ucum_data)
21
- case entity_type
22
- when "prefixes"
23
- ucum_data.prefixes
24
- when "units"
25
- # Combine base-units and units into a single array
26
- ucum_data.base_units + ucum_data.units
27
- else
28
- []
29
- end
30
- end
31
- end
32
- end
33
- end
34
- end
@@ -1,43 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require "thor"
4
-
5
- module Unitsdb
6
- module Commands
7
- class UcumCommand < Thor
8
- desc "check", "Check UCUM references in UnitsDB"
9
- option :entity_type, type: :string, aliases: "-e",
10
- desc: "Entity type to check (units, prefixes). If not specified, all types are checked"
11
- option :ucum_file, type: :string, required: true, aliases: "-u",
12
- desc: "Path to the UCUM essence XML file"
13
- option :output_updated_database, type: :string, aliases: "-o",
14
- desc: "Directory path to write updated YAML files with added UCUM references"
15
- option :direction, type: :string, default: "both", aliases: "-r",
16
- desc: "Direction to check: 'to_ucum' (UnitsDB→UCUM), 'from_ucum' (UCUM→UnitsDB), or 'both'"
17
- option :include_potential_matches, type: :boolean, default: false, aliases: "-p",
18
- desc: "Include potential matches when updating references (default: false)"
19
- option :database, type: :string, required: true, aliases: "-d",
20
- desc: "Path to UnitsDB database (required)"
21
- def check
22
- require_relative "ucum/check"
23
- Ucum::Check.new(options).run
24
- end
25
-
26
- desc "update", "Update UnitsDB with UCUM references"
27
- option :entity_type, type: :string, aliases: "-e",
28
- desc: "Entity type to update (units, prefixes). If not specified, all types are updated"
29
- option :ucum_file, type: :string, required: true, aliases: "-u",
30
- desc: "Path to the UCUM essence XML file"
31
- option :output_dir, type: :string, aliases: "-o",
32
- desc: "Directory path to write updated YAML files (defaults to database path)"
33
- option :include_potential_matches, type: :boolean, default: false, aliases: "-p",
34
- desc: "Include potential matches when updating references (default: false)"
35
- option :database, type: :string, required: true, aliases: "-d",
36
- desc: "Path to UnitsDB database (required)"
37
- def update
38
- require_relative "ucum/update"
39
- Ucum::Update.new(options).run
40
- end
41
- end
42
- end
43
- end