ensenar 0.0.3 → 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/lib/ensenar.rb +62 -48
- metadata +1 -1
data/lib/ensenar.rb
CHANGED
@@ -1,57 +1,71 @@
|
|
1
|
-
class
|
2
|
-
def
|
3
|
-
|
1
|
+
class String
|
2
|
+
def sentences
|
3
|
+
((split(/\.|\?|\!/).delete_if{|x| x=~ /^\s+/}).delete_if {|x| x =~ /\s+^/ }).delete_if {|x| x == []}
|
4
4
|
end
|
5
5
|
|
6
|
-
def
|
7
|
-
|
6
|
+
def words
|
7
|
+
split(/\s+|\W\s+/).delete_if {|x| x == ""}
|
8
8
|
end
|
9
|
-
|
10
|
-
|
11
|
-
|
9
|
+
end
|
10
|
+
|
11
|
+
module Ensenar
|
12
|
+
class FrequencyTable
|
13
|
+
|
14
|
+
attr_accessor :table
|
15
|
+
|
16
|
+
def initialize()
|
17
|
+
@table = {}
|
18
|
+
end
|
19
|
+
|
20
|
+
def add(string)
|
21
|
+
string = string.split(/\s+|\.$/)
|
22
|
+
while(word = string.shift)
|
23
|
+
next if string == []
|
24
|
+
@table[word.to_sym]||={}
|
25
|
+
@table[word.to_sym][string[0].to_sym]||=0
|
26
|
+
@table[word.to_sym][string[0].to_sym]+=1
|
27
|
+
end
|
28
|
+
return @table
|
29
|
+
end
|
30
|
+
|
31
|
+
def reset()
|
32
|
+
@table = {}
|
33
|
+
end
|
12
34
|
end
|
13
35
|
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
@words.each do |sentence|
|
19
|
-
if sentence.include? trigger
|
20
|
-
has = true
|
21
|
-
break
|
22
|
-
end
|
23
|
-
end
|
24
|
-
if has
|
25
|
-
scope = trigger
|
26
|
-
text << scope
|
27
|
-
else
|
28
|
-
return nil
|
29
|
-
end
|
30
|
-
else
|
31
|
-
x = rand(@words.length)
|
32
|
-
y = rand(@words[x].length)
|
33
|
-
scope = @words[x][y]
|
34
|
-
text << @words[x][y]
|
36
|
+
class Markov
|
37
|
+
|
38
|
+
def initialize()
|
39
|
+
@frequency_table = FrequencyTable.new
|
35
40
|
end
|
36
|
-
|
37
|
-
|
38
|
-
@
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
41
|
+
|
42
|
+
def add(string)
|
43
|
+
@frequency_table.add string
|
44
|
+
end
|
45
|
+
|
46
|
+
def reset
|
47
|
+
@frequency_table.reset
|
48
|
+
end
|
49
|
+
|
50
|
+
def generate(length=rand(20),about=nil)
|
51
|
+
text = []
|
52
|
+
word = @frequency_table.table.keys[rand(@frequency_table.table.keys.length)]
|
53
|
+
text << word.to_s
|
54
|
+
until text.length == length
|
55
|
+
(@frequency_table.table[word]) ? word = (@frequency_table.table[word].keys.zip(@frequency_table.table[word].values).sort_by {|x| x[1] })[0][0].to_s : break
|
56
|
+
text << word.to_s
|
57
|
+
word = word.to_sym
|
58
|
+
return text.delete_if {|x| x == ""} if text.length == 20
|
59
|
+
break if @frequency_table.table[word] == {}
|
51
60
|
end
|
61
|
+
return text.delete_if {|x| x == ""}
|
52
62
|
end
|
53
|
-
text.delete_if {|x| x.nil? }
|
54
|
-
return text
|
55
63
|
end
|
56
|
-
|
57
|
-
|
64
|
+
end
|
65
|
+
|
66
|
+
#
|
67
|
+
# Each word
|
68
|
+
# Find sentences that have that word
|
69
|
+
# Each sentence that includes it, add to the entry for it
|
70
|
+
#
|
71
|
+
#
|