Class: NLP::TextStatistics
- Inherits:
-
Object
- Object
- NLP::TextStatistics
- Defined in:
- lib/text_statistics.rb
Instance Attribute Summary collapse
-
#cwords ⇒ Object
readonly
Returns the value of attribute cwords.
-
#hash ⇒ Object
Returns the value of attribute hash.
-
#scores ⇒ Object
readonly
Returns the value of attribute scores.
-
#total_words ⇒ Object
Returns the value of attribute total_words.
-
#word_count ⇒ Object
readonly
Returns the value of attribute word_count.
-
#words ⇒ Object
readonly
Returns the value of attribute words.
Instance Method Summary collapse
- #[](key) ⇒ Object
- #[]=(key, value) ⇒ Object
-
#add(word, categories) ⇒ Object
Adds word and its category to stats.
- #category_participation(categories) ⇒ Object
-
#initialize ⇒ TextStatistics
constructor
A new instance of TextStatistics.
Constructor Details
#initialize ⇒ TextStatistics
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
#cwords ⇒ Object (readonly)
Returns the value of attribute cwords.
5 6 7 |
# File 'lib/text_statistics.rb', line 5 def cwords @cwords end |
#hash ⇒ Object
Returns the value of attribute hash.
4 5 6 |
# File 'lib/text_statistics.rb', line 4 def hash @hash end |
#scores ⇒ Object (readonly)
Returns the value of attribute scores.
5 6 7 |
# File 'lib/text_statistics.rb', line 5 def scores @scores end |
#total_words ⇒ Object
Returns the value of attribute total_words.
4 5 6 |
# File 'lib/text_statistics.rb', line 4 def total_words @total_words end |
#word_count ⇒ Object (readonly)
Returns the value of attribute word_count.
5 6 7 |
# File 'lib/text_statistics.rb', line 5 def word_count @word_count end |
#words ⇒ Object (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 |