Class: Wc

Inherits:
Object
  • Object
show all
Defined in:
lib/wc.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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, options={})
  @hide_list = options["hide_list"]
  @words = options["words"]
  @no_autorun = options["no_autorun"]
  
  @css_mode = options["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

#cssObject (readonly)

Returns the value of attribute css.



2
3
4
# File 'lib/wc.rb', line 2

def css
  @css
end

#css_modeObject (readonly)

Returns the value of attribute css_mode.



2
3
4
# File 'lib/wc.rb', line 2

def css_mode
  @css_mode
end

#filenameObject (readonly)

Returns the value of attribute filename.



2
3
4
# File 'lib/wc.rb', line 2

def filename
  @filename
end

#hide_listObject

Returns the value of attribute hide_list.



3
4
5
# File 'lib/wc.rb', line 3

def hide_list
  @hide_list
end

#no_autorunObject (readonly)

Returns the value of attribute no_autorun.



2
3
4
# File 'lib/wc.rb', line 2

def no_autorun
  @no_autorun
end

#occurrencesObject (readonly)

Returns the value of attribute occurrences.



2
3
4
# File 'lib/wc.rb', line 2

def occurrences
  @occurrences
end

#wordsObject (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



127
128
129
130
131
132
133
134
135
136
# File 'lib/wc.rb', line 127

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

#getObject



138
139
140
141
142
143
144
145
146
# File 'lib/wc.rb', line 138

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



112
113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/wc.rb', line 112

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_cloudObject

def to_json

  if @words == -1
    return @sorted.to_json
  else
    return @sorted[0..@words-1].to_json
  end
end


56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/wc.rb', line 56

def to_cloud
  if @words == -1
    cloud_items = @sorted
  else
    cloud_items = @sorted[0..@words-1]
  end
  max = @sorted.first[1]
  min = @sorted.last[1]
  
  range = max - min
  interval = (1.0 * range)/10
  
  r0=min+interval
  r1=r0+interval
  r2=r1+interval
  r3=r2+interval
  r4=r3+interval
  r5=r4+interval
  r6=r5+interval
  r7=r6+interval
  r8=r7+interval
  r9=r8+interval
  r10=r9+interval
  
  ret = "<ul id=\"cloud\">"
  
  cloud_items.each { |elem|
    if (elem[1]>r10)
      c="tag9"
    elsif (elem[1] > r9)
      c="tag8"
    elsif (elem[1] > r8)
      c="tag7"
    elsif (elem[1] > r7)
      c="tag6"
    elsif (elem[1] > r6)
      c="tag5"
    elsif (elem[1] > r5)
      c="tag4"
    elsif (elem[1] > r4)
      c="tag3"
    elsif (elem[1] > r3)
      c="tag2"
    elsif (elem[1] > r2)
      c="tag1"
    else
      c="tag0"
    end
  
    ret+="<li><a href=\"#\" class=\""+c+"\">" + elem[0] +"</a></li>"
    
  }
  ret += "</ul>"
  ret
end

#to_textObject



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