Class: Wc
- Inherits:
-
Object
- Object
- Wc
- Defined in:
- lib/wc.rb
Instance Attribute Summary collapse
-
#css ⇒ Object
readonly
Returns the value of attribute css.
-
#css_mode ⇒ Object
readonly
Returns the value of attribute css_mode.
-
#filename ⇒ Object
readonly
Returns the value of attribute filename.
-
#hide_list ⇒ Object
Returns the value of attribute hide_list.
-
#no_autorun ⇒ Object
readonly
Returns the value of attribute no_autorun.
-
#occurrences ⇒ Object
readonly
Returns the value of attribute occurrences.
-
#words ⇒ Object
readonly
Returns the value of attribute words.
Instance Method Summary collapse
- #feed(line) ⇒ Object
- #get ⇒ Object
-
#initialize(filename = nil, options = {}) ⇒ Wc
constructor
Creates a new Wc object.
- #read(filename) ⇒ Object
- #to_cloud ⇒ Object
- #to_json ⇒ Object
- #to_text ⇒ Object
Constructor Details
#initialize(filename = nil, options = {}) ⇒ Wc
Creates a new Wc object
The filename is no mandatory, but if provided it will be used to feed thw word counter gem, otherwise standard input is used. Options is an array customizing the gem’s behavior further.
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
# File 'lib/wc.rb', line 12 def initialize(filename=nil, ={}) @hide_list = ["hide_list"] @words = ["words"] @no_autorun = ["no_autorun"] @css_mode = ["css_mode"] if ! @hide_list @hide_list = [] end if ! @no_autorun warn "[DEPRECATION]: 'no_autorun' option will be deprecated in wc 1.0.0 and the default behavior will be no_autorun=TRUE" if filename @filename = filename @occurrences = _read else @filename =STDIN @occurrences = _feed end @sorted = Array(occurrences).sort { |one, two| -(one[1] <=> two[1]) } end end |
Instance Attribute Details
#css ⇒ Object (readonly)
Returns the value of attribute css.
2 3 4 |
# File 'lib/wc.rb', line 2 def css @css end |
#css_mode ⇒ Object (readonly)
Returns the value of attribute css_mode.
2 3 4 |
# File 'lib/wc.rb', line 2 def css_mode @css_mode end |
#filename ⇒ Object (readonly)
Returns the value of attribute filename.
2 3 4 |
# File 'lib/wc.rb', line 2 def filename @filename end |
#hide_list ⇒ Object
Returns the value of attribute hide_list.
3 4 5 |
# File 'lib/wc.rb', line 3 def hide_list @hide_list end |
#no_autorun ⇒ Object (readonly)
Returns the value of attribute no_autorun.
2 3 4 |
# File 'lib/wc.rb', line 2 def no_autorun @no_autorun end |
#occurrences ⇒ Object (readonly)
Returns the value of attribute occurrences.
2 3 4 |
# File 'lib/wc.rb', line 2 def occurrences @occurrences end |
#words ⇒ Object (readonly)
Returns the value of attribute words.
2 3 4 |
# File 'lib/wc.rb', line 2 def words @words end |
Instance Method Details
#feed(line) ⇒ Object
87 88 89 90 91 92 93 94 95 96 |
# File 'lib/wc.rb', line 87 def feed(line) @occurrences = Hash.new { |h, k| h[k] = 0 } words = line.split words.each { |w| if ! hide_list.include?(w.downcase) @occurrences[w.downcase] += 1 end } @occurrences end |
#get ⇒ Object
98 99 100 101 102 103 104 105 106 |
# File 'lib/wc.rb', line 98 def get() @sorted = Array(occurrences).sort { |one, two| -(one[1] <=> two[1]) } if @words == -1 c = @sorted else c = @sorted[0..@words-1] end @sorted = c end |
#read(filename) ⇒ Object
72 73 74 75 76 77 78 79 80 81 82 83 84 85 |
# File 'lib/wc.rb', line 72 def read(filename) @occurrences = Hash.new { |h, k| h[k] = 0 } File.open(filename, "r") { |f| f.each_line { |line| words = line.split words.each { |w| if ! hide_list.include?(w.downcase) @occurrences[w.downcase] += 1 end } } } @occurrences end |
#to_cloud ⇒ Object
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 |
# File 'lib/wc.rb', line 56 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 |
#to_json ⇒ Object
48 49 50 51 52 53 54 |
# File 'lib/wc.rb', line 48 def to_json if @words == -1 return @sorted.to_json else return @sorted[0..@words-1].to_json end end |
#to_text ⇒ Object
36 37 38 39 40 41 42 43 44 45 46 |
# File 'lib/wc.rb', line 36 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 |