Class: DoseAmountTagger
- Defined in:
- lib/taggers/dose_amount_tagger.rb
Overview
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.
Instance Method Summary collapse
-
#initialize ⇒ DoseAmountTagger
constructor
TODO replace the use of regular expressions searching for “<doseform” with checking for presence of the standoff annotations.
- #map_annotation_from_inline_to_clean(string_with_annots, existing_annotations, new_annotations) ⇒ Object
- #normalize(dose) ⇒ Object
- #parse_annotated_string(as) ⇒ Object
Methods inherited from Tagger
Constructor Details
#initialize ⇒ DoseAmountTagger
TODO replace the use of regular expressions searching for “<doseform” with checking for presence of the standoff annotations
19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
# 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 |
Instance Method Details
#map_annotation_from_inline_to_clean(string_with_annots, existing_annotations, new_annotations) ⇒ Object
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 |
# 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) ⇒ Object
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 |
# 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) ⇒ Object
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/taggers/dose_amount_tagger.rb', line 34 def parse_annotated_string(as) = 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. += 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. = map_annotation_from_inline_to_clean as.to_s, as., as. += return as end |