Blame view

lib/context/query_context.rb 1.03 KB
65040e3e6   Romain Deveaud   changes made for ...
1
2
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
  #!/usr/bin/env ruby
  
  class QueryContext < Array
  
    def best_concept_model
      max_sim = 0.0
      best    = nil
  
      for p in 0...self.count
        sim = 0.0
        for pp in 0...self.count
          next if pp == p
          combs = self.at(p).concepts.product self.at(pp).concepts
          sum_sim = combs.inject(0.0) { |sum,k| sum + k.first.weighted_concept_similarity(k.last) }
          sim += sum_sim/combs.count
        end
  
  
        if sim > max_sim
          max_sim = sim
          best = p
        end
      end
      
      best.nil? ? nil : self.at(best)
    end
  
    def best_concept_model_word
      max_sim = 0.0
      best    = nil
  
      for p in 0...self.count
        sim = 0.0
        for pp in 0...self.count
          next if pp == p
          combs = self.at(p).concepts.product self.at(pp).concepts
          sum_sim = combs.inject(0.0) { |sum,k| sum + k.first.concept_words_similarity(k.last) }
          sim += sum_sim/combs.count
        end
  
  
        if sim > max_sim
          max_sim = sim
          best = p
        end
      end
      
      best.nil? ? nil : self.at(best)
    end
  end