Class: WordBloom::Quality

Inherits:
Object
  • Object
show all
Defined in:
lib/word-bloom/quality.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeQuality

Returns a new instance of Quality.



12
13
14
15
# File 'lib/word-bloom/quality.rb', line 12

def initialize
  @correlations = {}
  @languages = []
end

Instance Attribute Details

#correlationsObject (readonly)

Returns the value of attribute correlations.



18
19
20
# File 'lib/word-bloom/quality.rb', line 18

def correlations
  @correlations
end

#languagesObject

Returns the value of attribute languages.



17
18
19
# File 'lib/word-bloom/quality.rb', line 17

def languages
  @languages
end

Class Method Details

.build_allObject



5
6
7
8
9
10
# File 'lib/word-bloom/quality.rb', line 5

def self.build_all
  qual = self.new
  qual.languages = Scorer::all_languages
  qual.build_metrics
  qual
end

Instance Method Details

#build_metricsObject



20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/word-bloom/quality.rb', line 20

def build_metrics
  require 'word-bloom/filter-builder'

  languages.each do |lang|
    Scorer::load_filter(lang)
  end

  languages.each_with_index do |lang, index|
    languages.drop(index + 1).each do |other_lang|
      @correlations[[lang, other_lang]] =
        (Scorer::filter_for(lang) & Scorer::filter_for(other_lang)).to_f / FilterBuilder::BITFIELD_WIDTH
    end
  end
end

#to_sObject



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/word-bloom/quality.rb', line 35

def to_s
  width = languages.map{|lang| lang.to_s.length}.max + 2
  col_sep = "  "
  ([([" " * width] + languages.map{|lang| lang.to_s.rjust(width)}).join(col_sep)] +
  languages.map do |left|
    ([left.to_s.ljust(width)] +
      languages.map do |right|
      if num = @correlations[[left, right]] || @correlations[[right, left]]
        "%#{width}f" % num
      else
        "-" * width
      end
      end
    ).join(col_sep)
  end).join("\n")
end