markov_uuid 0.0.3 → 0.0.5
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/.rspec +2 -0
- data/README.md +5 -1
- data/lib/markov_uuid/chain.rb +53 -0
- data/lib/markov_uuid/storage.rb +5 -24
- data/lib/markov_uuid.rb +2 -4
- data/markov_uuid.gemspec +3 -0
- data/spec/markov_spec.rb +52 -0
- data/spec/spec_helper.rb +17 -0
- metadata +41 -5
- data/lib/markov_uuid/key_selector.rb +0 -9
- data/lib/markov_uuid/markov.rb +0 -45
data/.rspec
ADDED
data/README.md
CHANGED
@@ -1,5 +1,9 @@
|
|
1
1
|
# MarkovUuid
|
2
|
-
|
2
|
+
# based on the Python version by Gary Burd: http://gary.burd.info/2003/11/markov-chain-generator.html
|
3
|
+
# Released into the public domain, please keep this notice intact
|
4
|
+
# (c) InVisible GmbH
|
5
|
+
# http://www.invisible.ch
|
6
|
+
#
|
3
7
|
TODO: Write a gem description
|
4
8
|
|
5
9
|
## Installation
|
@@ -0,0 +1,53 @@
|
|
1
|
+
module MarkovUuid
|
2
|
+
class Chain < Hash
|
3
|
+
SEPARATOR = "#-#-"
|
4
|
+
attr_reader :words
|
5
|
+
|
6
|
+
class << self
|
7
|
+
def from_file(f)
|
8
|
+
new(strip_punctuation File.read(f))
|
9
|
+
end
|
10
|
+
|
11
|
+
def strip_punctuation l
|
12
|
+
l.gsub(/[[:punct:]]/," ").gsub!(' ', ' ').split " "
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
def initialize words
|
17
|
+
@words = words
|
18
|
+
end
|
19
|
+
|
20
|
+
def new_key(key, word)
|
21
|
+
return SEPARATOR if word == "\n"
|
22
|
+
|
23
|
+
word or key
|
24
|
+
end
|
25
|
+
|
26
|
+
def add
|
27
|
+
key = SEPARATOR
|
28
|
+
|
29
|
+
words.each do |word|
|
30
|
+
(self[key] ||= []) << word
|
31
|
+
|
32
|
+
key = new_key(key, word)
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
def to_words length = 100
|
37
|
+
key = keys.sample
|
38
|
+
word = ""
|
39
|
+
|
40
|
+
result = length.times.map do
|
41
|
+
word = self[key].sample rescue nil
|
42
|
+
key = new_key key, word
|
43
|
+
word
|
44
|
+
end.compact
|
45
|
+
|
46
|
+
format result
|
47
|
+
end
|
48
|
+
|
49
|
+
def format words, i=32
|
50
|
+
words.join("-")[0..i.to_i].gsub(/\A\w+-/,'').gsub(/-$/,"").gsub(/-\w+$/,"")
|
51
|
+
end
|
52
|
+
end
|
53
|
+
end
|
data/lib/markov_uuid/storage.rb
CHANGED
@@ -2,36 +2,17 @@ require "yaml"
|
|
2
2
|
require "fileutils"
|
3
3
|
module MarkovUuid
|
4
4
|
class Storage
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
def initialize(data = nil )
|
9
|
-
@data = data if data.class == Hash
|
10
|
-
@data ||= Hash.new
|
11
|
-
end
|
12
|
-
|
13
|
-
def add words
|
14
|
-
words.add_to @data
|
5
|
+
def initialize filename
|
6
|
+
@filename = filename
|
15
7
|
end
|
16
8
|
|
17
|
-
|
18
|
-
key = Markov::SEPARATOR
|
19
|
-
word = ""
|
20
|
-
|
21
|
-
result = length.times.map do
|
22
|
-
word = @data[key].sample rescue nil
|
23
|
-
key = new_key key, word
|
24
|
-
word
|
25
|
-
end.compact
|
26
|
-
|
27
|
-
Markov.new result
|
28
|
-
end
|
9
|
+
attr_accessor :data, :filename
|
29
10
|
|
30
|
-
def save
|
11
|
+
def save
|
31
12
|
File.open(filename, "w"){|f| YAML.dump(@data, f) }
|
32
13
|
end
|
33
14
|
|
34
|
-
def load
|
15
|
+
def load
|
35
16
|
FileUtils.touch filename
|
36
17
|
File.open(filename) do |f|
|
37
18
|
@data = YAML.load f
|
data/lib/markov_uuid.rb
CHANGED
data/markov_uuid.gemspec
CHANGED
@@ -13,6 +13,9 @@ Gem::Specification.new do |gem|
|
|
13
13
|
that are more natural to convey to other people. E.g. over the phone, etc.}
|
14
14
|
gem.homepage = "http://github.com/markburns/markov_uuid"
|
15
15
|
|
16
|
+
gem.add_development_dependency 'rspec'
|
17
|
+
gem.add_development_dependency 'debugger'
|
18
|
+
|
16
19
|
gem.files = `git ls-files`.split($/)
|
17
20
|
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
|
18
21
|
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
|
data/spec/markov_spec.rb
ADDED
@@ -0,0 +1,52 @@
|
|
1
|
+
#encoding: utf-8
|
2
|
+
require File.expand_path('spec/spec_helper')
|
3
|
+
require './lib/markov_uuid'
|
4
|
+
|
5
|
+
describe MarkovUuid::Chain do
|
6
|
+
let(:file) do
|
7
|
+
<<-CONTENTS.gsub(/^\s*/, "")
|
8
|
+
CHAPTER I. Down the Rabbit-Hole
|
9
|
+
|
10
|
+
Alice was beginning to get very tired of sitting by her sister on the
|
11
|
+
bank, and of having nothing to do: once or twice she had peeped into the
|
12
|
+
book her sister was reading, but it had no pictures or conversations in
|
13
|
+
it, 'and what is the use of a book,' thought Alice 'without pictures or
|
14
|
+
conversation?'
|
15
|
+
CONTENTS
|
16
|
+
end
|
17
|
+
|
18
|
+
|
19
|
+
specify do
|
20
|
+
s = "これは Some text with, random -?! Punctuation in"
|
21
|
+
MarkovUuid::Chain.strip_punctuation(s).should == %w(これは Some text with random Punctuation in)
|
22
|
+
end
|
23
|
+
|
24
|
+
let(:words) do
|
25
|
+
["CHAPTER", "I", "Down", "the", "Rabbit", "Hole", "Alice", "was",
|
26
|
+
"beginning", "to", "get", "very", "tired", "of", "sitting", "by", "her",
|
27
|
+
"sister", "on", "the", "bank", "and", "of", "having", "nothing", "to",
|
28
|
+
"do", "once", "or", "twice", "she", "had", "peeped", "into", "the",
|
29
|
+
"book", "her", "sister", "was", "reading", "but", "it", "had", "no",
|
30
|
+
"pictures", "or", "conversations", "in", "it", "and", "what", "is",
|
31
|
+
"the", "use", "of", "a", "book", "thought", "Alice", "without",
|
32
|
+
"pictures", "or", "conversation"]
|
33
|
+
end
|
34
|
+
|
35
|
+
specify do
|
36
|
+
MarkovUuid::Chain.strip_punctuation(file).should == words
|
37
|
+
end
|
38
|
+
|
39
|
+
context 'from file' do
|
40
|
+
before do
|
41
|
+
File.stub(:read).and_return file
|
42
|
+
end
|
43
|
+
|
44
|
+
specify do
|
45
|
+
chain = MarkovUuid::Chain.from_file ''
|
46
|
+
chain.to_words.split("-").each do |word|
|
47
|
+
words.should include word
|
48
|
+
end
|
49
|
+
|
50
|
+
end
|
51
|
+
end
|
52
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,17 @@
|
|
1
|
+
# This file was generated by the `rspec --init` command. Conventionally, all
|
2
|
+
# specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
|
3
|
+
# Require this file using `require "spec_helper"` to ensure that it is only
|
4
|
+
# loaded once.
|
5
|
+
#
|
6
|
+
# See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
|
7
|
+
RSpec.configure do |config|
|
8
|
+
config.treat_symbols_as_metadata_keys_with_true_values = true
|
9
|
+
config.run_all_when_everything_filtered = true
|
10
|
+
config.filter_run :focus
|
11
|
+
|
12
|
+
# Run specs in random order to surface order dependencies. If you find an
|
13
|
+
# order dependency and want to debug it, you can fix the order by providing
|
14
|
+
# the seed, which is printed after each run.
|
15
|
+
# --seed 1234
|
16
|
+
config.order = 'random'
|
17
|
+
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: markov_uuid
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.5
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -10,7 +10,39 @@ autorequire:
|
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
12
|
date: 2012-10-02 00:00:00.000000000 Z
|
13
|
-
dependencies:
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: rspec
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ! '>='
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '0'
|
22
|
+
type: :development
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ! '>='
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: '0'
|
30
|
+
- !ruby/object:Gem::Dependency
|
31
|
+
name: debugger
|
32
|
+
requirement: !ruby/object:Gem::Requirement
|
33
|
+
none: false
|
34
|
+
requirements:
|
35
|
+
- - ! '>='
|
36
|
+
- !ruby/object:Gem::Version
|
37
|
+
version: '0'
|
38
|
+
type: :development
|
39
|
+
prerelease: false
|
40
|
+
version_requirements: !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - ! '>='
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: '0'
|
14
46
|
description: People friendly readable UUIDs
|
15
47
|
email:
|
16
48
|
- markthedeveloper@gmail.com
|
@@ -20,6 +52,7 @@ extensions: []
|
|
20
52
|
extra_rdoc_files: []
|
21
53
|
files:
|
22
54
|
- .gitignore
|
55
|
+
- .rspec
|
23
56
|
- Gemfile
|
24
57
|
- LICENSE.txt
|
25
58
|
- README.md
|
@@ -30,11 +63,12 @@ files:
|
|
30
63
|
- data/pride_and_prejudice_small.txt
|
31
64
|
- data/shakespeare_short.txt
|
32
65
|
- lib/markov_uuid.rb
|
33
|
-
- lib/markov_uuid/
|
34
|
-
- lib/markov_uuid/markov.rb
|
66
|
+
- lib/markov_uuid/chain.rb
|
35
67
|
- lib/markov_uuid/storage.rb
|
36
68
|
- markov.yaml
|
37
69
|
- markov_uuid.gemspec
|
70
|
+
- spec/markov_spec.rb
|
71
|
+
- spec/spec_helper.rb
|
38
72
|
- storage.rb
|
39
73
|
homepage: http://github.com/markburns/markov_uuid
|
40
74
|
licenses: []
|
@@ -61,5 +95,7 @@ signing_key:
|
|
61
95
|
specification_version: 3
|
62
96
|
summary: Easily generate random English-like UUIDs (or any other language) that are
|
63
97
|
more natural to convey to other people. E.g. over the phone, etc.
|
64
|
-
test_files:
|
98
|
+
test_files:
|
99
|
+
- spec/markov_spec.rb
|
100
|
+
- spec/spec_helper.rb
|
65
101
|
has_rdoc:
|
data/lib/markov_uuid/markov.rb
DELETED
@@ -1,45 +0,0 @@
|
|
1
|
-
module MarkovUuid
|
2
|
-
class Markov
|
3
|
-
include KeySelector
|
4
|
-
SEPARATOR = "#-#-"
|
5
|
-
|
6
|
-
def to_s i=32
|
7
|
-
@words.join("-")[0..i.to_i].gsub(/\A\w+-/,'').gsub(/-$/,"").gsub(/-\w+$/,"")
|
8
|
-
end
|
9
|
-
|
10
|
-
def initialize words
|
11
|
-
@words = words
|
12
|
-
end
|
13
|
-
|
14
|
-
def add_to markov_data
|
15
|
-
key = SEPARATOR
|
16
|
-
|
17
|
-
@words.each do |word|
|
18
|
-
(markov_data[key] ||= []) << word
|
19
|
-
|
20
|
-
key = new_key(key, word)
|
21
|
-
end
|
22
|
-
end
|
23
|
-
|
24
|
-
private
|
25
|
-
|
26
|
-
class << self
|
27
|
-
def from_file( f )
|
28
|
-
new lines(f).map { |l| from_string l }.flatten
|
29
|
-
end
|
30
|
-
|
31
|
-
private
|
32
|
-
|
33
|
-
def from_string l
|
34
|
-
l.gsub(/[^a-z ]/i,'').split " "
|
35
|
-
end
|
36
|
-
|
37
|
-
def lines f
|
38
|
-
content = File.read f
|
39
|
-
c = content.length
|
40
|
-
r = rand c
|
41
|
-
content[r-c .. r+c].gsub(/\A\s\w+/,"").gsub(/\w+\s\z/,"").downcase.split "\n"
|
42
|
-
end
|
43
|
-
end
|
44
|
-
end
|
45
|
-
end
|