Class: ParseComposition

Inherits:
Object
  • Object
show all
Defined in:
lib/oddb2xml/parslet_compositions.rb

Constant Summary collapse

ErrorsToFix =
{ /(sulfuris D6\s[^\s]+\smg)\s([^,]+)/ => '\1, \2',
  /(\d+)\s+\-\s*(\d+)/ => '\1-\2',
  'o.1' => '0.1',
  'g DER:' => 'g, DER:',
  /(excipiens ad solutionem pro \d+ ml), corresp\./ => '\1 corresp.',
  /^(pollinis allergeni extractum[^\:]+\:)/ => 'A): \1',
  /^(acari allergeni extractum 5000 U\.\:)/ => 'A): \1',
}
@@errorHandler =
ParseUtil::HandleSwissmedicErrors.new( ErrorsToFix )

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(source) ⇒ ParseComposition

Returns a new instance of ParseComposition.



524
525
526
527
528
# File 'lib/oddb2xml/parslet_compositions.rb', line 524

def initialize(source)
  @substances ||= []
  puts "ParseComposition.new from #{source.inspect} @substances #{@substances.inspect}" if VERBOSE_MESSAGES
  @source = source.to_s
end

Instance Attribute Details

#correspObject

Returns the value of attribute corresp.



511
512
513
# File 'lib/oddb2xml/parslet_compositions.rb', line 511

def corresp
  @corresp
end

#galenic_formObject

Returns the value of attribute galenic_form.



511
512
513
# File 'lib/oddb2xml/parslet_compositions.rb', line 511

def galenic_form
  @galenic_form
end

#labelObject

Returns the value of attribute label.



511
512
513
# File 'lib/oddb2xml/parslet_compositions.rb', line 511

def label
  @label
end

#label_descriptionObject

Returns the value of attribute label_description.



511
512
513
# File 'lib/oddb2xml/parslet_compositions.rb', line 511

def label_description
  @label_description
end

#route_of_administrationObject

Returns the value of attribute route_of_administration.



511
512
513
# File 'lib/oddb2xml/parslet_compositions.rb', line 511

def route_of_administration
  @route_of_administration
end

#sourceObject

Returns the value of attribute source.



511
512
513
# File 'lib/oddb2xml/parslet_compositions.rb', line 511

def source
  @source
end

#substancesObject

Returns the value of attribute substances.



511
512
513
# File 'lib/oddb2xml/parslet_compositions.rb', line 511

def substances
  @substances
end

Class Method Details

.from_string(string) ⇒ Object



535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
# File 'lib/oddb2xml/parslet_compositions.rb', line 535

def ParseComposition.from_string(string)
  return nil if string == nil or  string.eql?('.') or string.eql?('')
  stripped = string.gsub(/^"|["\n]+$/, '')
  return nil unless stripped
  @@errorHandler.nrParsingErrors += 1
  if /(U\.I\.|U\.)$/.match(stripped)
    cleaned = stripped
  else
    cleaned = stripped.sub(/[\.]+$/, '')
  end
  value = nil
  puts "ParseComposition.from_string #{string}" if VERBOSE_MESSAGES # /ng-tr/.match(Socket.gethostbyname(Socket.gethostname).first)

  cleaned = @@errorHandler.apply_fixes(cleaned)
  puts "ParseComposition.new cleaned #{cleaned}" if VERBOSE_MESSAGES and not cleaned.eql?(stripped)

  CompositionTransformer.clear_substances
  result = ParseComposition.new(cleaned)
  parser3 = CompositionParser.new
  transf3 = CompositionTransformer.new
  begin
    if defined?(RSpec)
      ast = transf3.apply(parser3.parse_with_debug(cleaned))
      puts "#{File.basename(__FILE__)}:#{__LINE__}: ==>  #{ast}" if VERBOSE_MESSAGES
    else
      ast = transf3.apply(parser3.parse(cleaned))
    end
  rescue Parslet::ParseFailed => error
    puts "#{File.basename(__FILE__)}:#{__LINE__}: failed parsing ==>  #{cleaned}"
    return nil
  end
  result.source = string
  return result unless ast
  return result if ast.is_a?(Parslet::Slice)
  # pp ast; binding.pry

  result.substances = CompositionTransformer.substances
  excipiens = CompositionTransformer.excipiens
  result.corresp = CompositionTransformer.corresp if CompositionTransformer.corresp
  if excipiens and excipiens.unit
    pro_qty = "/#{excipiens.qty} #{excipiens.unit}".sub(/\/1\s+/, '/')
    result.substances.each {
      |substance|
        substance.chemical_substance.unit = "#{substance.chemical_substance.unit}#{pro_qty}"    if substance.chemical_substance
        substance.dose.unit               = "#{substance.dose.unit}#{pro_qty}"                  if substance.unit and not substance.unit.eql?(excipiens.unit)
    }
  end
  if ast.is_a?(Array) and  ast.first.is_a?(Hash)
    label = ast.first[:label].to_s if ast.first[:label]
    label_description = ast.first[:label_description].to_s if ast.first[:label_description]
  elsif ast and ast.is_a?(Hash)
    label = ast[:label].to_s if  ast[:label]
    label_description = ast[:label_description].to_s if ast[:label_description]
  end
  if label
    if label and not /((A|B|C|D|E|I|II|III|IV|\)+)\s+et\s+(A|B|C|D|E|I|II|III|IV|\))+)/.match(label)
      result.label  = label
    end
    result.label_description = label_description
  end
  @@errorHandler.nrParsingErrors -=1 if result.substances.size > 0 or result.corresp
  return result
end

.reportObject



532
533
534
# File 'lib/oddb2xml/parslet_compositions.rb', line 532

def ParseComposition.report
  @@errorHandler.report
end

.resetObject



529
530
531
# File 'lib/oddb2xml/parslet_compositions.rb', line 529

def ParseComposition.reset
  @@errorHandler = ParseUtil::HandleSwissmedicErrors.new( ErrorsToFix )
end