<< | Index | >>
Task
module ODDB
module SizeParser
unit_pattern = '(([kmµucMG]?([glLJm]|mol|Bq)\b)(\/([mµu]?[glL])\b)?)|((Mio\s)?U\.?I\.?)|(%( [mV]\/[mV])?)|(I\.E\.)|(Fl\.)'
numeric_pattern = '\d+(\'\d+)*([.,]\d+)?'
iso_pattern = "[[:alpha:]()\-]+"
@@parser = Parse.generate_parser <<-EOG
Grammar OddbSize
Tokens
DESCRIPTION = /(?!#{unit_pattern}\s)#{iso_pattern}(\s+#{iso_pattern})*/u
NUMERIC = /#{numeric_pattern}/u
SPACE = /\s+/u [:Skip]
UNIT = /#{unit_pattern}/u
Productions
Size -> Multiple* Addition? Count? Measure? Scale? Dose? DESCRIPTION?
Count -> 'je'? NUMERIC
Multiple -> NUMERIC UNIT? /[xXà]|Set/u
Measure -> NUMERIC UNIT UNIT?
Addition -> NUMERIC UNIT? '+'
Scale -> '/' NUMERIC? UNIT
Dose -> '(' NUMERIC UNIT ')'
EOG
def size=(size)
unless size.to_s.strip.empty?
@addition, @multi, @count, @measure, @scale, @comform = parse_size(size)
...
Note
Note (Pattern samples)
mg kg ml mg mol Bg 'Mio U.I.'
123 123'456 123.4
abc abc(def) abc-def
Note
/bc|ab/ =~ "abc" => $& == 'ab' (not 'bc') left-most match /\w+/ =~ "abc" => $& == 'abc' (not 'a') longest match /a|\w+/ =~ "abc" => $& == 'a' (not 'abc') left-most > longest
Note
Reference
Experiment
Result
"9 Suppositorien" : [[], nil, [nil, "9"], nil, nil, nil, "Suppositorien"] "10 " : [[], nil, [nil, "10"], nil, nil, nil, nil] "200 ml" : [[], nil, nil, ["200", "ml", nil], nil, nil, nil] "10x200 ml" : [["10", nil, "x"], nil, nil, ["200", "ml", nil], nil, nil, nil]
Result
"9 Suppositorien" : [[], nil, [nil, "9"], nil, nil, nil, "Suppositorien"] "10 " : [[], nil, [nil, "10"], nil, nil, nil, nil] "200 ml" : [[], nil, nil, ["200", "ml", nil], nil, nil, nil] "10x200 ml" : [["10", nil, "x"], nil, nil, ["200", "ml", nil], nil, nil, nil]
Note
Experiment
Run
$ ruby test_rockit2.rb > test_rockit.dat $ ruby test_parser2.rb > test_parser.dat
Diff (diff -y --suppress-common-lines -W 200 test_rockit.dat test_parser.dat)
Note
Experiment
Run
$ ruby test_rockit3.rb > test_rockit.dat $ ruby test_parser3.rb > test_parser.dat
Diff (diff -y --suppress-common-lines -W 200 test_rockit.dat test_parser.dat)