Class: WordCloud

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

Instance Method Summary collapse

Constructor Details

#initialize(input) ⇒ WordCloud

Returns a new instance of WordCloud.



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

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

Instance Method Details

#genOutput(input, docnum) ⇒ Object

Generates HTML output based on word size



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

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



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

def parse   
  docnum = 0
  @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] + "<br />"
      end
    end
    @output = @output + "<br />"
  end
  return @output
end

#wordCount(word) ⇒ Object

Counts number of times a word shows up



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/wordcloud.rb', line 42

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