Class: WordsCounted::Counter
- Inherits:
-
Object
- Object
- WordsCounted::Counter
- Includes:
- Deprecated
- Defined in:
- lib/words_counted/counter.rb
Instance Attribute Summary collapse
-
#tokens ⇒ Array<String>
readonly
An array of tokens.
Instance Method Summary collapse
-
#average_chars_per_token(precision: 2) ⇒ Float
Returns the average char count per token rounded to a precision of two decimal places.
-
#char_count ⇒ Integer
Returns the character count of all tokens.
-
#initialize(tokens) ⇒ Counter
constructor
Initializes state with an array of tokens.
-
#longest_tokens ⇒ Hash{String => Integer}
Returns a hash of tokens and their lengths for tokens with the highest length.
-
#most_frequent_tokens ⇒ Hash{String => Integer}
Returns a hash of tokens and their frequencies for tokens with the highest frequency.
-
#token_count ⇒ Integer
Returns the number of tokens.
-
#token_density(precision: 2) ⇒ Array<Array<String, Float>>
Returns a sorted two-dimensional array where each member array is a token and its density as a float, rounded to a precision of two decimal places.
-
#token_frequency ⇒ Array<Array<String, Integer>>
Returns a sorted two-dimensional array where each member array is a token and its frequency.
-
#token_lengths ⇒ Array<Array<String, Integer>>
Returns a sorted two-dimensional array where each member array is a token and its length.
-
#uniq_token_count ⇒ Integer
Returns the number of unique tokens.
Methods included from Deprecated
#average_chars_per_word, #count, #longest_words, #most_occurring_words, #sorted_word_lengths, #sorted_word_occurrences, #unique_word_count, #word_count, #word_density, #word_lengths, #word_occurrences
Constructor Details
#initialize(tokens) ⇒ Counter
Initializes state with an array of tokens.
21 22 23 |
# File 'lib/words_counted/counter.rb', line 21 def initialize(tokens) @tokens = tokens end |
Instance Attribute Details
#tokens ⇒ Array<String> (readonly)
Returns an array of tokens.
16 17 18 |
# File 'lib/words_counted/counter.rb', line 16 def tokens @tokens end |
Instance Method Details
#average_chars_per_token(precision: 2) ⇒ Float
Returns the average char count per token rounded to a precision of two decimal places.
Accepts a precision
argument.
133 134 135 |
# File 'lib/words_counted/counter.rb', line 133 def average_chars_per_token(precision: 2) (char_count / token_count.to_f).round(precision) end |
#char_count ⇒ Integer
Returns the character count of all tokens.
54 55 56 |
# File 'lib/words_counted/counter.rb', line 54 def char_count tokens.join.size end |
#longest_tokens ⇒ Hash{String => Integer}
Returns a hash of tokens and their lengths for tokens with the highest length
120 121 122 |
# File 'lib/words_counted/counter.rb', line 120 def longest_tokens token_lengths.group_by(&:last).max.last.to_h end |
#most_frequent_tokens ⇒ Hash{String => Integer}
Returns a hash of tokens and their frequencies for tokens with the highest frequency.
109 110 111 |
# File 'lib/words_counted/counter.rb', line 109 def most_frequent_tokens token_frequency.group_by(&:last).max.last.to_h end |
#token_count ⇒ Integer
Returns the number of tokens.
32 33 34 |
# File 'lib/words_counted/counter.rb', line 32 def token_count tokens.size end |
#token_density(precision: 2) ⇒ Array<Array<String, Float>>
Returns a sorted two-dimensional array where each member array is a token and its density
as a float, rounded to a precision of two decimal places. It accepts a precision argument
which defaults to 2
.
96 97 98 99 100 |
# File 'lib/words_counted/counter.rb', line 96 def token_density(precision: 2) token_frequency.each_with_object({}) { |(token, freq), hash| hash[token] = (freq / token_count.to_f).round(precision) }.sort_by_value_desc end |
#token_frequency ⇒ Array<Array<String, Integer>>
Returns a sorted two-dimensional array where each member array is a token and its frequency. The array is sorted by frequency in descending order.
66 67 68 |
# File 'lib/words_counted/counter.rb', line 66 def token_frequency tokens.each_with_object(Hash.new(0)) { |token, hash| hash[token] += 1 }.sort_by_value_desc end |
#token_lengths ⇒ Array<Array<String, Integer>>
Returns a sorted two-dimensional array where each member array is a token and its length. The array is sorted by length in descending order.
78 79 80 |
# File 'lib/words_counted/counter.rb', line 78 def token_lengths tokens.uniq.each_with_object({}) { |token, hash| hash[token] = token.length }.sort_by_value_desc end |
#uniq_token_count ⇒ Integer
Returns the number of unique tokens.
43 44 45 |
# File 'lib/words_counted/counter.rb', line 43 def uniq_token_count tokens.uniq.size end |