Class: FrequencyAnalyzer

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(doctext) ⇒ FrequencyAnalyzer

Returns a new instance of FrequencyAnalyzer.



2
3
4
5
6
7
8
9
10
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
# File 'lib/FrequencyAnalyzer.rb', line 2

def initialize(doctext)
  @freqCount = {}
  @doctext = doctext
  @stopwords = SummaData.stopwords
  @mean = 0
  @keywords = [];

  doctext.each(' ') { |word|
    if word != nil
      word.removePunctuation!
      if !@stopwords.include?(word.downcase)
        #stemmed = word.stem
        if @freqCount.has_key?(word)
          @freqCount[word] = @freqCount[word] + 1
        else
          @freqCount[word] = 1
        end
      end
    end
  }
  #@freqCount.each {|key, value| puts "#{key} has #{value}" }

  sum = 0
  count = 0
  keys = @freqCount.keys
  for i in 0..keys.length
    if keys[i] != nil
      sum = sum + @freqCount[keys[i]]
      count = count + 1
    end
  end

  @mean = sum/count
end

Instance Attribute Details

#freqCountObject

Returns the value of attribute freqCount.



53
54
55
# File 'lib/FrequencyAnalyzer.rb', line 53

def freqCount
  @freqCount
end

#keywordsObject

Returns the value of attribute keywords.



53
54
55
# File 'lib/FrequencyAnalyzer.rb', line 53

def keywords
  @keywords
end

Instance Method Details

#analyze(k = 3) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/FrequencyAnalyzer.rb', line 38

def analyze(k=3)
	@keywords = [];
	
	keys = @freqCount.keys
	for i in 0..keys.length
		if keys[i] != nil
			value = @freqCount[keys[i]]
			if value > k * @mean && keys[i] != ""
				@keywords << keys[i]
			end
		end
	end
	@keywords
end