unitsdb 0.1.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.
Files changed (67) hide show
  1. checksums.yaml +4 -4
  2. data/.github/workflows/dependent-repos.json +5 -0
  3. data/.github/workflows/depenedent-gems.yml +16 -0
  4. data/.gitmodules +3 -0
  5. data/.rspec +2 -1
  6. data/.rubocop_todo.yml +88 -12
  7. data/Gemfile +2 -2
  8. data/README.adoc +697 -1
  9. data/exe/unitsdb +7 -0
  10. data/lib/unitsdb/cli.rb +81 -0
  11. data/lib/unitsdb/commands/_modify.rb +22 -0
  12. data/lib/unitsdb/commands/base.rb +52 -0
  13. data/lib/unitsdb/commands/check_si.rb +124 -0
  14. data/lib/unitsdb/commands/get.rb +133 -0
  15. data/lib/unitsdb/commands/normalize.rb +81 -0
  16. data/lib/unitsdb/commands/release.rb +112 -0
  17. data/lib/unitsdb/commands/search.rb +219 -0
  18. data/lib/unitsdb/commands/si_formatter.rb +485 -0
  19. data/lib/unitsdb/commands/si_matcher.rb +470 -0
  20. data/lib/unitsdb/commands/si_ttl_parser.rb +100 -0
  21. data/lib/unitsdb/commands/si_updater.rb +212 -0
  22. data/lib/unitsdb/commands/validate/identifiers.rb +40 -0
  23. data/lib/unitsdb/commands/validate/references.rb +316 -0
  24. data/lib/unitsdb/commands/validate/si_references.rb +115 -0
  25. data/lib/unitsdb/commands/validate.rb +40 -0
  26. data/lib/unitsdb/config.rb +19 -0
  27. data/lib/unitsdb/database.rb +661 -0
  28. data/lib/unitsdb/dimension.rb +19 -25
  29. data/lib/unitsdb/dimension_details.rb +20 -0
  30. data/lib/unitsdb/dimension_reference.rb +8 -0
  31. data/lib/unitsdb/dimensions.rb +3 -6
  32. data/lib/unitsdb/errors.rb +13 -0
  33. data/lib/unitsdb/external_reference.rb +14 -0
  34. data/lib/unitsdb/identifier.rb +8 -0
  35. data/lib/unitsdb/localized_string.rb +17 -0
  36. data/lib/unitsdb/prefix.rb +11 -12
  37. data/lib/unitsdb/prefix_reference.rb +10 -0
  38. data/lib/unitsdb/prefixes.rb +3 -6
  39. data/lib/unitsdb/quantities.rb +3 -27
  40. data/lib/unitsdb/quantity.rb +12 -24
  41. data/lib/unitsdb/quantity_reference.rb +4 -7
  42. data/lib/unitsdb/root_unit_reference.rb +14 -0
  43. data/lib/unitsdb/scale.rb +17 -0
  44. data/lib/unitsdb/scale_properties.rb +12 -0
  45. data/lib/unitsdb/scale_reference.rb +10 -0
  46. data/lib/unitsdb/scales.rb +11 -0
  47. data/lib/unitsdb/si_derived_base.rb +13 -14
  48. data/lib/unitsdb/symbol_presentations.rb +14 -0
  49. data/lib/unitsdb/unit.rb +20 -26
  50. data/lib/unitsdb/unit_reference.rb +5 -8
  51. data/lib/unitsdb/unit_system.rb +8 -10
  52. data/lib/unitsdb/unit_system_reference.rb +10 -0
  53. data/lib/unitsdb/unit_systems.rb +3 -16
  54. data/lib/unitsdb/units.rb +3 -6
  55. data/lib/unitsdb/utils.rb +84 -0
  56. data/lib/unitsdb/version.rb +1 -1
  57. data/lib/unitsdb.rb +13 -10
  58. data/unitsdb.gemspec +6 -1
  59. metadata +112 -12
  60. data/lib/unitsdb/dimension_quantity.rb +0 -28
  61. data/lib/unitsdb/dimension_symbol.rb +0 -22
  62. data/lib/unitsdb/prefix_symbol.rb +0 -12
  63. data/lib/unitsdb/root_unit.rb +0 -17
  64. data/lib/unitsdb/root_units.rb +0 -20
  65. data/lib/unitsdb/symbol.rb +0 -17
  66. data/lib/unitsdb/unit_symbol.rb +0 -15
  67. data/lib/unitsdb/unitsdb.rb +0 -6
@@ -0,0 +1,84 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Unitsdb
4
+ module Utils
5
+ DEFAULT_YAML_FILES = %w[dimensions prefixes quantities unit_systems units].map { |f| "#{f}.yaml" }
6
+
7
+ def self.levenshtein_distance(s, t)
8
+ m = s.length
9
+ n = t.length
10
+
11
+ return m if n.zero?
12
+ return n if m.zero?
13
+
14
+ d = Array.new(m + 1) { Array.new(n + 1) }
15
+
16
+ (0..m).each { |i| d[i][0] = i }
17
+ (0..n).each { |j| d[0][j] = j }
18
+
19
+ (1..n).each do |j|
20
+ (1..m).each do |i|
21
+ d[i][j] = if s[i - 1] == t[j - 1]
22
+ d[i - 1][j - 1]
23
+ else
24
+ [d[i - 1][j] + 1, d[i][j - 1] + 1, d[i - 1][j - 1] + 1].min
25
+ end
26
+ end
27
+ end
28
+
29
+ d[m][n]
30
+ end
31
+
32
+ def self.find_similar_ids(id, valid_ids, max_results = 5)
33
+ # Simple similarity measure based on Levenshtein distance
34
+ valid_ids
35
+ .map { |valid_id| [valid_id, levenshtein_distance(id.to_s, valid_id.to_s)] }
36
+ .sort_by { |_, distance| distance }
37
+ .take(max_results)
38
+ .select { |_, distance| distance < [id.to_s.length, 10].min }
39
+ .map { |valid_id, _| valid_id }
40
+ end
41
+
42
+ def self.sort_yaml_keys(obj)
43
+ case obj
44
+ when Hash
45
+ # Transform values first, then sort keys
46
+ obj.transform_values { |v| sort_yaml_keys(v) }
47
+ .sort_by do |key, _|
48
+ key_str = key.to_s
49
+
50
+ # Handle NIST IDs with proper category-based sorting
51
+ case key_str
52
+ when /\ANIST([a-z])(\d+)_(-?\d+)\Z/ # Prefixes with power (NISTp2_10)
53
+ [0, ::Regexp.last_match(1), ::Regexp.last_match(2).to_i, ::Regexp.last_match(3).to_i]
54
+ when /\ANIST([a-z])(\d+)\Z/ # Prefixes without power
55
+ [0, ::Regexp.last_match(1), ::Regexp.last_match(2).to_i, 0]
56
+ when %r{[/e]} # Composite IDs with '/' or 'e' to the end
57
+ [2, key_str]
58
+ when /\ANISTu(\d+)\.(.+)\Z/ # Simple unit IDs (NISTu10.1)
59
+ [1, "u", ::Regexp.last_match(1).to_i, ::Regexp.last_match(2)]
60
+ else
61
+ [3, key_str] # Everything else
62
+ end
63
+ end.to_h
64
+ when Array
65
+ # For arrays in the new structure, sort them if they're arrays of hashes with 'id' or 'short' keys
66
+ if !obj.empty? && obj.all? { |item| item.is_a?(Hash) }
67
+ # Sort by 'id' within identifiers array
68
+ if obj.first.key?("id") && obj.first.key?("type")
69
+ obj.sort_by { |item| item["id"].to_s }.map { |item| sort_yaml_keys(item) }
70
+ # Sort by 'short' for other arrays (like main unit arrays)
71
+ elsif obj.first.key?("short")
72
+ obj.sort_by { |item| item["short"].to_s }.map { |item| sort_yaml_keys(item) }
73
+ else
74
+ obj.map { |item| sort_yaml_keys(item) }
75
+ end
76
+ else
77
+ obj.map { |item| sort_yaml_keys(item) }
78
+ end
79
+ else
80
+ obj
81
+ end
82
+ end
83
+ end
84
+ end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Unitsdb
4
- VERSION = "0.1.1"
4
+ VERSION = "2.1.0"
5
5
  end
data/lib/unitsdb.rb CHANGED
@@ -2,17 +2,20 @@
2
2
 
3
3
  require "lutaml/model"
4
4
 
5
- # Lutaml::Model::Config.configure do |config|
6
- # require "lutaml/model/xml_adapter/nokogiri_adapter"
7
- # config.xml_adapter = Lutaml::Model::XmlAdapter::NokogiriAdapter
8
- # end
9
-
10
- module Unitsdb
11
- class Error < StandardError; end
12
- end
13
-
14
5
  require_relative "unitsdb/version"
15
- require_relative "unitsdb/units"
6
+ require_relative "unitsdb/config"
7
+ require_relative "unitsdb/errors"
8
+ require_relative "unitsdb/database"
16
9
  require_relative "unitsdb/dimensions"
17
10
  require_relative "unitsdb/prefixes"
18
11
  require_relative "unitsdb/quantities"
12
+ require_relative "unitsdb/unit_systems"
13
+ require_relative "unitsdb/scales"
14
+ require_relative "unitsdb/units"
15
+ require_relative "unitsdb/utils"
16
+
17
+ # CLI-related requires
18
+ require_relative "unitsdb/cli" if defined?(Thor)
19
+
20
+ module Unitsdb
21
+ end
data/unitsdb.gemspec CHANGED
@@ -33,5 +33,10 @@ Gem::Specification.new do |spec|
33
33
  spec.test_files = `git ls-files -- {spec}/*`.split("\n")
34
34
  spec.require_paths = ["lib"]
35
35
 
36
- spec.add_dependency "lutaml-model"
36
+ spec.add_dependency "lutaml-model", "~>0.7"
37
+ spec.add_dependency "rdf", "~> 3.1"
38
+ spec.add_dependency "rdf-turtle", "~> 3.1"
39
+ spec.add_dependency "rubyzip", "~> 2.3"
40
+ spec.add_dependency "terminal-table"
41
+ spec.add_dependency "thor", "~> 1.0"
37
42
  end
metadata CHANGED
@@ -1,17 +1,73 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: unitsdb
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 2.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ribose Inc.
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2025-01-11 00:00:00.000000000 Z
11
+ date: 2025-05-20 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: lutaml-model
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '0.7'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '0.7'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rdf
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '3.1'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '3.1'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rdf-turtle
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '3.1'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '3.1'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rubyzip
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '2.3'
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '2.3'
69
+ - !ruby/object:Gem::Dependency
70
+ name: terminal-table
15
71
  requirement: !ruby/object:Gem::Requirement
16
72
  requirements:
17
73
  - - ">="
@@ -24,16 +80,34 @@ dependencies:
24
80
  - - ">="
25
81
  - !ruby/object:Gem::Version
26
82
  version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: thor
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: '1.0'
90
+ type: :runtime
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: '1.0'
27
97
  description: Library to handle UnitsDB content.
28
98
  email:
29
99
  - open.source@ribose.com
30
- executables: []
100
+ executables:
101
+ - unitsdb
31
102
  extensions: []
32
103
  extra_rdoc_files: []
33
104
  files:
105
+ - ".github/workflows/dependent-repos.json"
106
+ - ".github/workflows/depenedent-gems.yml"
34
107
  - ".github/workflows/rake.yml"
35
108
  - ".github/workflows/release.yml"
36
109
  - ".gitignore"
110
+ - ".gitmodules"
37
111
  - ".rspec"
38
112
  - ".rubocop.yml"
39
113
  - ".rubocop_todo.yml"
@@ -44,28 +118,54 @@ files:
44
118
  - Rakefile
45
119
  - bin/console
46
120
  - bin/setup
121
+ - exe/unitsdb
47
122
  - lib/unitsdb.rb
123
+ - lib/unitsdb/cli.rb
124
+ - lib/unitsdb/commands/_modify.rb
125
+ - lib/unitsdb/commands/base.rb
126
+ - lib/unitsdb/commands/check_si.rb
127
+ - lib/unitsdb/commands/get.rb
128
+ - lib/unitsdb/commands/normalize.rb
129
+ - lib/unitsdb/commands/release.rb
130
+ - lib/unitsdb/commands/search.rb
131
+ - lib/unitsdb/commands/si_formatter.rb
132
+ - lib/unitsdb/commands/si_matcher.rb
133
+ - lib/unitsdb/commands/si_ttl_parser.rb
134
+ - lib/unitsdb/commands/si_updater.rb
135
+ - lib/unitsdb/commands/validate.rb
136
+ - lib/unitsdb/commands/validate/identifiers.rb
137
+ - lib/unitsdb/commands/validate/references.rb
138
+ - lib/unitsdb/commands/validate/si_references.rb
139
+ - lib/unitsdb/config.rb
140
+ - lib/unitsdb/database.rb
48
141
  - lib/unitsdb/dimension.rb
49
- - lib/unitsdb/dimension_quantity.rb
50
- - lib/unitsdb/dimension_symbol.rb
142
+ - lib/unitsdb/dimension_details.rb
143
+ - lib/unitsdb/dimension_reference.rb
51
144
  - lib/unitsdb/dimensions.rb
145
+ - lib/unitsdb/errors.rb
146
+ - lib/unitsdb/external_reference.rb
147
+ - lib/unitsdb/identifier.rb
148
+ - lib/unitsdb/localized_string.rb
52
149
  - lib/unitsdb/prefix.rb
53
- - lib/unitsdb/prefix_symbol.rb
150
+ - lib/unitsdb/prefix_reference.rb
54
151
  - lib/unitsdb/prefixes.rb
55
152
  - lib/unitsdb/quantities.rb
56
153
  - lib/unitsdb/quantity.rb
57
154
  - lib/unitsdb/quantity_reference.rb
58
- - lib/unitsdb/root_unit.rb
59
- - lib/unitsdb/root_units.rb
155
+ - lib/unitsdb/root_unit_reference.rb
156
+ - lib/unitsdb/scale.rb
157
+ - lib/unitsdb/scale_properties.rb
158
+ - lib/unitsdb/scale_reference.rb
159
+ - lib/unitsdb/scales.rb
60
160
  - lib/unitsdb/si_derived_base.rb
61
- - lib/unitsdb/symbol.rb
161
+ - lib/unitsdb/symbol_presentations.rb
62
162
  - lib/unitsdb/unit.rb
63
163
  - lib/unitsdb/unit_reference.rb
64
- - lib/unitsdb/unit_symbol.rb
65
164
  - lib/unitsdb/unit_system.rb
165
+ - lib/unitsdb/unit_system_reference.rb
66
166
  - lib/unitsdb/unit_systems.rb
67
167
  - lib/unitsdb/units.rb
68
- - lib/unitsdb/unitsdb.rb
168
+ - lib/unitsdb/utils.rb
69
169
  - lib/unitsdb/version.rb
70
170
  - sig/unitsdb.rbs
71
171
  - unitsdb.gemspec
@@ -91,7 +191,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
91
191
  - !ruby/object:Gem::Version
92
192
  version: '0'
93
193
  requirements: []
94
- rubygems_version: 3.3.27
194
+ rubygems_version: 3.5.22
95
195
  signing_key:
96
196
  specification_version: 4
97
197
  summary: Ruby library for UnitsDB
@@ -1,28 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require_relative "dimension_symbol"
4
- # NISTd1:
5
- # length:
6
- # powerNumerator: 1
7
- # symbol: L
8
- # dim_symbols:
9
- # - id: "dim_L"
10
- # ascii: "L"
11
- # html: "&#x1D5AB;"
12
- # mathml: "<mi mathvariant='sans-serif'>L</mi>"
13
- # latex: \ensuremath{\mathsf{L}}
14
- # unicode: "𝖫"
15
-
16
- module Unitsdb
17
- class DimensionQuantity < Lutaml::Model::Serializable
18
- attribute :power_numerator, :integer
19
- attribute :symbol, :string
20
- attribute :dim_symbols, DimensionSymbol, collection: true
21
-
22
- key_value do
23
- map :powerNumerator, to: :power_numerator
24
- map :symbol, to: :symbol
25
- map :dim_symbols, to: :dim_symbols
26
- end
27
- end
28
- end
@@ -1,22 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require_relative "symbol"
4
-
5
- # - id: "dim_L"
6
- # ascii: "L"
7
- # html: "&#x1D5AB;"
8
- # mathml: "<mi mathvariant='sans-serif'>L</mi>"
9
- # latex: \ensuremath{\mathsf{L}}
10
- # unicode: "𝖫"
11
-
12
- module Unitsdb
13
- class DimensionSymbol < Symbol
14
- attribute :id, :string
15
- attribute :mathml, :string
16
-
17
- key_value do
18
- map :id, to: :id
19
- map :mathml, to: :mathml
20
- end
21
- end
22
- end
@@ -1,12 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- # symbol:
4
- # ascii: R
5
- # html: R
6
- # latex: R
7
- # unicode: R
8
-
9
- module Unitsdb
10
- class PrefixSymbol < Symbol
11
- end
12
- end
@@ -1,17 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module Unitsdb
4
- class RootUnit < Lutaml::Model::Serializable
5
- attribute :unit, :string
6
- attribute :power_denominator, :integer
7
- attribute :power_numerator, :integer
8
- attribute :prefix, :string
9
-
10
- key_value do
11
- map :unit, to: :unit
12
- map :power_denominator, to: :power_denominator
13
- map :power_numerator, to: :power_numerator
14
- map :prefix, to: :prefix
15
- end
16
- end
17
- end
@@ -1,20 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require_relative "root_unit"
4
-
5
- module Unitsdb
6
- class RootUnits < Lutaml::Model::Serializable
7
- attribute :unit, :string
8
- attribute :power_denominator, :integer
9
- attribute :power_numerator, :integer
10
- attribute :enumerated_root_units, RootUnit, collection: true
11
-
12
- key_value do
13
- map :unit, to: :unit
14
- map :power_denominator, to: :power_denominator
15
- map :power_numerator, to: :power_numerator
16
- map :enumerated_root_units,
17
- to: :enumerated_root_units
18
- end
19
- end
20
- end
@@ -1,17 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module Unitsdb
4
- class Symbol < Lutaml::Model::Serializable
5
- attribute :ascii, :string
6
- attribute :html, :string
7
- attribute :latex, :string
8
- attribute :unicode, :string
9
-
10
- key_value do
11
- map :ascii, to: :ascii
12
- map :html, to: :html
13
- map :latex, to: :latex
14
- map :unicode, to: :unicode
15
- end
16
- end
17
- end
@@ -1,15 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require_relative "symbol"
4
-
5
- module Unitsdb
6
- class UnitSymbol < Symbol
7
- attribute :id, :string
8
- attribute :mathml, :string
9
-
10
- key_value do
11
- map :id, to: :id
12
- map :mathml, to: :mathml
13
- end
14
- end
15
- end
@@ -1,6 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module Unitsdb
4
- class Error < StandardError; end
5
- # Your code goes here...
6
- end