class DoseAmountTagger

Copyright 2015 The MITRE Corporation

Licensed under the Apache License, Version 2.0 (the “License”); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an “AS IS” BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

Public Class Methods

new() click to toggle source

TODO replace the use of regular expressions searching for “<doseform” with checking for presence of the standoff annotations

# File lib/taggers/dose_amount_tagger.rb, line 19
def initialize
    @name = 'doseamount'
    @content_group = 1
    
    @shared_search = /(?x)(?:puffs?|tablespoons?|teaspoons?|bolus(?:es)?|
              scoops?|caps?fuls?|ampules?|tubes?|drops?|gtts?|
              milliliters?|ml|c\.c\.|cc\b|\bL\b|tsp|tbsp)/i
    @default_precondition = /(<doseform)/i
    @default_search = /(?x)((?:\d\s+)?(?:\d(?:\/|-))?\d+(?:\.\d+)?
                   (?:\s?(?:-|\/)\s+\d+)?|one|two)(\s+<strength>
                   (?:[^<])+<\/strength>\s+<strength_unit>(?:[^<])+
                   <\/strength_unit>)?(\s+<doseform)/i
    
end

Public Instance Methods

map_annotation_from_inline_to_clean(string_with_annots, existing_annotations, new_annotations) click to toggle source
# File lib/taggers/dose_amount_tagger.rb, line 54
def map_annotation_from_inline_to_clean(string_with_annots, existing_annotations, new_annotations)
  clean_string = ""
  annots_re = /<[^>]+>/
  first_original = 0
  last_original = string_with_annots.length

  #re_result = str.scan(annots_re)
  re_results = string_with_annots.to_enum(:scan, annots_re).map { Regexp.last_match }

  map_dirty_to_clean_annot = lambda do |dirty_annot,re_results|
    clean_annot = dirty_annot.dup
    #correction = re_results.map{ |item| item.offset(0)[1] <= dirty_annot.start ? item.offset(0)[1] - item.offset(0)[0] : 0}.reduce(0, :+)
    prior_lengths = re_results.map{ |item| item.offset(0)[1] <= dirty_annot.start ? item.offset(0)[1] - item.offset(0)[0] : 0}
    correction = prior_lengths.reduce(0, :+)
    clean_annot.start = clean_annot.start - correction
    clean_annot.end = clean_annot.end - correction
    clean_annot
  end

  clean_annots = new_annotations.map{|old_entry| map_dirty_to_clean_annot[old_entry, re_results] }

  clean_annots
end
normalize(dose) click to toggle source
# File lib/taggers/dose_amount_tagger.rb, line 78
def normalize(dose)
  re = {:int =>  /^\d+$/,
      :decimal => /^(\d+)?\.\d+$/,
      :fraction => /^(\d+) ?\/ ?(\d+)$/,
      :compound => /^(\d+) (\d+) ?\/ ?(\d+)$/,
      :range => /^([0-9\/\.]+) ?- ?([0-9\/\.]+)$/}
  x = case dose
  when re[:int]
    Integer dose
  when re[:decimal]
    Float dose
  when re[:fraction] # simple fraction
    num, denom = dose.split '/'
    Float(num) / Float(denom)
  when re[:compound] # compound fraction
    Integer(dose[re[:compound], 1]) + Float(dose[re[:compound], 2] )/Float(dose[re[:compound], 3])
  when re[:range]
    normalize(dose[re[:range], 1]) .. normalize(dose[re[:range], 2])
  else 
    [nil,"one","two","three","four","five","six","seven","eight","nine","ten"].index dose.downcase
  end
end
parse_annotated_string(as) click to toggle source
# File lib/taggers/dose_amount_tagger.rb, line 34
def parse_annotated_string(as)
    new_tags = parse_text as.to_s, @default_precondition, @default_search
    local_precondition = /((#{@shared_search})|<route)/i
    local_search = /(?x)((?:(?:\d\s+)?(?:\d(?:\/|-))?(?:\d+(?:\.\d+)?
                   (?:\s?(?:-|\/)\s?\d+)?)|one|two))(\s+
                   (?:(?:#{@shared_search})|<route))/i

    # AnnotatedString::to_s interpolates standoff tags into inline XML.
    # This is necessary because the regexes rely on other tag adjacency for search context.
    # This is kludgy, and should at some point be replaced by using the standoff annotation directly so we can leave the signal text alone.
    new_tags += parse_text as.to_s, local_precondition, local_search

    # Because of the conversion from standoff to inline tags (above), match positions will be wrong.
    # The following line is a necessary workaround to map the start and end positions from the xml-augmented strings back to the signal text.
    new_tags2 = map_annotation_from_inline_to_clean as.to_s, as.tags, new_tags

    as.tags += new_tags2
    return as
end