more_markov 0.1.0
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.
- checksums.yaml +7 -0
- data/.gitignore +9 -0
- data/.travis.yml +4 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +21 -0
- data/README.md +29 -0
- data/Rakefile +1 -0
- data/bin/console +14 -0
- data/bin/setup +7 -0
- data/lib/more_markov/version.rb +3 -0
- data/lib/more_markov.rb +57 -0
- data/more_markov.gemspec +31 -0
- metadata +84 -0
checksums.yaml
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
---
|
|
2
|
+
SHA1:
|
|
3
|
+
metadata.gz: d6dfdad27963db7f030bb9885eba2eb3ade2df44
|
|
4
|
+
data.tar.gz: 06fb87fe85e07d5fca57f7062766aed0c42d6c2a
|
|
5
|
+
SHA512:
|
|
6
|
+
metadata.gz: 68aa6fc7bad7b48624b5063d76b69a6a64cf0629f46401ce7c9c84c657bc34ae31df9d17c72a7e474f3e23f9804afaae56ca3cbb3a8c89a39ed57ac0d5ec9c3c
|
|
7
|
+
data.tar.gz: 6f8f57643e3aaa5a49402c9d06b14f912991f13083e2958af09fcd03f36d586e380cee3b0d5ae6599acc84eb8bd50a5eea9c4cf7cbda36938b68107b56daffc8
|
data/.gitignore
ADDED
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
The MIT License (MIT)
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2016 nathanielltaylor
|
|
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.
|
data/README.md
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
# MoreMarkov
|
|
2
|
+
|
|
3
|
+
Simple Markov chain generation.
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
Add this line to your application's Gemfile:
|
|
8
|
+
|
|
9
|
+
```ruby
|
|
10
|
+
gem 'more_markov'
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
And then execute:
|
|
14
|
+
|
|
15
|
+
$ bundle
|
|
16
|
+
|
|
17
|
+
Or install it yourself as:
|
|
18
|
+
|
|
19
|
+
$ gem install more_markov
|
|
20
|
+
|
|
21
|
+
## Usage
|
|
22
|
+
```ruby
|
|
23
|
+
your_generator = MoreMarkov::MarkovChainGenerator.new(source_text_as_string)
|
|
24
|
+
your_generator.find_patterns
|
|
25
|
+
your_generator.generate_chain(word_count, optional_start_word) #=> Markov chain
|
|
26
|
+
```
|
|
27
|
+
## License
|
|
28
|
+
|
|
29
|
+
The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
|
data/Rakefile
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
require "bundler/gem_tasks"
|
data/bin/console
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
#!/usr/bin/env ruby
|
|
2
|
+
|
|
3
|
+
require "bundler/setup"
|
|
4
|
+
require "more_markov"
|
|
5
|
+
|
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
|
8
|
+
|
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
|
10
|
+
# require "pry"
|
|
11
|
+
# Pry.start
|
|
12
|
+
|
|
13
|
+
require "irb"
|
|
14
|
+
IRB.start
|
data/bin/setup
ADDED
data/lib/more_markov.rb
ADDED
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
require "more_markov/version"
|
|
2
|
+
|
|
3
|
+
module MoreMarkov
|
|
4
|
+
def self.weighted_choice(hash)
|
|
5
|
+
total = hash.inject(0) { |sum, item_and_weight| sum += item_and_weight[1] }
|
|
6
|
+
target = rand(total)
|
|
7
|
+
hash.each do |item, weight|
|
|
8
|
+
return item if target <= weight
|
|
9
|
+
target -= weight
|
|
10
|
+
end
|
|
11
|
+
end
|
|
12
|
+
|
|
13
|
+
class MarkovChainGenerator
|
|
14
|
+
attr_accessor :text, :words, :frequencies, :sequenced
|
|
15
|
+
|
|
16
|
+
def initialize(text)
|
|
17
|
+
@text = text
|
|
18
|
+
@words = text.split
|
|
19
|
+
@frequencies = {}
|
|
20
|
+
@sequenced = false
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
def find_patterns
|
|
24
|
+
@words.each_with_index do |word, index|
|
|
25
|
+
if index < (@words.length - 1)
|
|
26
|
+
if !@frequencies.include?(word)
|
|
27
|
+
@frequencies[word] = {}
|
|
28
|
+
@frequencies[word][@words[index + 1]] = 1
|
|
29
|
+
elsif !@frequencies[word].include?(@words[index + 1])
|
|
30
|
+
@frequencies[word][@words[index + 1]] = 1
|
|
31
|
+
else
|
|
32
|
+
@frequencies[word][@words[index + 1]] += 1
|
|
33
|
+
end
|
|
34
|
+
end
|
|
35
|
+
end
|
|
36
|
+
@sequenced = true
|
|
37
|
+
end
|
|
38
|
+
|
|
39
|
+
def generate_chain(word_count, start = nil)
|
|
40
|
+
warning_a = "Please run the instance method 'find_patterns' first to build the generator's dictionary."
|
|
41
|
+
warning_b = "The start word that you have provided does not appear in the text used to build this generator."
|
|
42
|
+
chain = ""
|
|
43
|
+
return warning_a if @sequenced == false
|
|
44
|
+
start = @words.sample if start == nil
|
|
45
|
+
return warning_b if !@words.include?(start)
|
|
46
|
+
chain += start
|
|
47
|
+
word_count.times do
|
|
48
|
+
chain += " "
|
|
49
|
+
probabilities = @frequencies[start]
|
|
50
|
+
next_word = MoreMarkov.weighted_choice(probabilities)
|
|
51
|
+
chain += next_word
|
|
52
|
+
start = next_word
|
|
53
|
+
end
|
|
54
|
+
chain
|
|
55
|
+
end
|
|
56
|
+
end
|
|
57
|
+
end
|
data/more_markov.gemspec
ADDED
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
# coding: utf-8
|
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
|
4
|
+
require 'more_markov/version'
|
|
5
|
+
|
|
6
|
+
Gem::Specification.new do |spec|
|
|
7
|
+
spec.name = "more_markov"
|
|
8
|
+
spec.version = MoreMarkov::VERSION
|
|
9
|
+
spec.authors = ["nathanielltaylor"]
|
|
10
|
+
spec.email = ["ntaylor390@gmail.com"]
|
|
11
|
+
|
|
12
|
+
spec.summary = %q{Simple Markov chain generation.}
|
|
13
|
+
spec.homepage = "https://github.com/nathanielltaylor/MoreMarkov"
|
|
14
|
+
spec.license = "MIT"
|
|
15
|
+
|
|
16
|
+
# Prevent pushing this gem to RubyGems.org by setting 'allowed_push_host', or
|
|
17
|
+
# delete this section to allow pushing this gem to any host.
|
|
18
|
+
if spec.respond_to?(:metadata)
|
|
19
|
+
spec.metadata['allowed_push_host'] = "https://rubygems.org"
|
|
20
|
+
else
|
|
21
|
+
raise "RubyGems 2.0 or newer is required to protect against public gem pushes."
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
|
25
|
+
spec.bindir = "exe"
|
|
26
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
|
27
|
+
spec.require_paths = ["lib"]
|
|
28
|
+
|
|
29
|
+
spec.add_development_dependency "bundler", "~> 1.10"
|
|
30
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
|
31
|
+
end
|
metadata
ADDED
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
|
2
|
+
name: more_markov
|
|
3
|
+
version: !ruby/object:Gem::Version
|
|
4
|
+
version: 0.1.0
|
|
5
|
+
platform: ruby
|
|
6
|
+
authors:
|
|
7
|
+
- nathanielltaylor
|
|
8
|
+
autorequire:
|
|
9
|
+
bindir: exe
|
|
10
|
+
cert_chain: []
|
|
11
|
+
date: 2016-06-14 00:00:00.000000000 Z
|
|
12
|
+
dependencies:
|
|
13
|
+
- !ruby/object:Gem::Dependency
|
|
14
|
+
name: bundler
|
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
|
16
|
+
requirements:
|
|
17
|
+
- - "~>"
|
|
18
|
+
- !ruby/object:Gem::Version
|
|
19
|
+
version: '1.10'
|
|
20
|
+
type: :development
|
|
21
|
+
prerelease: false
|
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
23
|
+
requirements:
|
|
24
|
+
- - "~>"
|
|
25
|
+
- !ruby/object:Gem::Version
|
|
26
|
+
version: '1.10'
|
|
27
|
+
- !ruby/object:Gem::Dependency
|
|
28
|
+
name: rake
|
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
|
30
|
+
requirements:
|
|
31
|
+
- - "~>"
|
|
32
|
+
- !ruby/object:Gem::Version
|
|
33
|
+
version: '10.0'
|
|
34
|
+
type: :development
|
|
35
|
+
prerelease: false
|
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
37
|
+
requirements:
|
|
38
|
+
- - "~>"
|
|
39
|
+
- !ruby/object:Gem::Version
|
|
40
|
+
version: '10.0'
|
|
41
|
+
description:
|
|
42
|
+
email:
|
|
43
|
+
- ntaylor390@gmail.com
|
|
44
|
+
executables: []
|
|
45
|
+
extensions: []
|
|
46
|
+
extra_rdoc_files: []
|
|
47
|
+
files:
|
|
48
|
+
- ".gitignore"
|
|
49
|
+
- ".travis.yml"
|
|
50
|
+
- Gemfile
|
|
51
|
+
- LICENSE.txt
|
|
52
|
+
- README.md
|
|
53
|
+
- Rakefile
|
|
54
|
+
- bin/console
|
|
55
|
+
- bin/setup
|
|
56
|
+
- lib/more_markov.rb
|
|
57
|
+
- lib/more_markov/version.rb
|
|
58
|
+
- more_markov.gemspec
|
|
59
|
+
homepage: https://github.com/nathanielltaylor/MoreMarkov
|
|
60
|
+
licenses:
|
|
61
|
+
- MIT
|
|
62
|
+
metadata:
|
|
63
|
+
allowed_push_host: https://rubygems.org
|
|
64
|
+
post_install_message:
|
|
65
|
+
rdoc_options: []
|
|
66
|
+
require_paths:
|
|
67
|
+
- lib
|
|
68
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
|
69
|
+
requirements:
|
|
70
|
+
- - ">="
|
|
71
|
+
- !ruby/object:Gem::Version
|
|
72
|
+
version: '0'
|
|
73
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
74
|
+
requirements:
|
|
75
|
+
- - ">="
|
|
76
|
+
- !ruby/object:Gem::Version
|
|
77
|
+
version: '0'
|
|
78
|
+
requirements: []
|
|
79
|
+
rubyforge_project:
|
|
80
|
+
rubygems_version: 2.4.5
|
|
81
|
+
signing_key:
|
|
82
|
+
specification_version: 4
|
|
83
|
+
summary: Simple Markov chain generation.
|
|
84
|
+
test_files: []
|