Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Ensenar
- def initialize()
- @table = {}
- end
- def add(string)
- string = string.split(/\s+|\.$/)
- while(word = string.shift)
- next if string == []
- @table[word.to_sym]||={}
- @table[word.to_sym][string[0].to_sym]||=0
- @table[word.to_sym][string[0].to_sym]+=1
- end
- return @table
- end
- def reset
- @table = {}
- end
- def generate(length=rand(20),about=nil)
- text = []
- word = @table.keys[rand(@table.keys.length)]
- text << word.to_s
- until text.length == length
- (@table[word]) ? word = (@table[word].keys.zip(@table[word].values).sort_by {|x| x[1] })[0][0].to_s : break
- text << word.to_s
- word = word.to_sym
- return text.delete_if {|x| x == ""} if text.length == 20
- break if @table[word] == {}
- end
- return text.delete_if {|x| x == ""}
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement