Class: Ensenar

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

Instance Method Summary collapse

Constructor Details

#initializeEnsenar

Returns a new instance of Ensenar.



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

def initialize()
    @table = {}
end

Instance Method Details

#add(string) ⇒ Object



5
6
7
8
9
10
11
12
13
14
# File 'lib/ensenar.rb', line 5

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

#generate(length = rand(20), about = nil) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/ensenar.rb', line 18

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

#resetObject



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

def reset
    @table = {}
end