pjaspers (owner)

Forks

Revisions

  • f990dd Tue Nov 24 04:40:35 -0800 2009
gist: 241847 Download_button fork
public
Description:
A class to generate a Haiku in ruby (prototype for haikuherman.eu)
Public Clone URL: git://gist.github.com/241847.git
Embed All Files: show embed
Ruby #
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
require 'lingua/en/readability'
class Haiku
  def initialize(text)
  @report = Lingua::EN::Readability.new(text)
  @used_words = Array.new
  end
  
  def get_most_used_words(words)
    top_words = Array.new
    words.sort{|a,b| a[1]<=>b[1]}.slice(words.length/2..words.length).each do |a|
      top_words << a[0].downcase
    end
    return top_words
  end
  
  def get_random_word(words, max_syll)
    word = words[rand(words.length)]
    if Lingua::EN::Readability.new(word).num_syllables > max_syll or @used_words.include?(word)
      word = get_random_word(words, max_syll)
    end
    return word
  end
 
  def generate_line(number_of_syllables)
    most_used_words = get_most_used_words(@report.frequencies)
    line = ""
    n_syll = 0
    while n_syll < number_of_syllables
      max_syll = number_of_syllables - n_syll
      word = get_random_word(most_used_words, max_syll)
      @used_words << word
      word_syll = Lingua::EN::Readability.new(word).num_syllables
      n_syll += word_syll
      (line.length == 0 ? line << "#{word}": line << " #{word}")
    end
    return line << "(#{n_syll})"
  end
  
  def print_report
    puts @report.report
    puts get_most_used_words(@report.frequencies)
  end
  def print
    puts generate_line(5).capitalize
    puts generate_line(7)
    puts generate_line(5)
  end
end