lib/MESH/tree.rb in mesh-medical-subject-headings-2.0.3 vs lib/MESH/tree.rb in mesh-medical-subject-headings-2.0.4

- old
+ new

@@ -9,10 +9,11 @@ @headings = [] @by_unique_id = {} @by_tree_number = {} @by_original_heading = {} + @by_entry = {} @locales = [@@default_locale] filename = File.expand_path('../../../data/mesh_data_2014/d2014.bin.gz', __FILE__) gzipped_file = File.open(filename) file = Zlib::GzipReader.new(gzipped_file) @@ -28,12 +29,18 @@ current_heading.entries.sort! @headings << current_heading @by_unique_id[current_heading.unique_id] = current_heading @by_original_heading[current_heading.original_heading] = current_heading current_heading.tree_numbers.each do |tree_number| + raise if @by_tree_number[tree_number] @by_tree_number[tree_number] = current_heading end + match_headings = current_heading.entries.map { |e| entry_match_key(e) }.uniq + match_headings.each do |entry| + raise "#{@by_entry[entry]} vs #{current_heading} on #{entry}\n\n#{@by_entry[entry].entries}\n\n#{current_heading.entries}" if @by_entry[entry] + @by_entry[entry] = current_heading + end end current_heading = MESH::Heading.new(self) current_heading.default_locale = @@default_locale when matches = line.match(/^UI = (.*)/) @@ -50,18 +57,18 @@ current_heading.descriptor_class = @@descriptor_classes[matches[1].to_i] when matches = line.match(/^MH = (.*)/) mh = matches[1] current_heading.set_original_heading(mh) - current_heading.entries << mh + current_heading.entries << mh unless current_heading.entries.include? mh librarian_parts = mh.match(/(.*), (.*)/) nln = librarian_parts.nil? ? mh : "#{librarian_parts[2]} #{librarian_parts[1]}" current_heading.set_natural_language_name(nln) when matches = line.match(/^(?:PRINT )?ENTRY = ([^|]+)/) entry = matches[1].chomp - current_heading.entries << entry + current_heading.entries << entry unless current_heading.entries.include? entry end end @@ -79,10 +86,14 @@ end end end + def entry_match_key(e) + e.strip.upcase + end + def load_translation(locale) return if @locales.include? locale filename = File.expand_path("../../../data/mesh_data_2014/d2014.#{locale}.bin.gz", __FILE__) gzipped_file = File.open(filename) file = Zlib::GzipReader.new(gzipped_file) @@ -134,10 +145,16 @@ end @locales << locale end + def linkify_summaries &block + @headings.each do |h| + h.linkify_summary &block + end + end + # NO LONGER COVERED BY TESTS # def translate(locale, tr) # return if @locales.include? locale # @headings.each_with_index do |h, i| # h.set_original_heading(tr.translate(h.original_heading), locale) @@ -158,9 +175,13 @@ return @by_tree_number[tree_number] end def find_by_original_heading(heading) return @by_original_heading[heading] + end + + def find_by_entry(entry) + return @by_entry[entry_match_key(entry)] end def where(conditions) matches = [] @headings.each do |heading| \ No newline at end of file