Class: CorrectHorseBatteryStaple::Assembler

Inherits:
Object
  • Object
show all
Includes:
Common
Defined in:
lib/correct_horse_battery_staple/assembler.rb

Constant Summary collapse

VALID_INITIAL_CHARS =
([*'a'..'z']).map {|ls| ls[0]}

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Common

#array_sample, #logger, #random_in_range, #random_number, #set_sample

Constructor Details

#initialize(parser = nil) ⇒ Assembler

Returns a new instance of Assembler.



11
12
13
# File 'lib/correct_horse_battery_staple/assembler.rb', line 11

def initialize(parser = nil)
  @parser = (parser || CorrectHorseBatteryStaple::Parser::Regex.new(:wiktionary))
end

Instance Attribute Details

#wordsObject

Returns the value of attribute words.



7
8
9
# File 'lib/correct_horse_battery_staple/assembler.rb', line 7

def words
  @words
end

Instance Method Details

#corpusObject



39
40
41
42
43
# File 'lib/correct_horse_battery_staple/assembler.rb', line 39

def corpus
  @corpus ||= CorrectHorseBatteryStaple::Corpus::Serialized.new(self.words).tap do |corpus|
    corpus.recalculate
  end
end

#limit(count) ⇒ Object



34
35
36
37
# File 'lib/correct_horse_battery_staple/assembler.rb', line 34

def limit(count)
  self.words.slice!(count..-1) if self.words.length > count
  self
end

#randomizeObject



29
30
31
32
# File 'lib/correct_horse_battery_staple/assembler.rb', line 29

def randomize
  self.words.shuffle!
  self
end

#read(urls) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/correct_horse_battery_staple/assembler.rb', line 15

def read(urls)
  self.words =
    urls.map do |url|
      @parser.parse open(url)
    end.reduce(:+).
        select {|wstruct| VALID_INITIAL_CHARS.include?(wstruct.word[0]) }.
    # we take a round-trip through a Hash to weed out dupes
        inject({}) {|h, wstruct|  h[wstruct.word] = wstruct; h }.
        values.
        sort

  self
end