lib/asciimath2unitsml/conv.rb in asciimath2unitsml-0.2.5 vs lib/asciimath2unitsml/conv.rb in asciimath2unitsml-0.3.0
- old
+ new
@@ -8,25 +8,29 @@
require_relative "render"
require_relative "unit"
module Asciimath2UnitsML
MATHML_NS = "http://www.w3.org/1998/Math/MathML".freeze
- UNITSML_NS = "http://unitsml.nist.gov/2005".freeze
+ UNITSML_NS = "https://schema.unitsml.org/unitsml/1.0".freeze
class Conv
def initialize(options = {})
- @dimensions_id = read_yaml("../unitsdb/dimensions.yaml").each_with_object({}) do |(k, v), m|
+ @dimensions_id = read_yaml("../unitsdb/dimensions.yaml").
+ each_with_object({}) do |(k, v), m|
m[k.to_s] = UnitsDB::Dimension.new(k, v)
end
- @prefixes_id = read_yaml("../unitsdb/prefixes.yaml").each_with_object({}) do |(k, v), m|
+ @prefixes_id = read_yaml("../unitsdb/prefixes.yaml").
+ each_with_object({}) do |(k, v), m|
m[k] = UnitsDB::Prefix.new(k, v)
end
@prefixes = flip_name_and_symbol(@prefixes_id)
- @quantities = read_yaml("../unitsdb/quantities.yaml").each_with_object({}) do |(k, v), m|
+ @quantities = read_yaml("../unitsdb/quantities.yaml").
+ each_with_object({}) do |(k, v), m|
m[k.to_s] = UnitsDB::Quantity.new(k, v)
end
- @units_id = read_yaml("../unitsdb/units.yaml").each_with_object({}) do |(k, v), m|
+ @units_id = read_yaml("../unitsdb/units.yaml").
+ each_with_object({}) do |(k, v), m|
m[k.to_s] = UnitsDB::Unit.new(k.to_s, v)
end
@units = flip_name_and_symbols(@units_id)
@symbols = @units.each_with_object({}) do |(k, v), m|
v.symbolids.each { |x| m[x] = v.symbols_hash[x] }
@@ -67,41 +71,48 @@
"m" => { dimension: "Length", order: 1, symbol: "L" },
"g" => { dimension: "Mass", order: 2, symbol: "M" },
"kg" => { dimension: "Mass", order: 2, symbol: "M" },
"s" => { dimension: "Time", order: 3, symbol: "T" },
"A" => { dimension: "ElectricCurrent", order: 4, symbol: "I" },
- "K" => { dimension: "ThermodynamicTemperature", order: 5, symbol: "Theta" },
- "degK" => { dimension: "ThermodynamicTemperature", order: 5, symbol: "Theta" },
+ "K" => { dimension: "ThermodynamicTemperature", order: 5,
+ symbol: "Theta" },
+ "degK" => { dimension: "ThermodynamicTemperature", order: 5,
+ symbol: "Theta" },
"mol" => { dimension: "AmountOfSubstance", order: 6, symbol: "N" },
"cd" => { dimension: "LuminousIntensity", order: 7, symbol: "J" },
"deg" => { dimension: "PlaneAngle", order: 8, symbol: "Phi" },
}.freeze
def units2dimensions(units)
norm = decompose_units(units)
- return if norm.any? { |u| u[:unit] == "unknown" || u[:prefix] == "unknown" || u[:unit].nil? }
+ return if norm.any? do |u|
+ u[:unit] == "unknown" || u[:prefix] == "unknown" || u[:unit].nil?
+ end
norm.map do |u|
{ dimension: U2D[u[:unit]][:dimension],
unit: u[:unit],
exponent: u[:exponent] || 1,
symbol: U2D[u[:unit]][:symbol] }
end.sort { |a, b| U2D[a[:unit]][:order] <=> U2D[b[:unit]][:order] }
end
def dimension1(u)
- %(<#{u[:dimension]} symbol="#{u[:symbol]}" powerNumerator="#{float_to_display(u[:exponent])}"/>)
+ %(<#{u[:dimension]} symbol="#{u[:symbol]}"
+ powerNumerator="#{float_to_display(u[:exponent])}"/>)
end
def dim_id(dims)
return nil if dims.nil? || dims.empty?
dimhash = dims.each_with_object({}) { |h, m| m[h[:dimension]] = h }
dimsvector = %w(Length Mass Time ElectricCurrent ThermodynamicTemperature
AmountOfSubstance LuminousIntensity PlaneAngle)
.map { |h| dimhash.dig(h, :exponent) }.join(":")
- id = @dimensions_id&.values&.select { |d| d.vector == dimsvector }&.first&.id and return id.to_s
+ id = @dimensions_id&.values&.select { |d| d.vector == dimsvector }&.
+ first&.id and return id.to_s
"D_" + dims.map do |d|
- U2D[d[:unit]][:symbol] + (d[:exponent] == 1 ? "" : float_to_display(d[:exponent]))
+ U2D[d[:unit]][:symbol] +
+ (d[:exponent] == 1 ? "" : float_to_display(d[:exponent]))
end.join("")
end
def decompose_units(units)
gather_units(units_only(units).map { |u| decompose_unit(u) }.flatten)
@@ -109,13 +120,16 @@
def gather_units(units)
units.sort { |a, b| a[:unit] <=> b[:unit] }.each_with_object([]) do |k, m|
if m.empty? || m[-1][:unit] != k[:unit] then m << k
else
- m[-1] = { prefix: combine_prefixes(@prefixes[m[-1][:prefix]], @prefixes[k[:prefix]]),
- unit: m[-1][:unit],
- exponent: (k[:exponent]&.to_f || 1) + (m[-1][:exponent]&.to_f || 1) }
+ m[-1] = {
+ prefix: combine_prefixes(
+ @prefixes[m[-1][:prefix]], @prefixes[k[:prefix]]),
+ unit: m[-1][:unit],
+ exponent: (k[:exponent]&.to_f || 1) +
+ (m[-1][:exponent]&.to_f || 1) }
end
end
end
# treat g not kg as base unit: we have stripped the prefix k in parsing
@@ -126,12 +140,14 @@
elsif @units[u[:unit]].system_type == "SI_base" then u
elsif !@units[u[:unit]].si_derived_bases
{ prefix: u[:prefix], unit: "unknown", exponent: u[:exponent] }
else
@units[u[:unit]].si_derived_bases.each_with_object([]) do |k, m|
- m << { prefix: !k[:prefix].nil? && !k[:prefix].empty? ?
- combine_prefixes(@prefixes_id[k[:prefix]], @prefixes[u[:prefix]]) : u[:prefix],
+ prefix = !k[:prefix].nil? && !k[:prefix].empty? ?
+ combine_prefixes(@prefixes_id[k[:prefix]], @prefixes[u[:prefix]]) :
+ u[:prefix]
+ m << { prefix: prefix,
unit: @units_id[k[:id]].symbolid,
exponent: (k[:power]&.to_i || 1) * (u[:exponent]&.to_f || 1) }
end
end
end
@@ -154,12 +170,14 @@
end
ret
end
def quantity(normtext, quantity)
- return unless @units[normtext] && @units[normtext].quantities.size == 1 || @quantities[quantity]
+ return unless @units[normtext] && @units[normtext].quantities.size == 1 ||
+ @quantities[quantity]
id = quantity || @units[normtext].quantities.first
- dim = %( dimensionURL="##{@units[normtext].dimension}") if @units[normtext]&.dimension
+ @units[normtext]&.dimension and
+ dim = %( dimensionURL="##{@units[normtext].dimension}")
<<~END
<Quantity xmlns='#{UNITSML_NS}' xml:id="#{id}"#{dim} quantityType="base">
#{quantityname(id)}
</Quantity>
END