ensenar 0.0.3 → 0.1.0

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 +62 -48
  2. metadata +1 -1
data/lib/ensenar.rb CHANGED
@@ -1,57 +1,71 @@
1
- class Ensenar
2
- def initialize
3
- @words = []
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 reset
7
- @words = []
6
+ def words
7
+ split(/\s+|\W\s+/).delete_if {|x| x == ""}
8
8
  end
9
-
10
- def add(sentence)
11
- @words.push sentence.split(/\W+/)
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
- def generate(length, trigger=nil)
15
- text = []
16
- if trigger
17
- has = false
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
- while(text.length < length)
37
- l = text.length
38
- @words.rotate(rand(20)).each do |sentence|
39
- if sentence.include? text.last
40
- pos = sentence.find_index text.last
41
- text << sentence[pos + 1]
42
- end
43
- end
44
- if l == text.length
45
- x = rand(@words.length)
46
- y = rand(@words[x].length)
47
- scope = @words[x][y]
48
- text << @words[x][y]
49
- else
50
- next
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
- end
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
+ #
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.0.3
4
+ version: 0.1.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors: