Advertisement
swarley

Ensenar.rb

Mar 11th, 2012
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 0.96 KB | None | 0 0
  1. class Ensenar
  2.     def initialize()
  3.         @table = {}
  4.     end
  5.     def add(string)
  6.         string = string.split(/\s+|\.$/)
  7.         while(word = string.shift)
  8.             next if string == []
  9.             @table[word.to_sym]||={}
  10.             @table[word.to_sym][string[0].to_sym]||=0
  11.             @table[word.to_sym][string[0].to_sym]+=1
  12.         end
  13.         return @table
  14.     end
  15.     def reset
  16.         @table = {}
  17.     end
  18.     def generate(length=rand(20),about=nil)
  19.         text = []
  20.         word = @table.keys[rand(@table.keys.length)]
  21.         text << word.to_s
  22.         until text.length == length
  23.             (@table[word]) ? word = (@table[word].keys.zip(@table[word].values).sort_by {|x| x[1] })[0][0].to_s : break
  24.             text << word.to_s
  25.             word = word.to_sym
  26.             return text.delete_if {|x| x == ""} if text.length == 20
  27.             break if @table[word] == {}
  28.         end
  29.         return text.delete_if {|x| x == ""}
  30.     end
  31. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement