class MESH::Heading

Attributes

children[RW]
default_locale[RW]
descriptor_class[RW]
forward_references[RW]
linkified_summary[R]
parents[RW]
roots[RW]
semantic_types[RW]
structured_entries[RW]
tree_numbers[RW]
unique_id[RW]
useful[RW]

Public Class Methods

new(tree, default_locale, lines) click to toggle source
# File lib/MESH/heading.rb, line 141
def initialize(tree, default_locale, lines)
  @tree = tree
  @default_locale = default_locale
  @useful = true
  @tree_numbers = []
  @semantic_types = []
  @roots = []
  @parents = []
  @children = []
  @forward_references = []
  @forward_reference_terms = []
  @structured_entries = []
  @original_heading = {}
  @natural_language_name = {}
  @summary = {}
  @wikipedia_links = []

  lines.each do |line|
    case

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

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

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

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

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

      when matches = line.match(/^MH = (.*)/)
        mh = matches[1]
        set_original_heading(mh)
        @structured_entries << MESH::Entry.new(self, mh, default_locale)
        librarian_parts = mh.match(/(.*), (.*)/)
        nln = librarian_parts.nil? ? mh : "#{librarian_parts[2]} #{librarian_parts[1]}"
        set_natural_language_name(nln)

      when matches = line.match(/^(?:PRINT )?ENTRY = (.*)/)
        entry = matches[1]
        @structured_entries << MESH::Entry.new(self, entry, default_locale)

      when matches = line.match(/^FX = (.*)/)
        @forward_reference_terms << matches[1]

    end

  end

end

Public Instance Methods

<=>(other) click to toggle source
# File lib/MESH/heading.rb, line 11
def <=> other
  self.unique_id <=> other.unique_id
end
connect_to_forward_references() click to toggle source
# File lib/MESH/heading.rb, line 128
def connect_to_forward_references
  if !@connected_to_forward_references
    @forward_references = @forward_reference_terms.map do |term|
      @tree.find_heading_by_main_heading(term)
    end
    @connected_to_forward_references = true
  end
end
connect_to_parents() click to toggle source
# File lib/MESH/heading.rb, line 111
def connect_to_parents
  if !@connected_to_parents
    @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 = @tree.find_heading_by_tree_number(parent_tree_number)
        @parents << parent unless parent.nil? || @parents.include?(parent)
        parent.children << self unless parent.nil? || parent.children.include?(self)
      end
    end
    @connected_to_parents = true
  end
end
deepest_position(root = '') click to toggle source
# File lib/MESH/heading.rb, line 58
def deepest_position(root = '')
  return nil if tree_numbers.empty?
  deepest_tree_number = tree_numbers.max_by { |tn| tn.start_with?(root) ? tn.length : 0 }
  deepest_tree_number.split('.').length
end
entries() click to toggle source
# File lib/MESH/heading.rb, line 27
def entries
  @structured_entries
end
entries_by_term() click to toggle source
# File lib/MESH/heading.rb, line 137
def entries_by_term
  Hash[@structured_entries.map { |entry| [entry.term, entry] }]
end
has_ancestor(heading) click to toggle source
# File lib/MESH/heading.rb, line 39
def has_ancestor(heading)
  return false if parents.empty?
  return true if parents.include? heading
  in_grandparents = parents.map { |p| p.has_ancestor(heading) }
  return in_grandparents.include? true
end
has_descendant(heading) click to toggle source
# File lib/MESH/heading.rb, line 46
def has_descendant(heading)
  return false if children.empty?
  return true if children.include? heading
  in_grandchildren = children.map { |p| p.has_descendant(heading) }
  return in_grandchildren.include? true
end
inspect() click to toggle source
# File lib/MESH/heading.rb, line 95
def inspect
  to_s
end
linkify_summary() { |text, entry| ... } click to toggle source
# File lib/MESH/heading.rb, line 31
def linkify_summary
  return if summary.nil?
  @linkified_summary = summary.gsub(/[A-Z]+[A-Z,\s-]+[A-Z]+/).each do |text|
    entry = @tree.find_entry_by_loose_match(text)
    entry ? yield(text, entry) : text
  end
end
load_translation(lines, locale) click to toggle source
# File lib/MESH/heading.rb, line 199
def load_translation(lines, locale)
  new_entries = []
  lines.each do |line|
    case

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

      when matches = line.match(/^MH = (.*)/)
        set_original_heading(matches[1], locale)
        librarian_parts = matches[1].match(/(.*), (.*)/)
        natural_language_name = librarian_parts.nil? ? matches[1] : "#{librarian_parts[2]} #{librarian_parts[1]}"
        set_natural_language_name(natural_language_name, locale)
        entry = new_or_existing_entry(matches[1], locale)
        new_entries << entry

      when matches = line.match(/^(?:PRINT )?ENTRY = (.*)/)
        entry = new_or_existing_entry(matches[1], locale)
        new_entries << entry

    end
  end
  new_entries
end
matches(conditions) click to toggle source
# File lib/MESH/heading.rb, line 70
def matches(conditions)
  conditions.each do |field, pattern|
    if field == :entries
      entries = @structured_entries.select { |entry| pattern =~ entry.term }
      return !entries.nil? && !entries.empty?
    end

    field_content = self.send(field)
    if field_content.kind_of?(Array)
      return false unless field_content.find { |fc| pattern =~ fc }
    elsif field_content.is_a?(TrueClass) || field_content.is_a?(FalseClass)
      return false unless field_content == pattern
    elsif field_content.is_a? Symbol
      return field_content == pattern
    else
      return false unless pattern =~ field_content
    end
  end
  return true
end
natural_language_name(locale = default_locale) click to toggle source
# File lib/MESH/heading.rb, line 19
def natural_language_name(locale = default_locale)
  @natural_language_name[locale]
end
new_or_existing_entry(term, locale) click to toggle source
# File lib/MESH/heading.rb, line 224
def new_or_existing_entry(term, locale)
  existing_entries = @structured_entries.select { |entry| entry.term == term }
  if existing_entries.empty?
    new_entry = MESH::Entry.new(self, term, locale)
    @structured_entries << new_entry
  else
    new_entry = existing_entries[0]
    new_entry.locales << locale
  end
  new_entry
end
original_heading(locale = default_locale) click to toggle source
# File lib/MESH/heading.rb, line 15
def original_heading(locale = default_locale)
  @original_heading[locale]
end
set_natural_language_name(name, locale = default_locale) click to toggle source
# File lib/MESH/heading.rb, line 103
def set_natural_language_name(name, locale = default_locale)
  @natural_language_name[locale] = name
end
set_original_heading(heading, locale = default_locale) click to toggle source
# File lib/MESH/heading.rb, line 99
def set_original_heading(heading, locale = default_locale)
  @original_heading[locale] = heading
end
set_summary(summary, locale = default_locale) click to toggle source
# File lib/MESH/heading.rb, line 107
def set_summary(summary, locale = default_locale)
  @summary[locale] = summary
end
shallowest_position() click to toggle source
# File lib/MESH/heading.rb, line 64
def shallowest_position
  return nil if tree_numbers.empty?
  shallowest_tree_number = tree_numbers.min_by { |tn| tn.length }
  shallowest_tree_number.split('.').length
end
sibling?(heading) click to toggle source
# File lib/MESH/heading.rb, line 53
def sibling?(heading)
  common_parents = parents & heading.parents
  !common_parents.empty?
end
summary(locale = default_locale) click to toggle source
# File lib/MESH/heading.rb, line 23
def summary(locale = default_locale)
  @summary[locale]
end
to_s() click to toggle source
# File lib/MESH/heading.rb, line 91
def to_s
  "#{unique_id}, #{original_heading}, [#{tree_numbers.join(',')}]"
end