markov_chains 0.0.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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 045674e5b0e005db622a8d1c6bae356074e88049
4
+ data.tar.gz: a52da2defd03a2cc01f5d8312cf5481cd27b2b20
5
+ SHA512:
6
+ metadata.gz: cf4da09f7460ecf5506a17eaeb2fde1ea04ea72dbc4d2f1f951d3f9b79a37a818168f9e4d796f56e1d921aed5f08be3d32427ae7f6e2ef46ffa26c6b9340711f
7
+ data.tar.gz: e40a5e57101f9bfd448e2ee9520ec240ecfaa3236e30ec31150af23a12a23d15be2f3211a58a1811bc60fba2999e5ca985bb8eec769cfcd8a218f62ccc6995d2
data/.gitignore ADDED
@@ -0,0 +1,14 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ *.bundle
11
+ *.so
12
+ *.o
13
+ *.a
14
+ mkmf.log
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in markov-chains.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 justindomingue
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,38 @@
1
+ # Markov Chains
2
+
3
+ A simple random text generator from source text using markov chains.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'markov_chains'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install markov_chains
20
+
21
+ ## Usage
22
+
23
+ Create a generator and give it a source text:
24
+
25
+ text = "Why did the chicken cross the Mobius Strip?, To get to the same side..."
26
+ generator = MarkovChains::Generator.new(text)
27
+
28
+ Generate a number of sentences, here 5:
29
+
30
+ generator.get_sentences(5)
31
+
32
+ ## Contributing
33
+
34
+ 1. Fork it ( https://github.com/[justindomingue]/markov-chains/fork )
35
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
36
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
37
+ 4. Push to the branch (`git push origin my-new-feature`)
38
+ 5. Create a new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,9 @@
1
+ require "bundler/gem_tasks"
2
+ require 'rake/testtask'
3
+
4
+ Rake::TestTask.new do |t|
5
+ t.libs << 'test'
6
+ end
7
+
8
+ desc "Run tests"
9
+ task :default => :test
@@ -0,0 +1,75 @@
1
+ module MarkovChains
2
+ class Dictionary
3
+
4
+ # Initialized the dictionary with a text source.
5
+ #
6
+ # @example Create a new dictionary
7
+ # MarkovChains::Dictionary.new(string)
8
+ # @param [String] the text source
9
+ #
10
+ def initialize(text)
11
+ @words = Hash.new
12
+ @start_words = Array.new
13
+
14
+ wordlist = text.split
15
+
16
+ # Add first word as possible start word
17
+ @start_words.push wordlist[0]
18
+
19
+ # Process each word
20
+ wordlist.each_with_index do |word, index|
21
+ add(word, wordlist[index + 1]) if index <= wordlist.size - 2
22
+ end
23
+ end
24
+
25
+ # Returns a word based on the likelihood of it appearing after the input word
26
+ #
27
+ # @example Get a word likely to appear next to the word 'It'
28
+ # get('It')
29
+ #
30
+ # @param [String] word for which we want a possible next word
31
+ # @return [String] word that is likely to follow the input word
32
+ #
33
+ def get(word)
34
+ return "" if !@words[word]
35
+ followers = @words[word]
36
+ sum = followers.inject(0) {|sum,kv| sum += kv[1]}
37
+ random = rand(sum)+1
38
+ partial_sum = 0
39
+ next_word = followers.find do |word, count|
40
+ partial_sum += count
41
+ partial_sum >= random
42
+ end.first
43
+ next_word
44
+ end
45
+
46
+ # Returns a word beginning a sentence seen in the source
47
+ #
48
+ # @example Get a start word
49
+ # get_start_word()
50
+ # @return [String] a possible start word
51
+ #
52
+ def get_start_word
53
+ @start_words.sample
54
+ end
55
+
56
+ private
57
+
58
+ # Adds word-next_word combination to the dictionary
59
+ #
60
+ # @example Adding a word in a order 1 dictionary
61
+ # add_word("It", "has")
62
+ # @example Adding words in a order m dictionary
63
+ # add_word("It has", "been")
64
+ #
65
+ # @param word [string] root word
66
+ # @param next_word [string] word following the root word
67
+ #
68
+ def add(word, next_word)
69
+ @words[word] = Hash.new(0) if !@words[word]
70
+ @words[word][next_word] += 1
71
+ @start_words.push(next_word) if word.end_with? '.' and !word.nil?
72
+ end
73
+
74
+ end
75
+ end
@@ -0,0 +1,40 @@
1
+ module MarkovChains
2
+ class Generator
3
+
4
+ # Initializes the generator
5
+ #
6
+ # @example Create a new generator
7
+ # MarkovChains::Generator.new(text)
8
+ #
9
+ # @param Text source to generate sentences from
10
+ #
11
+ def initialize(text)
12
+ @dict = MarkovChains::Dictionary.new(text)
13
+ end
14
+
15
+ # Returns a given number of randonly generated sentences
16
+ #
17
+ # @example Get 5 sentences
18
+ # get_sentences(5)
19
+ #
20
+ # @param n [int] number of sentences to generate
21
+ # @return Array conataining generated sentences
22
+ #
23
+ def get_sentences(n)
24
+ sentences = []
25
+
26
+ n.times do
27
+ sentence = ""
28
+ word = @dict.get_start_word
29
+ until sentence.scan(/\.|\?|!/).size == 1
30
+ sentence << word << " "
31
+ word = @dict.get(word)
32
+ end
33
+
34
+ sentences.push sentence
35
+ end
36
+
37
+ sentences
38
+ end
39
+ end
40
+ end
@@ -0,0 +1,3 @@
1
+ module MarkovChains
2
+ VERSION = '0.0.0'
3
+ end
@@ -0,0 +1,7 @@
1
+ require_relative 'markov_chains/version.rb'
2
+
3
+ module MarkovChains
4
+ end
5
+
6
+ require_relative 'markov_chains/dictionary.rb'
7
+ require_relative 'markov_chains/generator.rb'
@@ -0,0 +1,22 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'markov_chains/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "markov_chains"
8
+ spec.version = MarkovChains::VERSION
9
+ spec.authors = ["justindomingue"]
10
+ spec.email = ["justin.domingue@hotmail.com"]
11
+ spec.summary = 'A simple random text generator using markov chains.'
12
+ spec.homepage = "https://github.com/justindomingue/markov_chains"
13
+ spec.license = "MIT"
14
+
15
+ spec.files = `git ls-files -z`.split("\x0")
16
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
17
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
18
+ spec.require_paths = ["lib"]
19
+
20
+ spec.add_development_dependency "bundler", "~> 1.7"
21
+ spec.add_development_dependency "rake", "~> 10.0"
22
+ end
@@ -0,0 +1,19 @@
1
+ require 'minitest/autorun'
2
+ require 'markov_chains'
3
+
4
+ class DictionaryTest < Minitest::Test
5
+ def setup
6
+ source = "Why did the chicken cross the road?"
7
+ @dict = MarkovChains::Dictionary.new(source)
8
+ end
9
+
10
+ def test_get
11
+ assert_equal "did", @dict.get("Why")
12
+ assert_equal true, %w(chicken road).include?( @dict.get("the"))
13
+ assert_equal "", @dict.get("Not there...")
14
+ end
15
+
16
+ def test_get_start_word
17
+ assert_equal "Why", @dict.get_start_word()
18
+ end
19
+ end
@@ -0,0 +1,13 @@
1
+ require 'minitest/autorun'
2
+ require 'markov_chains'
3
+
4
+ class GeneratorTest < Minitest::Test
5
+ def setup
6
+ source = "Why did the chicken cross the Mobius Strip? To get to the same side!"
7
+ @generator = MarkovChains::Generator.new source
8
+ end
9
+
10
+ def test_get_sentences
11
+ assert_equal 5, @generator.get_sentences(5).count
12
+ end
13
+ end
metadata ADDED
@@ -0,0 +1,86 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: markov_chains
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.0
5
+ platform: ruby
6
+ authors:
7
+ - justindomingue
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-12-30 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.7'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.7'
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
+ - justin.domingue@hotmail.com
44
+ executables: []
45
+ extensions: []
46
+ extra_rdoc_files: []
47
+ files:
48
+ - ".gitignore"
49
+ - Gemfile
50
+ - LICENSE.txt
51
+ - README.md
52
+ - Rakefile
53
+ - lib/markov_chains.rb
54
+ - lib/markov_chains/dictionary.rb
55
+ - lib/markov_chains/generator.rb
56
+ - lib/markov_chains/version.rb
57
+ - markov_chains.gemspec
58
+ - test/test_dictionary.rb
59
+ - test/test_generator.rb
60
+ homepage: https://github.com/justindomingue/markov_chains
61
+ licenses:
62
+ - MIT
63
+ metadata: {}
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.3
81
+ signing_key:
82
+ specification_version: 4
83
+ summary: A simple random text generator using markov chains.
84
+ test_files:
85
+ - test/test_dictionary.rb
86
+ - test/test_generator.rb