Page MenuHomePhabricator

Module for unit conversions
Open, Needs TriagePublic

Description

Module:Unit (https://en.wikipedia.org/wiki/Module:Convert) is well maintained by @Johnuniq allowing converstions between many types of units. The module is very useful, and widely used in many wikis (~80 wikis). @Amire80 suggested we should consider to move it to its own extension, improving the maintainability of this module along wikis (currently when there is a fix in [[en:Module:Convert]] each wiki should update its module).

  1. First we would like to hear @Johnuniq opinion about the idea
  2. and second we should consider how the localization should be done (currently done through https://en.wikipedia.org/wiki/Module:Convert/documentation/conversion_data/doc used by https://en.wikipedia.org/wiki/Module:Convert/makeunits to generate Lua table )

Event Timeline

There is a bigger Wikidata story called T77978: [Story] Support unit conversion. The team (most notably @Lydia_Pintscher and @daniel) continue to do investigation for this story from time to time. The rough idea is to have unit conversion to SI units baked into the Wikidata RDF exports, as well as an optional way to convert any unit Wikidata can understand from and to SI.

Based on what I currently see in this ticket it might be worth combining both attempts.

Which project should be associated to this task?

Convert is unbelievably complex. When embarking on the process of translating the 4000+ templates used prior to the module (June 2013), I had the programmer's glow of confidence but that wilted on discovering how many quirks convert has. Something might be 12 feet (3.7 m) long, or 12 ft (3.7 m) or 12 feet (3.7 metres) or 12 feet (3.7 meters). Or perhaps you have a 12-foot-long (3.7-metre) something. Then there are a bunch of rounding options and ranges, for example 2 by 3 by 4 inches (50 mm × 75 mm × 100 mm).

Units can optionally be linked, and some are weird:

  • 12 [[Knot (unit)|knots]] (22 km/h; 14 mph)
  • Mach 2.5 (3,062.6 km/h; 1,903.0 mph)
  • 63 inches (15.3 [[Hand (unit)|hands]])
  • 6 feet 1<sup>1</sup>&frasl;<sub>2</sub> inches (186.7 cm)

An overview of the localization process is at transwiki translate. At zhwiki, no space is wanted between a number and the unit, but there should be a space if the unit is expressed in English characters. At slwiki, the output might include 1 meter or 2 metra or 3 metri or 5 metrov (the name changes, depending on the value).

Localization can be applied to units, and more. For example bnwiki text and viwiki text. An important point is that options should always work if given in English because a primary reason for having convert is that people copy articles from enwiki, and any copied converts should work. If wanted, localized options can be provided, but they need to work in addition to any in English.

An extension is an attractive idea but implementation would be more difficult than first appears. An additional complication may be that an extension might impose a centralized style upon individual Wikipedias, or an extension may limit or complicate the ability of local contributors to fix a problem in a way they think suits their situation.

I think the title part "... unit conversions" is incorrect. What is converted is the "physical quantity value". By SI definition:

  • physical quantity = number × unit

By intention and design, this formula may be used algebraically. The complete right-hand side is called "value": number part times unit part. (This is where the title could be improved: "quantity value conversion" ideally, or "value conversion"). For the same reason, proposed name "Module:Unit" is incomplete for this purpose. Maybe "Module:Physicalquantity"?
Notes:
Usually, there are separate but well-defined symbols for both the quantity and for the unit. (speed: ''v'', unit of speed: "m/s"). Omitting this distinction (lefthand, righthand) often is cause for misunderstanding a statement.

When symbols are used, the formula becomes universal (not dependent on language): "m" for meter is never 'translated' (nor transcribed into Cyrillic). So just as it is useful to make the calculations always to SI-(base-)unit first (triangular calculation: "bounce it"), the units should be based on their Wikidata item.

Both sides must have the same dimension (lenght, length/time, ...). The elements may be complicated (log scale, number range like [1, 200⟩ etc.); and specified (speed, max speed, average seed); the unit may be "1" i.e. dimensionless.

I think getting the calculations in the Module is essential, and could be done without local issues. Then, formatting (adjective, spacing, symbol local names) could be/should be superimposed on this somehow, but that never can influence the ''math'' of quantities. HTH.

Continuing @DePiep, you can use the translationwiki for multiple forms in different languages. For example, MediaWiki:Convertunits-length-longform/sl will be something like

$1 {{#switch:$1|1=meter|2=metra|3=metri|#default=metrov}}

Using wikicode and magic worgs as plural, you can change forms, order, remove spaces, and so on. So, you can leave to local users to translate, choosing the most fit form for any measure, long and abbreviated as well.

I'd like to point out that Wikibase has code for unit conversion, along with conversion factors for several thousand units in the config. We currently only use this when exporting to RDF. It wouldn't be very hard to make this functionality available via Lua, though.

Wikidata also has labels and symbols for units, but these are not yet integrated with the conversion system.

In any case, I highly recommend to re-use what we already have.

Continuing @DePiep, you can use the translationwiki for multiple forms in different languages. For example, MediaWiki:Convertunits-length-longform/sl will be something like

$1 {{#switch:$1|1=meter|2=metra|3=metri|#default=metrov}}

Using wikicode and magic worgs as plural, you can change forms, order, remove spaces, and so on. So, you can leave to local users to translate, choosing the most fit form for any measure, long and abbreviated as well.

(I'm new her at Phab. How can I, just simply, *reply* to IKhitron?)

@DePiep You can mention people, and you can quite them. You can't really "reply".

[tech note: I could not add a reply to IKhitron's relpy. That it ended up ~ok is not as predited]. (what the heck. Even in Preview mode, I cannot edit or change or whatever). -~~~~

@DePiep, it's not threaded conversation. Just answer at the end, I'm here.

[reply tech flow; no content]: OK, I understand by now (sort of). New here. Thankx.

I'm skeptical about Wikidata considering that it can't get foot right. ( See https://www.wikidata.org/wiki/Q3710 )

... Wikibase has code for unit conversion, along with conversion factors for several thousand units in the config. ...

Wikidata also has labels and symbols for units, but these are not yet integrated with the conversion system.

Basically, per unit only *one* conversion factor is required: "1 unit of this × factor = y unit in SI basic units". Complicated scales like temperature (0-scaled) and dB (logarithmic) need a formula, not just a factor. ft,in-like systems (non-decimal, and/or multiple units in one value) add other complications -- don't compromise re SI I'd say.

Note that it is SI base units (m not km). @Johnuniq 's Convert module nicely uses this value-in-SI-base-units: it is an excellend sorting number (tablesort; order).

I am puzzled by the "symbols .... not yet integrated" remark: how are units defined/identified then? Not by QID?

You can have a measure along some physical dimension, given in meter or foot, which would then nicely be split in a value and an unit. Conversion from meter to foot would then follow simple conversion factors. This is complicated when you have units that isn't well-defined, and can't easily be converted. In a lot of cases you can convert them with increased uncertainty, and in a few cases you cant convert them at all because you don't know enough about the historical units.

This make unit conversion quite difficult. The comment in T190813#4092798 visualizes the problem quite well. You can't simply convert “foot”, you must know the date of the measurement and the country, and sometimes even the city. Unless I'm mistaken the module Convert use one single foot for all conversions. What about the 273.8 mm voet in Ieper, Belgium, and the 347.73 mm piede in Venice, Italy?

For some length units there could be several in use over long periods, like the “rast” (“coffestop”), “vei” (“road”), and “mil” in Scandinavia. In Norway and Sweden a “mil” is now 10 km, but in Denmark it is 7421.43 m. “Rast” is still in use as “coffestops”. Weird…

Some derived units has specific names, and some of those names has slightly different meaning in different countries. A common one is “resistance” Ω – ohm vs “conductance” S – Siemens. Some derived units in the SI system even has non-SI parts. Anyone using Dalton? If you calculate with numbers that has units, then the units should come out with the correct name. That is slightly complex. To be able to recognize the name you must normalize the derived unit.

You must also consider use of prefix for the unit, which is not always obvious. For example, you will write 4700 µF, not 4.7 mF. Another example is 1 kg, which is pretty obvious, you don't write 1000 g. Usually you will chose prefix as to minimize the representation of the number, but for some named units the obvious choice is wrong.

Lastly, some units are redefined over the years. Even the SI units are updated and refined. I like how Wikibase points to an item with the definition, it make things slightly easier.