Class: String

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

Instance Method Summary collapse

Instance Method Details

#ariObject



49
50
51
# File 'lib/textstats.rb', line 49

def ari
  (4.71 * avg_word_length) + (0.5 * avg_sentence_length) - 21.43
end

#avg_sentence_lengthObject



15
16
17
# File 'lib/textstats.rb', line 15

def avg_sentence_length
  Float(words.size/sentences)
end

#avg_word_lengthObject



6
7
8
# File 'lib/textstats.rb', line 6

def avg_word_length
  Float(words.collect{|w|w.size}.inject(nil){|sum,x|sum ? sum + x : x})/words.size
end

#clfObject



53
54
55
# File 'lib/textstats.rb', line 53

def clf
  (5.89 * avg_word_length) - (0.4 * 100.0/avg_sentence_length) - 15.8
end

#long_words(size = 6) ⇒ Object



43
44
45
46
47
# File 'lib/textstats.rb', line 43

def long_words(size=6)
  wl = []
  words.each{|w|wl << w if w.size >= size}
  wl
end

#punct_ratioObject



23
24
25
# File 'lib/textstats.rb', line 23

def punct_ratio
  Float(puncts.size)/size
end

#punctsObject



19
20
21
# File 'lib/textstats.rb', line 19

def puncts
  scan(/(\s-\s|,|;|\(|\)|\")/)
end

#sentencesObject



10
11
12
13
# File 'lib/textstats.rb', line 10

def sentences
  out = scan(/(\.|\?|\!|:)(\s+[A-Z]|$)/).size
  (out > 0) ? out : 1
end

#type_token_ratio(downcase = false) ⇒ Object



27
28
29
30
31
32
33
34
# File 'lib/textstats.rb', line 27

def type_token_ratio(downcase=false)
  if downcase
    w = words.map{|w|w.downcase}
  else
    w = words
  end
  Float(w.uniq.size)/w.size
end

#word_freqsObject



36
37
38
39
40
# File 'lib/textstats.rb', line 36

def word_freqs
  wl = Hash.new(0)
  words.each{|w|wl[w] +=1}
  wl.sort{|a,b| a[1]<=>b[1]}.reverse
end

#wordsObject



2
3
4
# File 'lib/textstats.rb', line 2

def words
  scan(/\w[\w\'\-]*/)
end