remarkovable 0.2 → 0.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/remarkovable.rb +29 -22
- data/test/{test_remarkovable.rb → test_markov_model.rb} +2 -19
- data/test/test_speak.rb +39 -0
- metadata +8 -5
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 328379d1bc17df872005ce1054d8a2a059d2f584
|
4
|
+
data.tar.gz: 385d35193fbe32e3fe5324305ac6d6daa5d1b0ba
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: d0156262c4e1cdce770660ef53e634a2a21c26f7059d4ba77318cc43d0ccbe2d387feb0a5fc7723991a8e8fbf92c1c706eed7d67f9870f4dc2b6ccd2d289bba9
|
7
|
+
data.tar.gz: 3535ab92ffe1dfb6c1d199df4db93d88f882b21d09199a8e03302c112c572c4a6e41f02c2244f6d8ae9dd091b811eec19ac8998b70576fb495a9f23706fde232
|
data/lib/remarkovable.rb
CHANGED
@@ -1,32 +1,14 @@
|
|
1
1
|
class Remarkovable
|
2
2
|
attr_accessor :markov_model
|
3
3
|
|
4
|
-
def initialize(string
|
4
|
+
def initialize(string:, prefix_length: 2)
|
5
5
|
return if string.nil?
|
6
|
-
|
7
|
-
|
8
|
-
words = string.split(/([.!?])|\s+/)
|
9
|
-
words.each_with_index do |word, i|
|
10
|
-
key = [word]
|
11
|
-
|
12
|
-
(prefix_length - 1).times do |n|
|
13
|
-
next_word = i + n + 1
|
14
|
-
key << words[next_word]
|
15
|
-
end
|
16
|
-
|
17
|
-
key = key.join(' ')
|
18
|
-
match = words[i + prefix_length]
|
19
|
-
add_triad(key: key, match: match) if i < words.size - prefix_length
|
20
|
-
end
|
6
|
+
build_markov_model(string, prefix_length)
|
21
7
|
end
|
22
8
|
|
23
9
|
def speak(custom_key: nil)
|
24
10
|
return if @markov_model.nil?
|
25
|
-
key =
|
26
|
-
custom_key
|
27
|
-
else
|
28
|
-
@markov_model.keys.sample
|
29
|
-
end
|
11
|
+
key = assign_key(custom_key)
|
30
12
|
output = Array(key.capitalize.split(' '))
|
31
13
|
|
32
14
|
until (output & %w(. ! ?)).any?
|
@@ -45,7 +27,32 @@ class Remarkovable
|
|
45
27
|
|
46
28
|
private
|
47
29
|
|
48
|
-
def
|
30
|
+
def build_markov_model(string, prefix_length)
|
31
|
+
@markov_model = Hash.new { |hash, key| hash[key] = [] }
|
32
|
+
words = string.split(/([.!?])|\s+/)
|
33
|
+
words.each_with_index do |word, i|
|
34
|
+
key = [word]
|
35
|
+
|
36
|
+
(prefix_length - 1).times do |n|
|
37
|
+
next_word = i + n + 1
|
38
|
+
key << words[next_word]
|
39
|
+
end
|
40
|
+
|
41
|
+
key = key.join(' ')
|
42
|
+
match = words[i + prefix_length]
|
43
|
+
add_triad(key: key, match: match) if i < words.size - prefix_length
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
def assign_key(custom_key)
|
48
|
+
if !custom_key.nil? && @markov_model.include?(custom_key)
|
49
|
+
custom_key
|
50
|
+
else
|
51
|
+
@markov_model.keys.sample
|
52
|
+
end
|
53
|
+
end
|
54
|
+
|
55
|
+
def add_triad(key:, match:)
|
49
56
|
@markov_model[key] = {} unless @markov_model[key]
|
50
57
|
@markov_model[key] += [match]
|
51
58
|
end
|
@@ -1,7 +1,7 @@
|
|
1
1
|
require 'minitest/autorun'
|
2
2
|
require './lib/remarkovable.rb'
|
3
3
|
|
4
|
-
class
|
4
|
+
class TestMarkovModel < Minitest::Test
|
5
5
|
def test_add_pair_simple_case
|
6
6
|
string = 'we are walking'
|
7
7
|
mc = Remarkovable.new(string: string)
|
@@ -13,9 +13,7 @@ class RemarkovableTest < Minitest::Test
|
|
13
13
|
end
|
14
14
|
|
15
15
|
def test_add_pair_nil
|
16
|
-
|
17
|
-
mc = Remarkovable.new(string: string)
|
18
|
-
expected_output = nil
|
16
|
+
mc = Remarkovable.new(string: nil)
|
19
17
|
|
20
18
|
assert_equal nil, mc.speak
|
21
19
|
end
|
@@ -121,19 +119,4 @@ class RemarkovableTest < Minitest::Test
|
|
121
119
|
|
122
120
|
assert_equal expected_output, mc.markov_model
|
123
121
|
end
|
124
|
-
|
125
|
-
def test_custom_key
|
126
|
-
string = "we are walking\n\rwe are talking we are walking."
|
127
|
-
mc = Remarkovable.new(string: string)
|
128
|
-
expected_output = /^We are./
|
129
|
-
|
130
|
-
assert_match expected_output, mc.speak(custom_key: 'we are')
|
131
|
-
end
|
132
|
-
|
133
|
-
def test_custom_key_no_match
|
134
|
-
string = "we are walking\n\rwe are talking we are walking."
|
135
|
-
mc = Remarkovable.new(string: string)
|
136
|
-
|
137
|
-
assert_nil mc.speak(custom_key: 'foo bar') =~ /Foo bar/i
|
138
|
-
end
|
139
122
|
end
|
data/test/test_speak.rb
ADDED
@@ -0,0 +1,39 @@
|
|
1
|
+
require 'minitest/autorun'
|
2
|
+
require './lib/remarkovable.rb'
|
3
|
+
|
4
|
+
class TestSpeak < Minitest::Test
|
5
|
+
def test_period_ending
|
6
|
+
string = 'we are walking.'
|
7
|
+
mc = Remarkovable.new(string: string)
|
8
|
+
|
9
|
+
assert_match(/\A.*walking.*\.\z/, mc.speak)
|
10
|
+
end
|
11
|
+
|
12
|
+
def test_bang_ending
|
13
|
+
string = 'we are walking!'
|
14
|
+
mc = Remarkovable.new(string: string)
|
15
|
+
|
16
|
+
assert_match(/\A.*walking.*!\z/, mc.speak)
|
17
|
+
end
|
18
|
+
|
19
|
+
def test_question_mark_ending
|
20
|
+
string = 'we are walking?'
|
21
|
+
mc = Remarkovable.new(string: string)
|
22
|
+
|
23
|
+
assert_match(/\A.*walking.*\?\z/, mc.speak)
|
24
|
+
end
|
25
|
+
|
26
|
+
def test_custom_key
|
27
|
+
string = "we are walking\n\rwe are talking we are walking."
|
28
|
+
mc = Remarkovable.new(string: string)
|
29
|
+
|
30
|
+
assert_match(/^We are./, mc.speak(custom_key: 'we are'))
|
31
|
+
end
|
32
|
+
|
33
|
+
def test_custom_key_no_match
|
34
|
+
string = "we are walking\n\rwe are talking we are walking."
|
35
|
+
mc = Remarkovable.new(string: string)
|
36
|
+
|
37
|
+
assert_nil(mc.speak(custom_key: 'foo bar') =~ /Foo bar/i)
|
38
|
+
end
|
39
|
+
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: remarkovable
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: '0.
|
4
|
+
version: '0.3'
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Jake Worth
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-
|
11
|
+
date: 2015-11-04 00:00:00.000000000 Z
|
12
12
|
dependencies: []
|
13
13
|
description: A gem that produces Markov chain output from any text.
|
14
14
|
email:
|
@@ -18,7 +18,8 @@ extensions: []
|
|
18
18
|
extra_rdoc_files: []
|
19
19
|
files:
|
20
20
|
- lib/remarkovable.rb
|
21
|
-
- test/
|
21
|
+
- test/test_markov_model.rb
|
22
|
+
- test/test_speak.rb
|
22
23
|
homepage: https://rubygems.org/gems/remarkovable
|
23
24
|
licenses:
|
24
25
|
- MIT
|
@@ -39,9 +40,11 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
39
40
|
version: '0'
|
40
41
|
requirements: []
|
41
42
|
rubyforge_project:
|
42
|
-
rubygems_version: 2.4.
|
43
|
+
rubygems_version: 2.4.8
|
43
44
|
signing_key:
|
44
45
|
specification_version: 4
|
45
46
|
summary: Markov chains for all.
|
46
47
|
test_files:
|
47
|
-
- test/
|
48
|
+
- test/test_speak.rb
|
49
|
+
- test/test_markov_model.rb
|
50
|
+
has_rdoc:
|