Class: Triphthong::Executable

Inherits:
Object
  • Object
show all
Defined in:
lib/triphthong/executable.rb

Instance Method Summary collapse

Constructor Details

#initialize(args = ARGV) ⇒ Executable

Returns a new instance of Executable.



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/triphthong/executable.rb', line 4

def initialize args = ARGV
  opts = Trollop.options args do
    opt :charlimit, 'Character limit',          default: 1000
    opt :database,  'Location of the database', default: 'db.yml'
    opt :structure, 'X+Y syllable structure',   default: '7+6', multi: true
  end

  Trollop.die '--structure must be of the form m+n (where m and n are numbers)' unless opts[:structure].all? { |s| s =~ /^\d+\+\d+$/ }

  @charlimit  = opts[:charlimit]
  @database   = opts[:database]
  @structures = opts[:structure]

  @action = args.shift
  @paths  = args
end

Instance Method Details

#runObject



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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/triphthong/executable.rb', line 21

def run
  db = YAML::Store.new @database

  case @action
  when 'build-db-from-api'
    db.transaction do
      @structures.each { |str, _| db[str] = Hash.new { |str, rhyme| str[rhyme] = [] } }
      books = JSON.parse open('http://www.wolnelektury.pl/api/books/').read
      books.each.with_index do |book, i|
        begin
          puts "#{(i+1).to_s.rjust books.size.to_s.size}/#{books.size}: #{book['title']}"
          info = JSON.parse open(book['href']).read
          text = open(info['txt']).read.force_encoding('UTF-8').split("\n-----\n").first.split("\n\n\n", 2).last.extend Text
          text.sentences.each do |verse|
            verse.source = "#{book['title']} (#{info['authors'].map { |a| a['name'] }.join ', '})"
            @structures.each do |str|
              db[str][verse.rhyme_pattern] << verse if verse.has_structure? str
            end
          end
        rescue
          next
        end
      end
    end
  when 'build-db-from-txt'
    db.transaction do
      @structures.each { |str, _| db[str] = Hash.new { |str, rhyme| str[rhyme] = [] } }
      @paths.each do |path|
        File.read(path).extend(Text).sentences.each do |verse|
          verse.source = File.basename path
          @structures.each do |str|
            db[str][verse.rhyme_pattern] << verse if verse.has_structure? str
          end
        end
      end
    end
  when 'rhyme'
    verses = []
    db.transaction true do
      rhymes = db[@structures.first].reject { |_, vs| vs.size < 2 }
      while verses.map(&:size).inject(0, :+) < @charlimit do
        a, b = rhymes[rhymes.keys.sample].sample 2
        next unless a.rhymes_with? b
        next if a.source and b.source and a.source == b.source
        verses += [a, b]
      end
    end
    @charlimit -= verses.size + verses.size / 4
    verses.pop while verses.map(&:size).inject(0, :+) > @charlimit or (verses.size % 4).nonzero?
    puts verses.each_slice(4).map { |s| s.map(&:text).join "\n" }.join "\n\n"
  end
end