Module: CorrectHorseBatteryStaple

Defined in:
lib/correct_horse_battery_staple.rb,
lib/correct_horse_battery_staple/statistical_array.rb

Defined Under Namespace

Modules: Common, Memoize, Util Classes: Assembler, Backend, Corpus, Generator, Parser, RangeParser, StatisticalArray, Stats, Word, Writer

Constant Summary collapse

VERSION =
'0.6.6'
DEFAULT_CORPUS_NAME =
"tvscripts"
SUPPORTED_FORMATS =
%w[isam isamkd sqlite json csv marshal]

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.corpus_directoryObject

Returns the value of attribute corpus_directory.



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

def corpus_directory
  @corpus_directory
end

.loggerObject

Returns the value of attribute logger.



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

def logger
  @logger
end

Class Method Details

.corpus_list(options = {}) ⇒ Object



24
25
26
27
28
29
# File 'lib/correct_horse_battery_staple.rb', line 24

def self.corpus_list(options = {})
  self.corpus_search_directories.map do |dir|
    Dir[File.join(dir, "*.{#{SUPPORTED_FORMATS.join(',')}}")].
      map {|file| options[:with_paths] ? File.expand_path(file) : File.basename(file, File.extname(file)) }
  end.flatten.sort.uniq
end

.corpus_search_directoriesObject



20
21
22
# File 'lib/correct_horse_battery_staple.rb', line 20

def self.corpus_search_directories
  [self.corpus_directory]
end

.default_corpusObject



16
17
18
# File 'lib/correct_horse_battery_staple.rb', line 16

def self.default_corpus
  self.load_corpus DEFAULT_CORPUS_NAME
end

.find_corpus(corpus_name, formats = SUPPORTED_FORMATS) ⇒ Object



31
32
33
34
35
36
37
38
39
40
# File 'lib/correct_horse_battery_staple.rb', line 31

def self.find_corpus(corpus_name, formats = SUPPORTED_FORMATS)
  formats.each do |fmt|
    fname = "#{corpus_name}.#{fmt}"
    self.corpus_search_directories.each do |dir|
      path = File.join(dir, fname)
      return path if File.exist?(path)
    end
  end
  nil
end

.generate(length = 4) ⇒ Object



57
58
59
# File 'lib/correct_horse_battery_staple.rb', line 57

def self.generate(length = 4)
  CorrectHorseBatteryStaple::Generator.new(self.default_corpus).make(length)
end

.load_corpus(corpus_name, formats = nil) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/correct_horse_battery_staple.rb', line 42

def self.load_corpus(corpus_name, formats = nil)
  if corpus_name.include?(':')
    return CorrectHorseBatteryStaple::Corpus.read corpus_name
  end

  formats = Array(formats || SUPPORTED_FORMATS)
  filename = corpus_name.match(/[.?]/) ? corpus_name :
    self.find_corpus(corpus_name, formats)
  unless (filename && File.exist?(filename))
    raise ArgumentError, "Cannot find corpus #{corpus_name}"
  end

  CorrectHorseBatteryStaple::Corpus.read filename
end