Class: WordCloud

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

Instance Method Summary collapse

Constructor Details

#initialize(input, type) ⇒ WordCloud



4
5
6
7
8
9
# File 'lib/wordcloud.rb', line 4

def initialize(input, type)
  @input = JSON.parse(input)
  @output = ""
  @wordhash = Hash.new
  @type = type
end

Instance Method Details

#genOutput(input, docnum) ⇒ Object

Generates HTML output based on word size



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
111
112
# File 'lib/wordcloud.rb', line 85

def genOutput(input, docnum)
  splitinput = input.split(/ /)
  output = ""
  
  splitinput.each do |w|
    if w =~ /\n/
      w.gsub!(/\n/, "<br />")
    end

    if @wordhash[w]
      size = 10 + @wordhash[w] 
      if @wordhash[w] > 2
       size = size - (docnum*0.1)
      end

      if size > 18
        size = 18
        output = output + " <span style=\"font-size:" + size.to_s + "px\"><b>" + w + "</b></span>"
      else
        output = output + " <span style=\"font-size:" + size.to_s + "px\">" + w + "</span>"
      end
    else
      output = output + " " + w
    end
  end

  return output
end

#parseObject

Splits corpus on words



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/wordcloud.rb', line 12

def parse   
  docnum = 0
  if @type == "single"
    @input.each do |j|
      if (j[1] != nil) && (j[1].is_a? String)
        splitinput = j[1].split(" ")
        splitinput.each do |w|
          if w.include? "\\n"
            w.gsub!("\\n", "<br />")
          end
          wordCount(w)
        end
      end
      docnum += 1
    end

    @input.each do |j|
      if (j[1] != nil) && (j[1].is_a? String)
        @output = @output + "<b>" + j[0] + ": " + "</b>" + genOutput(j[1], docnum) + "<br />"
      else
        @output = @output + "<b>" + j[0] + ": " + "</b>" + j[1].to_s + "<br />"
      end
    end
    @output = @output + "<br />"

  elsif @type == "multiple"
    @input.each do |i|
      i.each do |j|
        if (j[1] != nil) && (j[1].is_a? String)
          splitinput = j[1].split(" ")
          splitinput.each do |w|
            if w.include? "\\n"
              w.gsub!("\\n", "<br />")
            end
            wordCount(w)
          end
        end
      end
      docnum += 1
    end

    @input.each do |i|
      i.each do |j|
        if (j[1] != nil) && (j[1].is_a? String)
          @output = @output + "<b>" + j[0] + ": " + "</b>" + genOutput(j[1], docnum) + "<br />"
        else
          @output = @output + "<b>" + j[0] + ": " + "</b>" + j[1].to_s + "<br />"
        end
      end
      @output = @output + "<br />"
    end
  end
  return @output
end

#wordCount(word) ⇒ Object

Counts number of times a word shows up



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/wordcloud.rb', line 68

def wordCount(word)
  commonwords = ["the", "and", "of", "a", "to", "is", "in", "its", "The", "on", "as", "for", "has", "will", "As", "or", "have", "while", "While", "that", "out", "such", "also", "by", "said", "with", "than", "only", "into", "an", "one", "other", "but", "for", "from", "<br />", "I", "more", "about", "About", "again", "Again", "against", "all", "are", "at", "be", "being", "been", "can", "could", "did", "do", "don't", "down", "up", "each", "few", "get", "got", "great", "had", "have", "has", "he", "her", "she", "he", "it", "we", "they", "if", "thus", "it's", "hers", "his", "how", "why", "when", "where", "just", "like", "you", "me", "my", "most", "more", "no", "not", "yes", "off", "once", "only", "our", "out", "over", "under", "own", "then", "some", "these", "there", "then", "this", "those", "too", "through", "between", "until", "very", "who", "with", "wouldn't", "would", "was", "were", "itself", "himself", "herself", "which", "make", "during", "before", "after", "if", "any", "become", "around", "several", "them", "their", "however"]

  # Make capitalized array of common words
  commoncaps = Array.new
  commonwords.each do |c|
    commoncaps.push(c.capitalize)
  end

  if (@wordhash[word]) && (!commonwords.include? word) && (!commoncaps.include? word) 
    @wordhash[word] += 1
  else
    @wordhash[word] = 1
  end
end