Sha256: 932386f380eb61f40d65c0a879cf786c534260b4a9eadb33d952f2c2a3d2e8dd

Contents?: true

Size: 1.17 KB

Versions: 1

Compression:

Stored size: 1.17 KB

Contents

class Wc 
  attr_reader :filename, :occurrences, :words
  
  def initialize(filename, words)
    @filename = filename
    @occurrences = read
    @sorted = Array(occurrences).sort { |one, two| -(one[1] <=> two[1]) }
    @words = words
  end
  
  def to_text
    if @words == -1
      @sorted.each { |elem|
        puts "\"#{elem[0]}\" has #{elem[1]} occurrences"
      }
    else 
      @sorted[0..@words-1].each { |elem|
        puts "\"#{elem[0]}\" has #{elem[1]} occurrences"
      }
    end
  end
  
  def to_json
    if @words == -1
      return @sorted.to_json
    else
      return @sorted[0..@words-1].to_json
    end
  end
  
  def to_cloud
    if @words == -1
      cloud_items = @sorted
    else
      cloud_item = @sorted[0..@words-1]
    end
    ret = "<dl>"
    i=1
    cloud_item.each { |elem|
      ret+="<dt id=\"a"+i.to_s+"\">" + elem[0] +"</dt>"
      i+=1
    }
    ret += "</dl>"
    ret
  end
  
  private
  
  def read() 
    occurrences = Hash.new { |h, k| h[k] = 0 }
    File.open(@filename, "r") { |f|
          f.each_line { |line|
            words = line.split
            words.each { |w| occurrences[w] += 1 }
          }
        }
    occurrences
  end
  
  
  
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
wc-0.92.0 lib/wc.rb