marky_markov 0.3.2 → 0.3.3
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.
- data/bin/marky_markov +25 -10
- data/lib/marky_markov.rb +2 -2
- data/marky_markov.gemspec +6 -8
- data/spec/data/onetextdictcompare.mmd +38 -0
- data/spec/data/test.txt +1 -0
- data/spec/{textdictcompare.mmd → data/textdictcompare.mmd} +3 -1
- data/spec/marky_markov/marky_markov_spec.rb +34 -33
- metadata +9 -11
- data/spec/marky_markov/markov_dictionary_spec.rb +0 -65
- data/spec/marky_markov/markov_sentence_generator_spec.rb +0 -25
- data/spec/marky_markov/persistent_dictionary_spec.rb +0 -22
- data/spec/test.txt +0 -1
data/bin/marky_markov
CHANGED
@@ -42,7 +42,10 @@ opt_parser = OptionParser.new do |opts|
|
|
42
42
|
end
|
43
43
|
end
|
44
44
|
|
45
|
-
opt_parser.parse!
|
45
|
+
begin opt_parser.parse!
|
46
|
+
rescue OptionParser::InvalidOption => err
|
47
|
+
abort("Sorry, #{err}. Run marky_markov -h for allowed options.")
|
48
|
+
end
|
46
49
|
|
47
50
|
if options[:resetdictionary]
|
48
51
|
STDOUT.puts MarkyMarkov::Dictionary.delete_dictionary!(options[:dictionary])
|
@@ -53,11 +56,14 @@ case ARGV[0]
|
|
53
56
|
when "speak"
|
54
57
|
if options[:source]
|
55
58
|
markov = MarkyMarkov::TemporaryDictionary.new
|
56
|
-
markov.parse_file(options[:source])
|
59
|
+
begin markov.parse_file(options[:source])
|
60
|
+
rescue MarkovDictionary::FileNotFoundError => err
|
61
|
+
abort("Sorry, #{err}. I can't use it as a source.")
|
62
|
+
end
|
57
63
|
else
|
58
64
|
unless File.exists?("#{options[:dictionary]}.mmd")
|
59
|
-
STDERR.puts "Dictionary file #{options[:dictionary]}
|
60
|
-
STDERR.puts "Please build a dictionary with read or use the --source option
|
65
|
+
STDERR.puts "Dictionary file #{options[:dictionary]}.mmd does not exist. Cannot generate sentence."
|
66
|
+
STDERR.puts "Please build a dictionary with read or use the --source option to build a temporary dictionary."
|
61
67
|
exit(false)
|
62
68
|
end
|
63
69
|
markov = MarkyMarkov::Dictionary.new(options[:dictionary])
|
@@ -66,20 +72,29 @@ when "speak"
|
|
66
72
|
when "read"
|
67
73
|
source = ARGV[1] || options[:source]
|
68
74
|
markov = MarkyMarkov::Dictionary.new(options[:dictionary])
|
69
|
-
|
70
|
-
|
71
|
-
|
75
|
+
begin
|
76
|
+
markov.parse_file(source)
|
77
|
+
markov.save_dictionary!
|
78
|
+
STDOUT.puts "Added #{source} to dictionary."
|
79
|
+
rescue MarkovDictionary::FileNotFoundError => err
|
80
|
+
abort("Sorry, I can't read #{source}. I'm afraid it doesn't exist!")
|
81
|
+
end
|
72
82
|
when "listen"
|
73
83
|
markov = MarkyMarkov::TemporaryDictionary.new
|
74
|
-
|
75
|
-
|
84
|
+
begin
|
85
|
+
markov.parse_string(STDIN.tty? ? ARGV[1] : STDIN.read)
|
86
|
+
STDOUT.puts markov.generate_n_sentences(options[:sentencecount])
|
87
|
+
rescue
|
88
|
+
abort("Not enough words to generate a sentence.")
|
89
|
+
end
|
76
90
|
else
|
77
91
|
unless STDIN.tty?
|
78
92
|
markov = MarkyMarkov::TemporaryDictionary.new
|
79
93
|
markov.parse_string(STDIN.read)
|
80
94
|
STDOUT.puts markov.generate_n_sentences(options[:sentencecount])
|
81
95
|
else
|
96
|
+
STDERR.puts "Sorry, I don't know how to #{ARGV[0]}."
|
82
97
|
STDOUT.puts opt_parser
|
98
|
+
exit false
|
83
99
|
end
|
84
100
|
end
|
85
|
-
#end
|
data/lib/marky_markov.rb
CHANGED
@@ -4,12 +4,12 @@
|
|
4
4
|
require_relative 'marky_markov/persistent_dictionary'
|
5
5
|
require_relative 'marky_markov/markov_sentence_generator'
|
6
6
|
|
7
|
-
# @version = 0.3.
|
7
|
+
# @version = 0.3.3
|
8
8
|
# @author Matt Furden
|
9
9
|
# Module containing TemporaryDictionary and Dictionary for creation of
|
10
10
|
# Markov Chain Dictionaries and generating sentences from those dictionaries.
|
11
11
|
module MarkyMarkov
|
12
|
-
VERSION = '0.3.
|
12
|
+
VERSION = '0.3.3'
|
13
13
|
|
14
14
|
class TemporaryDictionary
|
15
15
|
# Create a new Temporary Markov Chain Dictionary and sentence generator for use.
|
data/marky_markov.gemspec
CHANGED
@@ -13,8 +13,8 @@ Gem::Specification.new do |s|
|
|
13
13
|
## If your rubyforge_project name is different, then edit it and comment out
|
14
14
|
## the sub! line in the Rakefile
|
15
15
|
s.name = 'marky_markov'
|
16
|
-
s.version = '0.3.
|
17
|
-
s.date = '2012-02-
|
16
|
+
s.version = '0.3.3'
|
17
|
+
s.date = '2012-02-13'
|
18
18
|
s.rubyforge_project = 'marky_markov'
|
19
19
|
|
20
20
|
## Make sure your summary is short. The description may be as long
|
@@ -29,7 +29,7 @@ Gem::Specification.new do |s|
|
|
29
29
|
## a custom homepage, consider using your GitHub URL or the like.
|
30
30
|
s.authors = ["Matt Furden"]
|
31
31
|
s.email = 'mfurden@gmail.com'
|
32
|
-
s.homepage = '
|
32
|
+
s.homepage = 'https://github.com/zolrath/marky_markov'
|
33
33
|
|
34
34
|
## This gets added to the $LOAD_PATH so that 'lib/NAME.rb' can be required as
|
35
35
|
## require 'NAME.rb' or'/lib/NAME/file.rb' can be as require 'NAME/file.rb'
|
@@ -67,13 +67,11 @@ Gem::Specification.new do |s|
|
|
67
67
|
lib/marky_markov/markov_sentence_generator.rb
|
68
68
|
lib/marky_markov/persistent_dictionary.rb
|
69
69
|
marky_markov.gemspec
|
70
|
-
spec/
|
71
|
-
spec/
|
70
|
+
spec/data/onetextdictcompare.mmd
|
71
|
+
spec/data/test.txt
|
72
|
+
spec/data/textdictcompare.mmd
|
72
73
|
spec/marky_markov/marky_markov_spec.rb
|
73
|
-
spec/marky_markov/persistent_dictionary_spec.rb
|
74
74
|
spec/spec_helper.rb
|
75
|
-
spec/test.txt
|
76
|
-
spec/textdictcompare.mmd
|
77
75
|
]
|
78
76
|
# = MANIFEST =
|
79
77
|
|
@@ -0,0 +1,38 @@
|
|
1
|
+
1<h>
|
2
|
+
<a>
|
3
|
+
<s>The</s>
|
4
|
+
</a>
|
5
|
+
<a>
|
6
|
+
<s>cat</s>
|
7
|
+
</a>
|
8
|
+
<a>
|
9
|
+
<s>cat</s>
|
10
|
+
</a>
|
11
|
+
<a>
|
12
|
+
<s>likes</s>
|
13
|
+
</a>
|
14
|
+
<a>
|
15
|
+
<s>likes</s>
|
16
|
+
</a>
|
17
|
+
<a>
|
18
|
+
<s>pie</s>
|
19
|
+
</a>
|
20
|
+
<a>
|
21
|
+
<s>pie</s>
|
22
|
+
</a>
|
23
|
+
<a>
|
24
|
+
<s>and</s>
|
25
|
+
</a>
|
26
|
+
<a>
|
27
|
+
<s>and</s>
|
28
|
+
</a>
|
29
|
+
<a>
|
30
|
+
<s>chainsaws</s>
|
31
|
+
</a>
|
32
|
+
<a>
|
33
|
+
<s>chainsaws</s>
|
34
|
+
</a>
|
35
|
+
<a>
|
36
|
+
<s>!</s>
|
37
|
+
</a>
|
38
|
+
</h>
|
data/spec/data/test.txt
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
The cat likes pie and chainsaws!
|
@@ -1,64 +1,65 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
3
|
describe MarkyMarkov do
|
4
|
+
before(:each) do
|
5
|
+
@textsource = "spec/data/test.txt"
|
6
|
+
@onedictcompare = { ["The"] => ["cat"],
|
7
|
+
["cat"] => ["likes"],
|
8
|
+
["likes"] => ["pie"],
|
9
|
+
["pie"] => ["and"],
|
10
|
+
["and"] => ["chainsaws"],
|
11
|
+
["chainsaws"] => ["!"]}
|
12
|
+
@twodictcompare = {["The", "cat"] => ["likes"],
|
13
|
+
["and", "chainsaws"] => ["!"],
|
14
|
+
["cat", "likes"] => ["pie"],
|
15
|
+
["likes", "pie"] => ["and"],
|
16
|
+
["pie", "and"] => ["chainsaws"]}
|
17
|
+
end
|
18
|
+
|
4
19
|
context "TemporaryDictionary" do
|
5
20
|
before(:each) do
|
6
|
-
@textsource = "spec/test.txt"
|
7
21
|
@dictionary = MarkyMarkov::TemporaryDictionary.new
|
8
|
-
@onedictcompare = { ["The"] => ["cat"],
|
9
|
-
["cat"] => ["likes"],
|
10
|
-
["likes"] => ["pie"],
|
11
|
-
["pie"] => ["and"],
|
12
|
-
["and"] => ["chainsaws"],
|
13
|
-
["chainsaws"] => []}
|
14
|
-
@twodictcompare = {["The", "cat"] => ["likes"],
|
15
|
-
["and", "chainsaws"] => [],
|
16
|
-
["cat", "likes"] => ["pie"],
|
17
|
-
["likes", "pie"] => ["and"],
|
18
|
-
["pie", "and"] => ["chainsaws"]}
|
19
22
|
end
|
20
23
|
|
21
24
|
it "should be able to parse a string" do
|
22
|
-
|
25
|
+
@dictionary.parse_string "The cat likes pie and chainsaws!"
|
23
26
|
@dictionary.dictionary.should eql(@twodictcompare)
|
24
27
|
end
|
25
28
|
|
26
29
|
it "should generate the right number of sentences" do
|
30
|
+
@dictionary.parse_string "Hey man. How are you doing? Let's get pie!"
|
31
|
+
sentence = @dictionary.generate_5_sentences
|
32
|
+
sentence.should have(5).scan(/[.?!]/)
|
27
33
|
end
|
28
34
|
|
29
35
|
it "should create the right number of words" do
|
36
|
+
@dictionary.parse_string "Hey man. How are you doing? Let's get pie!"
|
37
|
+
sentence = @dictionary.generate_10_words
|
38
|
+
sentence.split.should have(10).words
|
30
39
|
end
|
31
40
|
end
|
32
41
|
|
33
42
|
context "PersistentDictionary" do
|
34
|
-
before
|
35
|
-
@
|
36
|
-
@dictionary
|
37
|
-
@onedictcompare = { ["The"] => ["cat"],
|
38
|
-
["cat"] => ["likes"],
|
39
|
-
["likes"] => ["pie"],
|
40
|
-
["pie"] => ["and"],
|
41
|
-
["and"] => ["chainsaws"],
|
42
|
-
["chainsaws"] => []}
|
43
|
-
@twodictcompare = {["The", "cat"] => ["likes"],
|
44
|
-
["and", "chainsaws"] => [],
|
45
|
-
["cat", "likes"] => ["pie"],
|
46
|
-
["likes", "pie"] => ["and"],
|
47
|
-
["pie", "and"] => ["chainsaws"]}
|
43
|
+
before do
|
44
|
+
@dictionary = MarkyMarkov::Dictionary.new("spec/data/temptextdict")
|
45
|
+
@dictionary.parse_file "spec/data/test.txt"
|
48
46
|
end
|
49
47
|
|
50
|
-
it "should
|
48
|
+
it "should be able to save a dictionary" do
|
49
|
+
@dictionary.save_dictionary!.should eql(true)
|
51
50
|
end
|
52
51
|
|
53
|
-
it "should be able to
|
54
|
-
|
55
|
-
@dictionary.dictionary.should
|
52
|
+
it "should be able to load an existing dictionary" do
|
53
|
+
otherdict = MarkyMarkov::Dictionary.new("spec/data/textdictcompare")
|
54
|
+
@dictionary.dictionary.should eql(otherdict.dictionary)
|
56
55
|
end
|
57
56
|
|
58
|
-
it "should
|
57
|
+
it "should load the saved dictionary" do
|
58
|
+
@dictionary.dictionary.should include(@twodictcompare)
|
59
59
|
end
|
60
60
|
|
61
|
-
|
61
|
+
after do
|
62
|
+
PersistentDictionary.delete_dictionary!(@dictionary)
|
62
63
|
end
|
63
64
|
end
|
64
65
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: marky_markov
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.3.
|
4
|
+
version: 0.3.3
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,11 +9,11 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2012-02-
|
12
|
+
date: 2012-02-13 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: ox
|
16
|
-
requirement: &
|
16
|
+
requirement: &70245354003740 !ruby/object:Gem::Requirement
|
17
17
|
none: false
|
18
18
|
requirements:
|
19
19
|
- - ~>
|
@@ -21,7 +21,7 @@ dependencies:
|
|
21
21
|
version: '1.4'
|
22
22
|
type: :runtime
|
23
23
|
prerelease: false
|
24
|
-
version_requirements: *
|
24
|
+
version_requirements: *70245354003740
|
25
25
|
description: ! "MarkyMarkov makes it easy to generate simply Markov Chains based upon
|
26
26
|
input from\n either a source file or a string. While usable as a module in your
|
27
27
|
code it can also be called on\n from the command line and piped into like a standard
|
@@ -40,14 +40,12 @@ files:
|
|
40
40
|
- lib/marky_markov/markov_sentence_generator.rb
|
41
41
|
- lib/marky_markov/persistent_dictionary.rb
|
42
42
|
- marky_markov.gemspec
|
43
|
-
- spec/
|
44
|
-
- spec/
|
43
|
+
- spec/data/onetextdictcompare.mmd
|
44
|
+
- spec/data/test.txt
|
45
|
+
- spec/data/textdictcompare.mmd
|
45
46
|
- spec/marky_markov/marky_markov_spec.rb
|
46
|
-
- spec/marky_markov/persistent_dictionary_spec.rb
|
47
47
|
- spec/spec_helper.rb
|
48
|
-
|
49
|
-
- spec/textdictcompare.mmd
|
50
|
-
homepage: http://www.thefurd.com
|
48
|
+
homepage: https://github.com/zolrath/marky_markov
|
51
49
|
licenses: []
|
52
50
|
post_install_message:
|
53
51
|
rdoc_options:
|
@@ -68,7 +66,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
68
66
|
version: '0'
|
69
67
|
requirements: []
|
70
68
|
rubyforge_project: marky_markov
|
71
|
-
rubygems_version: 1.8.
|
69
|
+
rubygems_version: 1.8.16
|
72
70
|
signing_key:
|
73
71
|
specification_version: 2
|
74
72
|
summary: Simple Markov Chain generation available in the command-line
|
@@ -1,65 +0,0 @@
|
|
1
|
-
require 'spec_helper.rb'
|
2
|
-
|
3
|
-
describe MarkovDictionary do
|
4
|
-
context "One Word Depth Dictionary" do
|
5
|
-
before(:each) do
|
6
|
-
@onetextsource = "spec/test.txt"
|
7
|
-
@onedict = MarkovDictionary.new(1)
|
8
|
-
@onedict.parse_source("The cat likes pie and chainsaws", false)
|
9
|
-
@onetextdict = { ["The"] => ["cat"],
|
10
|
-
["cat"] => ["likes"],
|
11
|
-
["likes"] => ["pie"],
|
12
|
-
["pie"] => ["and"],
|
13
|
-
["and"] => ["chainsaws"],
|
14
|
-
["chainsaws"] => []}
|
15
|
-
end
|
16
|
-
|
17
|
-
it "can open a file" do
|
18
|
-
@onedict.open_source(@onetextsource).should_not be_nil
|
19
|
-
end
|
20
|
-
|
21
|
-
it "should give a FileNotFoundError if the file doesn't exist" do
|
22
|
-
expect { @onedict.open_source("thisisntreal") }.to
|
23
|
-
raise_error(MarkovDictionary::FileNotFoundError,"thisisntreal does not exist!")
|
24
|
-
end
|
25
|
-
|
26
|
-
it "can add a word to the dictionary" do
|
27
|
-
@onedict.add_word(["to"], "be")
|
28
|
-
@onedict.dictionary.should include(["to"] => ["be"])
|
29
|
-
end
|
30
|
-
|
31
|
-
it "create a dictionary via parsing a text file" do
|
32
|
-
@onedict.dictionary = {}
|
33
|
-
@onedict.parse_source(@onetextsource)
|
34
|
-
@onedict.dictionary.should eql(@onetextdict)
|
35
|
-
end
|
36
|
-
|
37
|
-
it "builds a one word dictionary properly" do
|
38
|
-
@onedict.dictionary.should eql(@onetextdict)
|
39
|
-
end
|
40
|
-
end
|
41
|
-
|
42
|
-
context "Two Word Dictionary" do
|
43
|
-
before(:each) do
|
44
|
-
@twodict = MarkovDictionary.new
|
45
|
-
@twodict.parse_source("The cat likes pie and chainsaws", false)
|
46
|
-
@twotextsource = "spec/test.txt"
|
47
|
-
@twotextdict = {["The", "cat"] => ["likes"],
|
48
|
-
["and", "chainsaws"] => [],
|
49
|
-
["cat", "likes"] => ["pie"],
|
50
|
-
["likes", "pie"] => ["and"],
|
51
|
-
["pie", "and"] => ["chainsaws"]}
|
52
|
-
end
|
53
|
-
|
54
|
-
it "can add a word to the two-word dictionary" do
|
55
|
-
@twodict.add_word(["Zebras", "like"], "kung-fu")
|
56
|
-
@twodict.dictionary.should eql(@twotextdict.merge( {["Zebras", "like"] => ["kung-fu"]} ))
|
57
|
-
end
|
58
|
-
|
59
|
-
it "create a two-word dictionary via parsing a text file" do
|
60
|
-
@twodict.dictionary = {}
|
61
|
-
@twodict.parse_source(@twotextsource)
|
62
|
-
@twodict.dictionary.should eql(@twotextdict)
|
63
|
-
end
|
64
|
-
end
|
65
|
-
end
|
@@ -1,25 +0,0 @@
|
|
1
|
-
require 'spec_helper.rb'
|
2
|
-
|
3
|
-
describe MarkovSentenceGenerator do
|
4
|
-
before(:each) do
|
5
|
-
@dict = MarkovDictionary.new
|
6
|
-
@dict.parse_source("Hello man how are you today", false)
|
7
|
-
@sentence = MarkovSentenceGenerator.new(@dict)
|
8
|
-
end
|
9
|
-
|
10
|
-
it "can pick a random word" do
|
11
|
-
|
12
|
-
end
|
13
|
-
|
14
|
-
it "can choose a weighted random word" do
|
15
|
-
|
16
|
-
end
|
17
|
-
|
18
|
-
it "will use a random word if the word does not exist" do
|
19
|
-
|
20
|
-
end
|
21
|
-
|
22
|
-
it "generates a sentence of the appropriate length" do
|
23
|
-
@sentence.generate(20).split.count.should eql(20)
|
24
|
-
end
|
25
|
-
end
|
@@ -1,22 +0,0 @@
|
|
1
|
-
require 'spec_helper'
|
2
|
-
|
3
|
-
describe PersistentDictionary do
|
4
|
-
before do
|
5
|
-
@dict = PersistentDictionary.new("spec/textdict.mmd")
|
6
|
-
@dict.parse_source("spec/test.txt")
|
7
|
-
end
|
8
|
-
|
9
|
-
it "should be able to save a dictionary" do
|
10
|
-
@dict.save_dictionary!.should eql(true)
|
11
|
-
end
|
12
|
-
|
13
|
-
it "should be able to load an existing dictionary" do
|
14
|
-
otherdict = PersistentDictionary.new("spec/textdictcompare.mmd")
|
15
|
-
@dict.dictionary.should eql(otherdict.dictionary)
|
16
|
-
end
|
17
|
-
|
18
|
-
after do
|
19
|
-
PersistentDictionary.delete_dictionary!("spec/textdict.mmd")
|
20
|
-
end
|
21
|
-
end
|
22
|
-
|
data/spec/test.txt
DELETED
@@ -1 +0,0 @@
|
|
1
|
-
The cat likes pie and chainsaws
|