rubykov 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/README.md +40 -0
- data/lib/rubykov/markov_model.rb +48 -0
- data/lib/rubykov/text_generator.rb +19 -0
- data/lib/rubykov.rb +2 -0
- data/license.md +21 -0
- metadata +68 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 1bdb3e4295782a7bf03967d2f4de67aef65fa64b
|
4
|
+
data.tar.gz: 16e45e2b424bb261476afc2dae7619af27f9bcb6
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 3570f5ad7f21b459e7f31575b08b4dd27e799189c081b54352bdc2d0bdf5ff04d0672efb664896eec00787951394cc5953c151cb659f7e87b1269ff7dee71a3f
|
7
|
+
data.tar.gz: 8b0fd9ba3513aaf47e58e66bd0f7a252df8a529c29cd45f43ea00547b5782784a502c8deadaed6af757ca1ad4f80f1315378fe22b72a6733fcfaed20f1a885d3
|
data/README.md
ADDED
@@ -0,0 +1,40 @@
|
|
1
|
+
### Rubykov
|
2
|
+
|
3
|
+
Sensible, easy Markov chains in Ruby.
|
4
|
+
|
5
|
+
## Usage
|
6
|
+
|
7
|
+
Markov models are generated by an order and a set of training data.
|
8
|
+
|
9
|
+
For example, to create a first-order Markov model given some weather data:
|
10
|
+
|
11
|
+
```
|
12
|
+
markov_model = Rubykov::MarkovModel.new(1, ['sunny','rainy','sunny','sunny','sunny','windy','sunny','rainy','windy','rainy'])
|
13
|
+
```
|
14
|
+
|
15
|
+
To obtain predictions, use the chain method, which returns an enumerator:
|
16
|
+
|
17
|
+
```
|
18
|
+
markov_chain = markov_model.chain
|
19
|
+
markov_chain.next
|
20
|
+
=> "sunny"
|
21
|
+
```
|
22
|
+
|
23
|
+
There is a text generator subclass which has useful methods for outputting character- and word-limited strings.
|
24
|
+
The following is a second-order Markov model:
|
25
|
+
|
26
|
+
```
|
27
|
+
text_generator = Rubykov::TextGenerator.new(2, 'The quick brown fox jumps over the brown fox who is slow jumps over the brown fox who is dead.')
|
28
|
+
text_generator.character_limited_output(140)
|
29
|
+
=> "The quick brown fox who is dead."
|
30
|
+
```
|
31
|
+
|
32
|
+
## Testing
|
33
|
+
|
34
|
+
To run the test suite simply execute:
|
35
|
+
|
36
|
+
```
|
37
|
+
rspec
|
38
|
+
```
|
39
|
+
|
40
|
+
(c) Evan Hemsley 2014
|
@@ -0,0 +1,48 @@
|
|
1
|
+
module Rubykov
|
2
|
+
class MarkovModel
|
3
|
+
def initialize(order, training_data)
|
4
|
+
raise ArgumentError unless order.is_a? Integer
|
5
|
+
raise ArgumentError unless training_data.is_a? Array
|
6
|
+
chain_data = {}.tap do |representation|
|
7
|
+
training_data.each_cons(order + 1).each do |datum|
|
8
|
+
key = datum.first(order)
|
9
|
+
value = datum.last
|
10
|
+
if representation.include? key
|
11
|
+
representation[key] << value
|
12
|
+
else
|
13
|
+
representation[key] = [value]
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
17
|
+
@order = order
|
18
|
+
@representation = chain_data
|
19
|
+
end
|
20
|
+
|
21
|
+
def chain
|
22
|
+
Enumerator.new do |y|
|
23
|
+
current_state = @representation.keys.sample
|
24
|
+
current_state.each do |word|
|
25
|
+
y << word
|
26
|
+
end
|
27
|
+
|
28
|
+
loop do
|
29
|
+
if @representation[current_state].nil?
|
30
|
+
break
|
31
|
+
else
|
32
|
+
next_word = @representation[current_state].sample
|
33
|
+
y << next_word
|
34
|
+
current_state = current_state.last(@order-1) + [next_word]
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
def states
|
41
|
+
@representation.keys
|
42
|
+
end
|
43
|
+
|
44
|
+
def transitions
|
45
|
+
@representation
|
46
|
+
end
|
47
|
+
end
|
48
|
+
end
|
@@ -0,0 +1,19 @@
|
|
1
|
+
module Rubykov
|
2
|
+
class TextGenerator < MarkovModel
|
3
|
+
def initialize(order, training_text)
|
4
|
+
super(order, training_text.downcase.gsub('.', ' .').split(' '))
|
5
|
+
end
|
6
|
+
|
7
|
+
def character_limited_output(desired_length)
|
8
|
+
length = 0
|
9
|
+
chain.take_while do |word|
|
10
|
+
length += (word.length + 1)
|
11
|
+
length < desired_length
|
12
|
+
end.join(' ').gsub(' .', '.').capitalize
|
13
|
+
end
|
14
|
+
|
15
|
+
def word_limited_output(desired_length)
|
16
|
+
chain.take(desired_length).join(' ').gsub(' .', '.').capitalize
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
data/lib/rubykov.rb
ADDED
data/license.md
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) <2014> <Evan Hemsley>
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
metadata
ADDED
@@ -0,0 +1,68 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: rubykov
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Evan Hemsley
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2014-12-10 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: rspec
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: 3.1.0
|
20
|
+
- - ">="
|
21
|
+
- !ruby/object:Gem::Version
|
22
|
+
version: 3.1.0
|
23
|
+
type: :development
|
24
|
+
prerelease: false
|
25
|
+
version_requirements: !ruby/object:Gem::Requirement
|
26
|
+
requirements:
|
27
|
+
- - "~>"
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: 3.1.0
|
30
|
+
- - ">="
|
31
|
+
- !ruby/object:Gem::Version
|
32
|
+
version: 3.1.0
|
33
|
+
description: This gem defines Markov model-related classes.
|
34
|
+
email: evan.hemsley@gmail.com
|
35
|
+
executables: []
|
36
|
+
extensions: []
|
37
|
+
extra_rdoc_files: []
|
38
|
+
files:
|
39
|
+
- README.md
|
40
|
+
- lib/rubykov.rb
|
41
|
+
- lib/rubykov/markov_model.rb
|
42
|
+
- lib/rubykov/text_generator.rb
|
43
|
+
- license.md
|
44
|
+
homepage: https://gitlab.com/ehemsley/rubykov
|
45
|
+
licenses:
|
46
|
+
- MIT
|
47
|
+
metadata: {}
|
48
|
+
post_install_message:
|
49
|
+
rdoc_options: []
|
50
|
+
require_paths:
|
51
|
+
- lib
|
52
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
53
|
+
requirements:
|
54
|
+
- - ">="
|
55
|
+
- !ruby/object:Gem::Version
|
56
|
+
version: '0'
|
57
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - ">="
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
requirements: []
|
63
|
+
rubyforge_project:
|
64
|
+
rubygems_version: 2.2.2
|
65
|
+
signing_key:
|
66
|
+
specification_version: 4
|
67
|
+
summary: Sensible, easy Markov models in Ruby.
|
68
|
+
test_files: []
|