Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Simple physical quantity measurement unit parser for Java

Tags:

java

parsing

I want to be able to parse expressions representing physical quantities like

g/l
m/s^2
m/s/kg
m/(s*kg)
kg*m*s
°F/(lb*s^2)

and so on. In the simplest way possible. Is it possible to do so using something like Pyparsing (if such a thing exists for Java), or should I use more complex tools like Java CUP?

EDIT: To answere MrD's question the goal is to make conversion between quantities, so for example convert g to kg (this one is simple...), or maybe °F/(kg*s^2) to K/(lb*h^2) supposing h is four hour and lb for pounds

like image 838
Paolo Avatar asked May 15 '13 12:05

Paolo


1 Answers

This is harder than it looks. (I have done a fair amount of work here). The main problem is there is no standard (I have worked with NIST on units and although they have finally created a markup language few people use it). So it's really a form of natural language processing and has to deal with :

  • ambiguity (what does "M" mean - meters or mega)
  • inconsistent punctuation
  • abbreviations
  • symbols (e.g. "mu" for micro)
  • unclear semantics (e.g. is kg/m/s the same as kg/(m*s)?

If you are just creating a toy system then you should create a BNF for the system and make sure that all examples adhere to it. This will use common punctuation ("/", "", "(", ")", "^"). Character fields can be of variable length ("m", "kg", "lb"). Algebra on these strings ("kg" -> 1000"g" has problems as kg is a fundamental unit.

If you are doing it seriously then ANTLR (@Yaugen) is useful, but be aware that units in the wild will not follow a regular grammar due to the inconsistencies above.

If you are REALLY serious (i.e. prepared to put in a solid month), I'd be interested to know. :-)

My current approach (which is outside the scope of your question) is to collect a large number of examples from the literature automatically and create a number of heuristics.

like image 177
peter.murray.rust Avatar answered Nov 18 '22 02:11

peter.murray.rust