简体   繁体   English

用于Java的简单物理量测量单元解析器

[英]Simple physical quantity measurement unit parser for Java

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? 是否可以使用像Pyparsing这样的东西(如果Java存在这样的东西),或者我应该使用更复杂的工具,如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 编辑:回答MrD的问题,目标是在数量之间进行转换,例如将g转换为kg(这个很简单......),或者可能是°F /(kg * s ^ 2)到K /(lb * h ^ 2)假设h为4小时,lb为磅

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). 主要的问题是没有标准(我已经在NIST上使用了单位,尽管他们最终创建了一种很少有人使用它的标记语言)。 So it's really a form of natural language processing and has to deal with : 所以它真的是一种自然语言处理形式,必须处理:

  • ambiguity (what does "M" mean - meters or mega) 歧义(“M”是什么意思 - 米或兆)
  • inconsistent punctuation 不一致的标点符号
  • abbreviations 缩写
  • symbols (eg "mu" for micro) 符号(例如“mu”表示微观)
  • unclear semantics (eg is kg/m/s the same as kg/(m*s)? 不明确的语义(例如kg / m / s与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. 如果您只是创建一个玩具系统,那么您应该为系统创建一个BNF,并确保所有示例都遵循它。 This will use common punctuation ("/", " ", "(", ")", "^"). 这将使用常见的标点符号(“/”,“ ”,“(”,“)”,“^”)。 Character fields can be of variable length ("m", "kg", "lb"). 字符字段可以是可变长度(“m”,“kg”,“lb”)。 Algebra on these strings ("kg" -> 1000 "g" has problems as kg is a fundamental unit. 这些弦上的代数(“kg” - > 1000 “g”存在问题,因为kg是基本单位。

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. 如果你认真对待它,那么ANTLR(@Yaugen)很有用,但请注意,由于上面的不一致,野外单位不会遵循常规语法。

If you are REALLY serious (ie 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. 我目前的方法(超出了你的问题的范围)是自动从文献中收集大量的例子并创建一些启发式方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM