Module: BOOK

Defined in:
lib/meiou/book.rb

Defined Under Namespace

Classes: Book

Constant Summary collapse

@@IGNORE =
6
@@TINY =
12
@@SMALL =
30
@@MED =
50
@@LARGE =
120
@@BOOK =
Hash.new { |h,k| h[k] = Book.new(k) }

Class Method Summary collapse

Class Method Details

.[](k) ⇒ Object



118
119
120
# File 'lib/meiou/book.rb', line 118

def self.[] k
  @@BOOK[k]
end

.compile!Object



195
196
197
198
199
200
201
202
203
# File 'lib/meiou/book.rb', line 195

def self.compile!
  Dir['books/*'].each { |e|
    k = e.gsub("books/", "").gsub(".txt", "").gsub("_", " ");
    Meiou.log :compile_load, %[#{k}...]
    @@BOOK[k].load(e)
  }
  Meiou.log :compile_done, %[done!]
  return "DONE!"
end

.find(k, *t, &b) ⇒ Object



162
163
164
165
# File 'lib/meiou/book.rb', line 162

def self.find k, *t, &b
  @@BOOK.keys.map { |kk| [t, :tiny].uniq.flatten.each { |s| book = @@BOOK[kk]; book.filter(s,k).each { |e| b.call({ book: kk, index: e, mood: book.mood(e), text: book[e] }); } } }
  return nil
end

.init!Object



185
186
187
188
189
190
191
192
193
# File 'lib/meiou/book.rb', line 185

def self.init!
  Dir['books/*'].each { |e|
    k = e.gsub("books/", "").gsub(".txt", "").gsub("_", " ");
    Meiou.log :init_scan, %[Scanning #{k}...]
    @@BOOK[k]
  }
  Meiou.log :init_done, %[Books scanned!]
  return "DONE!"    
end

.keysObject



122
123
124
# File 'lib/meiou/book.rb', line 122

def self.keys
  @@BOOK.keys
end

.section(k) ⇒ Object



138
139
140
141
142
# File 'lib/meiou/book.rb', line 138

def self.section k
  h = {}
  @@BOOK.keys.map { |e| h[e] = @@BOOK[e].section(k) }
  return h
end

.sectionsObject



156
157
158
159
160
# File 'lib/meiou/book.rb', line 156

def self.sections
  h = {}
  @@BOOK.keys.map { |e| h[e] = @@BOOK[e].sections }
  return h
end

.sizeObject



8
9
10
# File 'lib/meiou/book.rb', line 8

def self.size
  [ @@IGNORE, @@TINY, @@SMALL, @@MED, @@LARGE ]
end

.size?(w) ⇒ Boolean

Returns:

  • (Boolean)


14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/meiou/book.rb', line 14

def self.size? w
  
  if w.length < BOOK.size[0]
    s = :ignore
  elsif w.length > BOOK.size[0] && w.length <= BOOK.size[1]
    s = :tiny
  elsif w.length > BOOK.size[1] && w.length <= BOOK.size[2]
    s = :info
  elsif w.length > BOOK.size[2] && w.length <= BOOK.size[3]
    s = :short
  elsif w.length > BOOK.size[3] && w.length <= BOOK.size[4]
    s = :med
  elsif w.length > BOOK.size[4]
    s = :long
  end
  return s
end

.sizesObject



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

def self.sizes
  [ :tiny, :info, :short, :med, :long ]
end

.tag(k) ⇒ Object



144
145
146
147
148
# File 'lib/meiou/book.rb', line 144

def self.tag k
  h = {}
  @@BOOK.keys.map { |e| h[e] = @@BOOK[e].tag(k) }
  return h
end

.tagsObject



150
151
152
153
154
# File 'lib/meiou/book.rb', line 150

def self.tags
  h = {}
  @@BOOK.keys.map { |e| h[e] = @@BOOK[e].tags }
  return h    
end

.to_aObject



175
176
177
178
179
# File 'lib/meiou/book.rb', line 175

def self.to_a
  a = []
  @@BOOK.each_pair { |k,v| v.map { |e| a << e }}
  return a.compact
end

.to_sObject



181
182
183
# File 'lib/meiou/book.rb', line 181

def self.to_s
  BOOK.to_a.join("\n\n")
end

.topic(k) ⇒ Object



126
127
128
129
130
# File 'lib/meiou/book.rb', line 126

def self.topic k
  h = {}
  @@BOOK.keys.map { |e| h[e] = @@BOOK[e].topic(k) }
  return h
end

.topicsObject



132
133
134
135
136
# File 'lib/meiou/book.rb', line 132

def self.topics
  h = {}
  @@BOOK.keys.map { |e| h[e] = @@BOOK[e].topics }
  return h
end

.word(word, *num, &block) ⇒ Object

Get num number of “opinions” of word based on compiled BOOK.tag(word) keywords and pass it to block



168
169
170
171
172
173
# File 'lib/meiou/book.rb', line 168

def self.word word, *num, &block
  a = []
  BOOK.tag(word).each_pair { |k,v| if v != nil; v.sample(num[0] || 3).compact.uniq.shuffle.each { |e| a << { book: k, index: e, mood: BOOK[k].mood(e), text: BOOK[k][e] } }; end }
  a.flatten.sample(num[1] || num[0] || 3).compact.uniq.shuffle.each { |e| block.call(e) }
  return nil
end