ensenar 0.1.0 → 0.1.1
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 +24 -64
- metadata +1 -1
data/lib/ensenar.rb
CHANGED
@@ -1,71 +1,31 @@
|
|
1
|
-
class
|
2
|
-
def
|
3
|
-
|
1
|
+
class Ensenar
|
2
|
+
def initialize()
|
3
|
+
@table = {}
|
4
4
|
end
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
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 = {}
|
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
|
33
12
|
end
|
13
|
+
return @table
|
34
14
|
end
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
@
|
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)]
|
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
|
53
24
|
text << word.to_s
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
word = word.to_sym
|
58
|
-
return text.delete_if {|x| x == ""} if text.length == 20
|
59
|
-
break if @frequency_table.table[word] == {}
|
60
|
-
end
|
61
|
-
return text.delete_if {|x| x == ""}
|
25
|
+
word = word.to_sym
|
26
|
+
return text.delete_if {|x| x == ""} if text.length == 20
|
27
|
+
break if @table[word] == {}
|
62
28
|
end
|
29
|
+
return text.delete_if {|x| x == ""}
|
63
30
|
end
|
64
31
|
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
|
-
#
|