Class: WordsCounted::Counter

Inherits:
Object
  • Object
show all
Includes:
Deprecated
Defined in:
lib/words_counted/counter.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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.

Parameters:

  • An (Array)

    array of tokens to perform operations on



21
22
23
# File 'lib/words_counted/counter.rb', line 21

def initialize(tokens)
  @tokens = tokens
end

Instance Attribute Details

#tokensArray<String> (readonly)

Returns an array of tokens.

Returns:

  • (Array<String>)

    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.

Examples:

Counter.new(%w[one three five seven]).average_chars_per_token
# => 4.25

Parameters:

  • precision (Integer) (defaults to: 2)

    The number of decimal places to round average char count to

Returns:

  • (Float)

    The average char count per token



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_countInteger

Returns the character count of all tokens.

Examples:

Counter.new(%w[one two]).char_count
# => 6

Returns:

  • (Integer)

    The total char count of tokens



54
55
56
# File 'lib/words_counted/counter.rb', line 54

def char_count
  tokens.join.size
end

#longest_tokensHash{String => Integer}

Returns a hash of tokens and their lengths for tokens with the highest length

Examples:

Counter.new(%w[one three five seven]).longest_tokens
# => { 'three' => 5, 'seven' => 5 }

Returns:

  • (Hash{String => Integer})

    A hash of tokens and their lengths



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_tokensHash{String => Integer}

Returns a hash of tokens and their frequencies for tokens with the highest frequency.

Examples:

Counter.new(%w[one once two two twice twice]).most_frequent_tokens
# => { 'two' => 2, 'twice' => 2 }

Returns:

  • (Hash{String => Integer})

    A hash of tokens and their frequencies



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_countInteger

Returns the number of tokens.

Examples:

Counter.new(%w[one two two three three three]).token_count
# => 6

Returns:

  • (Integer)

    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.

Examples:

Counter.new(%w[Maj. Major Major Major]).token_density
# => [ ['major', .75], ['maj', .25] ]

with precision

Counter.new(%w[Maj. Major Major Major]).token_density(precision: 4)
# => [ ['major', .7500], ['maj', .2500] ]

Parameters:

  • precision (Integer) (defaults to: 2)

    The number of decimal places to round density to

Returns:

  • (Array<Array<String, Float>>)

    An array of tokens and their densities



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_frequencyArray<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.

Examples:

Counter.new(%w[one two two three three three]).token_frequency
# => [ ['three', 3], ['two', 2], ['one', 1] ]

Returns:

  • (Array<Array<String, Integer>>)

    An array of tokens and their frequencies



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_lengthsArray<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.

Examples:

Counter.new(%w[one two three four five]).token_lenghts
# => [ ['three', 5], ['four', 4], ['five', 4], ['one', 3], ['two', 3] ]

Returns:

  • (Array<Array<String, Integer>>)

    An array of tokens and their lengths



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_countInteger

Returns the number of unique tokens.

Examples:

Counter.new(%w[one two two three three three]).uniq_token_count
# => 3

Returns:

  • (Integer)

    The number of unique tokens



43
44
45
# File 'lib/words_counted/counter.rb', line 43

def uniq_token_count
  tokens.uniq.size
end