Module: ParseUtil

Defined in:
lib/oddb2xml/parse_compositions.rb

Overview

This file is shared since oddb2xml 2.0.0 (lib/oddb2xml/parse_compositions.rb) with oddb.org src/plugin/parse_compositions.rb

It allows an easy parsing of the column P Zusammensetzung of the swissmedic packages.xlsx file

Defined Under Namespace

Classes: ParseComposition, ParseSubstance

Constant Summary collapse

SCALE_P =
%r{pro\s+(?<scale>(?<qty>[\d.,]+)\s*(?<unit>[kcmuµn]?[glh]))}u

Class Method Summary collapse

Class Method Details

.capitalize(string) ⇒ Object



13
14
15
# File 'lib/oddb2xml/parse_compositions.rb', line 13

def ParseUtil.capitalize(string)
  string.split(/\s+/u).collect { |word| word.capitalize }.join(' ')
end

.parse_compositions(composition) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/oddb2xml/parse_compositions.rb', line 17

def ParseUtil.parse_compositions(composition)
  rep_1 = '----';   to_1 = '('
  rep_2 = '-----';  to_2 = ')'
  rep_3 = '------'; to_3 = ','

  comps = []
  label_pattern = /^(?<label>A|I|B|II|C|III|D|IV|E|V|F|VI)[)]\s*(?<description>[^)]+):/
  composition_text = composition.gsub(/\r\n?/u, "\n")
  puts "composition_text for #{name}: #{composition_text}" if composition_text.split(/\n/u).size > 1 and $VERBOSE
  lines = composition_text.split(/\n/u)
  idx = 0
  compositions = lines.select do |line|
    if match = label_pattern.match(line)
      label = match[:label]
      label_description = match[:description]
    else
      label = nil
      label_description = nil
    end
    idx += 1
    next if idx > 1 and not label # avoid lines like 'I) et II)'
    substances = []
    filler = line.split(',')[-1].sub(/\.$/, '')
    filler_match = /^(?<name>[^,\d]+)\s*(?<dose>[\d\-.]+(\s*(?:(Mio\.?\s*)?(U\.\s*Ph\.\s*Eur\.|[^\s,]+))))/.match(filler)
    components = line.split(/([^\(]+\([^)]+\)[^,]+|),/).each {
      |component|
      next unless component.size > 0
      to_consider = component.strip.split(':')[-1].gsub(to_1, rep_1).gsub(to_2, rep_2).gsub(to_3, rep_3) # remove label
      # very ugly hack to ignore ,()
      ptrn1 = /^(?<name>.+)\s+(?<dose>[\d\-.]+(\s*(?:(Mio\.?\s*)?(U\.\s*Ph\.\s*Eur\.|[^\s,]+))))/
      m = ptrn1.match(to_consider)
      if m2 = /^(|[^:]+:\s)(E\s+\d+)$/.match(component.strip)
        to_add = ParseSubstance.new(m2[2], '', '')
        substances << to_add
      elsif m
        ptrn = /(\s*(?:ut|corresp\.?)\s+(?<chemical>[^\d,]+)\s*(?<cdose>[\d\-.]+(\s*(?:(Mio\.?\s*)?(U\.\s*Ph\.\s*Eur\.|[^\s,]+))(\s*[mv]\/[mv])?))?)/
        m3 = ptrn.match(to_consider)
        dose = nil
        unit = nil
        name = m[:name].split(/\s/).collect{ |x| x.capitalize }.join(' ').strip.gsub(rep_3, to_3).gsub(rep_2, to_2).gsub(rep_1, to_1)
        dose = m[:dose].split(/\b\s*(?![.,\d\-]|Mio\.?)/u, 2) if m[:dose]
        if dose && (scale = SCALE_P.match(filler)) && dose[1] && !dose[1].include?('/')
          unit = dose[1] << '/'
          num = scale[:qty].to_f
          if num <= 1
            unit << scale[:unit]
          else
            unit << scale[:scale]
          end
        elsif dose.size == 2
          unit = dose[1]
        end
        next if /\s+pro($|\s+)|emulsion|solution/i.match(name)
        chemical = m3 ? capitalize(m3[:chemical]) : nil
        cdose    = m3 ? m3[:cdose] : nil
        substances << ParseSubstance.new(name, dose ? dose[0].to_f : nil, unit ? unit.gsub(rep_3, to_3).gsub(rep_2, to_2).gsub(rep_1, to_1) : nil,
                                    chemical, cdose)
      end
    }
    comps << ParseComposition.new(line, label, label_description, substances) if substances.size > 0
  end
  comps
end