Class: MESH::Classifier

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

Instance Method Summary collapse

Instance Method Details

#calculate_scores(scored, root, heading, weight) ⇒ Object



38
39
40
41
42
43
44
45
46
# File 'lib/MESH/classifier.rb', line 38

def calculate_scores(scored, root, heading, weight)
  scored[heading] ||= 0
  scored[heading] += weight
  heading.parents.each do |p|
    if p.roots.include? root
      calculate_scores(scored, root, p, weight / 3.0)
    end
  end
end

#classify(weighted_matches) ⇒ Object



3
4
5
6
7
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
# File 'lib/MESH/classifier.rb', line 3

def classify(weighted_matches)

  weighted_headings = []
  weighted_matches.each do |wm|
    wm[:matches].each do |match|
      weighted_headings << [wm[:weight], match[:heading]]
    end
  end

  root_groups = {}
  weighted_headings.each do |weight, heading|
    heading.roots.each do |root|
      root_groups[root] ||= []
      root_groups[root] << [weight, heading]
    end
  end

  chosen = {}

  root_groups.each do |root, weighted_headings|
    scored = {}
    weighted_headings.each do |weight, heading|
      calculate_scores(scored, root, heading, weight)
    end
    scored.each { |h,s| scored[h] = s.round(3) }
    scored.delete_if { |h,s| s == 0 }
    best_score, best_connected = scored.reduce({}) { |h, (k, v)| (h[v] ||= []) << k; h }.max
    most_specific = best_connected.max_by { |h| h.deepest_position(root) }
    chosen[root] = [best_score, scored]
  end

  chosen

end