Class: String
- Inherits:
-
Object
- Object
- String
- Defined in:
- lib/textstats.rb
Instance Method Summary collapse
- #ari ⇒ Object
- #avg_sentence_length ⇒ Object
- #avg_word_length ⇒ Object
- #clf ⇒ Object
- #long_words(size = 6) ⇒ Object
- #punct_ratio ⇒ Object
- #puncts ⇒ Object
- #sentences ⇒ Object
- #type_token_ratio(downcase = false) ⇒ Object
- #word_freqs ⇒ Object
- #words ⇒ Object
Instance Method Details
#ari ⇒ Object
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_length ⇒ Object
15 16 17 |
# File 'lib/textstats.rb', line 15 def avg_sentence_length Float(words.size/sentences) end |
#avg_word_length ⇒ Object
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 |
#clf ⇒ Object
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_ratio ⇒ Object
23 24 25 |
# File 'lib/textstats.rb', line 23 def punct_ratio Float(puncts.size)/size end |
#puncts ⇒ Object
19 20 21 |
# File 'lib/textstats.rb', line 19 def puncts scan(/(\s-\s|,|;|\(|\)|\")/) end |
#sentences ⇒ Object
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_freqs ⇒ Object
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 |
#words ⇒ Object
2 3 4 |
# File 'lib/textstats.rb', line 2 def words scan(/\w[\w\'\-]*/) end |