Class: BOOK::Book
- Inherits:
-
Object
- Object
- BOOK::Book
- Defined in:
- lib/meiou/book.rb
Instance Method Summary collapse
- #[](k) ⇒ Object
- #each(k, &b) ⇒ Object
- #filter(s, k) ⇒ Object
- #id ⇒ Object
-
#initialize(k) ⇒ Book
constructor
A new instance of Book.
- #load(f) ⇒ Object
- #map(&b) ⇒ Object
- #mood(k) ⇒ Object
- #section(k) ⇒ Object
- #sections ⇒ Object
- #tag(k) ⇒ Object
- #tags ⇒ Object
- #topic(k) ⇒ Object
- #topics ⇒ Object
Constructor Details
#initialize(k) ⇒ Book
Returns a new instance of Book.
33 34 35 36 37 38 39 40 41 |
# File 'lib/meiou/book.rb', line 33 def initialize k @id = k @ind = PStore.new("db/book-#{k}.pstore") # index sizes @sec = PStore.new("db/book-sec-#{k}.pstore") # sections @top = PStore.new("db/book-top-#{k}.pstore") # topics @tag = PStore.new("db/book-tag-#{k}.pstore") # tags @mod = PStore.new("db/book-mod-#{k}.pstore") # moods @bin = Hash.new { |h,k| h[k] = PStore.new("db/book-#{@id}-#{k}.pstore") } # sorted bins end |
Instance Method Details
#[](k) ⇒ Object
70 71 72 73 |
# File 'lib/meiou/book.rb', line 70 def [] k n = @ind.transaction { |db| db[k] } @bin[n].transaction { |db| db[k] } end |
#each(k, &b) ⇒ Object
112 113 114 115 |
# File 'lib/meiou/book.rb', line 112 def each k, &b @bin[k].transaction { |db| db.keys.map { |e| b.call(db[e]) } } return nil end |
#filter(s, k) ⇒ Object
102 103 104 105 106 |
# File 'lib/meiou/book.rb', line 102 def filter s, k a = [] @bin[s].transaction { |db| db.keys.map { |e| if Regexp.new(k).match(db[e]); a << e end } } return a end |
#id ⇒ Object
43 |
# File 'lib/meiou/book.rb', line 43 def id; @id; end |
#load(f) ⇒ Object
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
# File 'lib/meiou/book.rb', line 45 def load f c = File.read(f) c.gsub(/\r\n/,"\n").gsub(/\n\n+/,"\n\n").split("\n\n").each_with_index do |e,i| x = e.gsub(/\n/," ").gsub(/\s+/," ").strip; if !/Project Gutenberg/.match(x) if !/^[[[:upper:]]|[[:blank:]]|[[:punct:]]|[[:digit:]]]*$/.match(x) s = BOOK.size?(x.split(/\s/)) Meiou.log "compile load item", "[#{@id}][#{i}] #{s}" if s != :ignore @bin[s].transaction { |db| db[i] = x } @ind.transaction { |db| db[i] = s } @mod.transaction { |db| db[i] = MOOD[x] } Meiou.extract(x) { |e| @tag.transaction { |db| if !db.key?(e[:word]); db[e[:word]] = []; end; db[e[:word]] << i; } } end else if !/GUTENBERG/.match(x) Meiou.log "compile load section", %[#{@id}>#{i}: #{x}] @sec.transaction { |db| db[i] = x } @top.transaction { |db| db[x] = i } end end end end end |
#map(&b) ⇒ Object
107 108 109 110 111 |
# File 'lib/meiou/book.rb', line 107 def map &b BOOK.sizes.each do |size| @bin[size].transaction { |db| db.keys.map { |e| b.call(db[e]) } } end end |
#mood(k) ⇒ Object
74 75 76 |
# File 'lib/meiou/book.rb', line 74 def mood k @mod.transaction { |db| db[k] } end |
#section(k) ⇒ Object
92 93 94 |
# File 'lib/meiou/book.rb', line 92 def section k @sec.transaction { |db| db[k] } end |
#sections ⇒ Object
96 97 98 99 100 |
# File 'lib/meiou/book.rb', line 96 def sections h = {} @sec.transaction { |db| db.keys.each { |e| h[e] = db[e] } } return h end |
#tag(k) ⇒ Object
77 78 79 |
# File 'lib/meiou/book.rb', line 77 def tag k @tag.transaction { |db| db[k] } end |
#tags ⇒ Object
80 81 82 |
# File 'lib/meiou/book.rb', line 80 def @tag.transaction { |db| db.keys } end |
#topic(k) ⇒ Object
83 84 85 |
# File 'lib/meiou/book.rb', line 83 def topic k @top.transaction { |db| db[k] } end |
#topics ⇒ Object
86 87 88 89 90 |
# File 'lib/meiou/book.rb', line 86 def topics h = {} @top.transaction { |db| db.keys.each { |e| h[e] = db[e] } } return h end |