Sha256: 81eb51bec110dccd7f6146a43d53dcc90a3c1270d861ba4c1e902d153148ed66

Contents?: true

Size: 1.33 KB

Versions: 11

Compression:

Stored size: 1.33 KB

Contents

require 'rubygems'
require 'couchrest'

couch = CouchRest.new("http://127.0.0.1:5984")
db = couch.database('word-count-example')
db.delete! rescue nil
db = couch.create_db('word-count-example')

books = {
  'outline-of-science.txt' => 'http://www.gutenberg.org/files/20417/20417.txt',
  'ulysses.txt' => 'http://www.gutenberg.org/dirs/etext03/ulyss12.txt',
  'america.txt' => 'http://www.gutenberg.org/files/16960/16960.txt',
  'da-vinci.txt' => 'http://www.gutenberg.org/dirs/etext04/7ldv110.txt'
}

books.each do |file, url|
  pathfile = File.join(File.dirname(__FILE__),file)
  `curl #{url} > #{pathfile}` unless File.exists?(pathfile)  
end


books.keys.each do |book|
  title = book.split('.')[0]
  puts title
  File.open(File.join(File.dirname(__FILE__),book),'r') do |file|
    lines = []
    chunk = 0
    while line = file.gets
      lines << line
      if lines.length > 10
        db.save_doc({
          :title => title,
          :chunk => chunk, 
          :text => lines.join('')
        })
        chunk += 1
        puts chunk
        lines = []
      end
    end
  end
end

# puts "The books have been stored in your CouchDB. To initiate the MapReduce process, visit http://127.0.0.1:5984/_utils/ in your browser and click 'word-count-example', then select view 'words' or 'count'. The process could take about 15 minutes on an average MacBook."


Version data entries

11 entries across 11 versions & 2 rubygems

Version Path
couchrest-2.0.1 examples/word_count/word_count.rb
couchrest-2.0.0 examples/word_count/word_count.rb
couchrest-2.0.0.rc3 examples/word_count/word_count.rb
couchrest-2.0.0.rc2 examples/word_count/word_count.rb
couchrest-2.0.0.rc1 examples/word_count/word_count.rb
couchrest-2.0.0.beta2 examples/word_count/word_count.rb
couchrest-2.0.0.beta1 examples/word_count/word_count.rb
couchrest-1.2.1 examples/word_count/word_count.rb
dpla-couchrest-1.2.1.pre.dpla examples/word_count/word_count.rb
couchrest-1.2.0 examples/word_count/word_count.rb
couchrest-1.1.3 examples/word_count/word_count.rb