Module: WordStats::Characters

Defined in:
lib/word_stats.rb

Class Method Summary collapse

Class Method Details

.bigrams(text) ⇒ Object

Computes the bigram frequency of the input string



18
19
20
# File 'lib/word_stats.rb', line 18

def self.bigrams(text)
  ngrams(2,text)
end

.letters(text) ⇒ Object

Computes a letter frequency distribution of the input string



12
13
14
# File 'lib/word_stats.rb', line 12

def self.letters(text)
  ngrams(1,text)
end

.ngrams(n, text) ⇒ Object

Compute the N-gram frequency of the input string



30
31
32
33
34
35
36
37
# File 'lib/word_stats.rb', line 30

def self.ngrams(n,text)
  text = text.remove_punctuation.downcase
  ngrams = Hash.new(0)
  for i in n-1..(text.length-1)
    ngrams[(text[i-(n-1)..i]).to_sym] += 1
  end
  ngrams
end

.trigrams(text) ⇒ Object

Computes the trigram frequency of the input string



24
25
26
# File 'lib/word_stats.rb', line 24

def self.trigrams(text)
  ngrams(3,text)
end