Class: NLP::TextStatistics

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeTextStatistics

Returns a new instance of TextStatistics.



7
8
9
10
11
12
13
14
# File 'lib/text_statistics.rb', line 7

def initialize
  @word_count = 0           # number of found words
  @total_words = 0          # total number of words
  @scores = Hash.new { 0 }  #numbers of words in each category
  @words = []               #found words
  @cwords = Hash.new {nil}  #found words grouped into categories   
  @hash = {}                #additional data
end

Instance Attribute Details

#cwordsObject (readonly)

Returns the value of attribute cwords.



5
6
7
# File 'lib/text_statistics.rb', line 5

def cwords
  @cwords
end

#hashObject

Returns the value of attribute hash.



4
5
6
# File 'lib/text_statistics.rb', line 4

def hash
  @hash
end

#scoresObject (readonly)

Returns the value of attribute scores.



5
6
7
# File 'lib/text_statistics.rb', line 5

def scores
  @scores
end

#total_wordsObject

Returns the value of attribute total_words.



4
5
6
# File 'lib/text_statistics.rb', line 4

def total_words
  @total_words
end

#word_countObject (readonly)

Returns the value of attribute word_count.



5
6
7
# File 'lib/text_statistics.rb', line 5

def word_count
  @word_count
end

#wordsObject (readonly)

Returns the value of attribute words.



5
6
7
# File 'lib/text_statistics.rb', line 5

def words
  @words
end

Instance Method Details

#[](key) ⇒ Object



27
28
29
# File 'lib/text_statistics.rb', line 27

def [](key)
    @hash[key]
end

#[]=(key, value) ⇒ Object



31
32
33
# File 'lib/text_statistics.rb', line 31

def []=(key,value)
  @hash[key] = value
end

#add(word, categories) ⇒ Object

Adds word and its category to stats.



17
18
19
20
21
22
23
24
25
# File 'lib/text_statistics.rb', line 17

def add(word,categories)
  categories.each do |category|
    @cwords[category] = [] if @cwords[category].nil?
    @cwords[category].push word
    @scores[category] += 1
  end
  @words.push word
  @word_count += 1
end

#category_participation(categories) ⇒ Object



35
36
37
38
39
40
41
42
# File 'lib/text_statistics.rb', line 35

def category_participation(categories)
  sorted_scores = @scores.to_a.sort_by{ |result| -result[1] }
  r = {}
  categories.each do |cat|
    r[cat] = percentage_distribution(sorted_scores){|c| c.send(cat.to_s+'?')}
  end
  r
end