ensenar 0.1.0 → 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (2) hide show
  1. data/lib/ensenar.rb +24 -64
  2. metadata +1 -1
data/lib/ensenar.rb CHANGED
@@ -1,71 +1,31 @@
1
- class String
2
- def sentences
3
- ((split(/\.|\?|\!/).delete_if{|x| x=~ /^\s+/}).delete_if {|x| x =~ /\s+^/ }).delete_if {|x| x == []}
1
+ class Ensenar
2
+ def initialize()
3
+ @table = {}
4
4
  end
5
-
6
- def words
7
- split(/\s+|\W\s+/).delete_if {|x| x == ""}
8
- end
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 = {}
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
- class Markov
37
-
38
- def initialize()
39
- @frequency_table = FrequencyTable.new
40
- end
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)]
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
- 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] == {}
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
- #
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ensenar
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.1
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors: