Class: MESH::Tree

Inherits:
Object
  • Object
show all
Defined in:
lib/MESH/tree.rb

Constant Summary collapse

@@descriptor_classes =
[:make_array_start_at_1, :topical_descriptor, :publication_type, :check_tag, :geographic_descriptor]
@@default_locale =
:en_us

Instance Method Summary collapse

Constructor Details

#initializeTree



8
9
10
11
12
13
14
15
16
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/MESH/tree.rb', line 8

def initialize

  @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)

  current_heading = MESH::Heading.new(self)
  current_heading.default_locale = @@default_locale
  file.each_line do |line|

    case

      when line.match(/^\*NEWRECORD$/)
        unless current_heading.unique_id.nil?
          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 = (.*)/)
        current_heading.unique_id = matches[1]

      when matches = line.match(/^MN = (.*)/)
        current_heading.tree_numbers << matches[1]
        current_heading.roots << matches[1][0] unless current_heading.roots.include?(matches[1][0])

      when matches = line.match(/^MS = (.*)/)
        current_heading.set_summary(matches[1])

      when matches = line.match(/^DC = (.*)/)
        current_heading.descriptor_class = @@descriptor_classes[matches[1].to_i]

      when matches = line.match(/^ST = (.*)/)
        current_heading.semantic_types << MESH::SemanticTypes[matches[1]]

      when matches = line.match(/^MH = (.*)/)
        mh = matches[1]
        current_heading.set_original_heading(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 unless current_heading.entries.include? entry
      #
      when matches = line.match(/^(?:PRINT )?ENTRY = (.*)/)
        entry = matches[1]
        term = entry.match(/([^|]+)/)
        current_heading.entries << term[1] unless current_heading.entries.include? term[1]
        current_heading.structured_entries << MESH::Entry.new(current_heading, entry)

    end

  end

  @by_unique_id.each do |id, heading|
    heading.tree_numbers.each do |tree_number|
      #D03.438.221.173
      parts = tree_number.split('.')
      if parts.size > 1
        parts.pop
        parent_tree_number = parts.join '.'
        parent = @by_tree_number[parent_tree_number]
        heading.parents << parent unless parent.nil? || heading.parents.include?(parent)
        parent.children << heading unless parent.nil? || parent.children.include?(heading)
      end
    end
  end

end

Instance Method Details

#eachObject



242
243
244
245
246
# File 'lib/MESH/tree.rb', line 242

def each
  for i in 0 ... @headings.size
    yield @headings[i] if @headings[i].useful
  end
end

#entry_match_key(e) ⇒ Object



100
101
102
# File 'lib/MESH/tree.rb', line 100

def entry_match_key(e)
  e.strip.upcase
end

#find(unique_id) ⇒ Object

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)
  h.set_natural_language_name(tr.translate(h.natural_language_name), locale)
  h.set_summary(tr.translate(h.summary), locale)
  h.entries.each { |entry| h.entries(locale) << tr.translate(entry) }
  h.entries(locale).sort!
end

@locales << locale

end



218
219
220
# File 'lib/MESH/tree.rb', line 218

def find(unique_id)
  return @by_unique_id[unique_id]
end

#find_by_entry(entry) ⇒ Object



230
231
232
# File 'lib/MESH/tree.rb', line 230

def find_by_entry(entry)
  return @by_entry[entry_match_key(entry)]
end

#find_by_original_heading(heading) ⇒ Object



226
227
228
# File 'lib/MESH/tree.rb', line 226

def find_by_original_heading(heading)
  return @by_original_heading[heading]
end

#find_by_tree_number(tree_number) ⇒ Object



222
223
224
# File 'lib/MESH/tree.rb', line 222

def find_by_tree_number(tree_number)
  return @by_tree_number[tree_number]
end

#linkify_summaries(&block) ⇒ Object



198
199
200
201
202
# File 'lib/MESH/tree.rb', line 198

def linkify_summaries &block
  @headings.each do |h|
    h.linkify_summary &block
  end
end

#load_translation(locale) ⇒ Object



104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
# File 'lib/MESH/tree.rb', line 104

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)

  entries = []
  original_heading = nil
  natural_language_name = nil
  summary = nil
  unique_id = nil
  file.each_line do |line|

    case

      when line.match(/^\*NEWRECORD$/)
        unless unique_id.nil?
          entries.sort!
          entries.uniq!
          if heading = find(unique_id)
            heading.set_original_heading(original_heading, locale) unless original_heading.nil?
            heading.set_natural_language_name(natural_language_name, locale) unless natural_language_name.nil?
            heading.set_summary(summary, locale) unless summary.nil?
            entries.each { |entry| heading.entries(locale) << entry }
          end

          entries = []
          original_heading = nil
          summary = nil
          unique_id = nil
        end

      when matches = line.match(/^UI = (.*)/)
        unique_id = matches[1]

      when matches = line.match(/^MS = (.*)/)
        summary = matches[1]

      when matches = line.match(/^MH = (.*)/)
        mh = matches[1]
        original_heading = mh
        entries << mh
        librarian_parts = mh.match(/(.*), (.*)/)
        natural_language_name = librarian_parts.nil? ? mh : "#{librarian_parts[2]} #{librarian_parts[1]}"

      when matches = line.match(/^(?:PRINT )?ENTRY = ([^|]+)/)
        entry = matches[1].chomp
        entries << entry

    end

  end
  @locales << locale
end

#load_wikipediaObject



159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
# File 'lib/MESH/tree.rb', line 159

def load_wikipedia
  return if @wikipedia_loaded
  filename = File.expand_path("../../../data/mesh_data_2014/d2014.wikipedia.bin.gz", __FILE__)
  gzipped_file = File.open(filename)
  file = Zlib::GzipReader.new(gzipped_file)

  unique_id = nil
  wikipedia_links = []
  file.each_line do |line|

    case

      when line.match(/^\*NEWRECORD$/)
        unless unique_id.nil?
          if heading = find(unique_id)
            wikipedia_links.each do |wl|
              wl[:score] = (wl[:score].to_f / heading.entries.length.to_f).round(2)
            end
            heading.wikipedia_links = wikipedia_links
          end

          wikipedia_links = []
          unique_id = nil
        end

      when matches = line.match(/^UI = (.*)/)
        unique_id = matches[1]

      when matches = line.match(/^WK = (.*)/)
        hash = JSON.parse(matches[1], symbolize_names: true)
        wikipedia_links << hash

    end

  end
  @wikipedia_loaded = true
end

#match_in_text(text) ⇒ Object



248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
# File 'lib/MESH/tree.rb', line 248

def match_in_text(text)
  return [] if text.nil?
  downcased = text.downcase
  matches = []
  @headings.each do |heading|
    next unless heading.useful
    @locales.each do |locale|
      heading.entries(locale).each do |entry|
        if downcased.include? entry.downcase #This is a looser check than the regex but much, much faster
          if /^[A-Z0-9]+$/ =~ entry
            regex = /(^|\W)#{Regexp.quote(entry)}(\W|$)/
          else
            regex = /(^|\W)#{Regexp.quote(entry)}(\W|$)/i
          end
          text.to_enum(:scan, regex).map do |m,|
            matches << {heading: heading, matched: entry, index: $`.size}
          end
        end
      end
    end
  end
  confirmed_matches = []
  matches.combination(2) do |l, r|
    if (r[:index] >= l[:index]) && (r[:index] + r[:matched].length <= l[:index] + l[:matched].length)
      #r is within l
      r[:delete] = true
    elsif (l[:index] >= r[:index]) && (l[:index] + l[:matched].length <= r[:index] + r[:matched].length)
      #l is within r
      l[:delete] = true
    end
  end
  matches.delete_if { |match| match[:delete] }
end

#where(conditions) ⇒ Object



234
235
236
237
238
239
240
# File 'lib/MESH/tree.rb', line 234

def where(conditions)
  matches = []
  @headings.each do |heading|
    matches << heading if heading.matches(conditions)
  end
  matches
end