This repository has been archived by the owner on May 17, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
15 changed files
with
302 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
--color | ||
--color --require spec_helper |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
guard 'rspec', :version => 2, :cli => open('.rspec').readline.chop do | ||
watch(%r{^spec/.+_spec\.rb$}) | ||
watch(%r{^lib/(.+)\.rb$}) { |m| "spec/lib/#{m[1]}_spec.rb" } | ||
watch('spec/spec_helper.rb') { "spec" } | ||
end | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
0.0.0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
#!/usr/bin/env ruby | ||
# coding: utf-8 | ||
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib')) | ||
require 'markovchain' | ||
|
||
markov = Markovchain.new 2 | ||
markov.seed 'To be or not to be' | ||
markov.seed 'Too drunk to fuck' | ||
markov.seed 'Shut the fuck up and write some code' | ||
|
||
puts markov.random_sequence |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
require 'markovchain/corpus' | ||
|
||
class Markovchain | ||
attr_reader :corpus | ||
|
||
NON_WORD = "\0" | ||
|
||
def initialize(gram) | ||
@gram = gram | ||
@corpus = Markovchain::Corpus.new gram | ||
end | ||
|
||
def seed(sequence) | ||
@corpus.seed sequence | ||
end | ||
|
||
def random_sequence | ||
result = '' | ||
token = nil | ||
prev_sequence = NON_WORD * @gram | ||
while token != NON_WORD | ||
token = random_pick(@corpus.tokens_after(prev_sequence)) | ||
result += token unless token == NON_WORD | ||
prev_sequence = prev_sequence[1, @gram - 1] + token | ||
end | ||
result | ||
end | ||
|
||
private | ||
def random_pick(tokens) | ||
choices = [] | ||
tokens.each do |token, weight| | ||
weight.times do | ||
choices.push token | ||
end | ||
end | ||
choices.sample | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
require 'markovchain' | ||
|
||
class Markovchain | ||
class Corpus | ||
attr_reader :prev_sequence, :storage | ||
|
||
def initialize(gram) | ||
@gram = gram | ||
@storage = {} | ||
end | ||
|
||
def seed(sequence) | ||
@prev_sequence = NON_WORD * @gram | ||
sequence.each_char do |token| | ||
seed_token(token) | ||
end | ||
finalize_seeding | ||
end | ||
|
||
def seed_token(token) | ||
increment(token) | ||
@prev_sequence = @prev_sequence[1, @gram - 1] + token | ||
end | ||
|
||
def tokens_after(sequence) | ||
@storage[sequence] || {} | ||
end | ||
|
||
private | ||
def increment(token) | ||
@storage[@prev_sequence] = {} unless @storage.key? @prev_sequence | ||
@storage[@prev_sequence][token] = 0 unless @storage[@prev_sequence].key? token | ||
@storage[@prev_sequence][token] += 1 | ||
end | ||
|
||
def finalize_seeding | ||
increment(NON_WORD) | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
# Generated by jeweler | ||
# DO NOT EDIT THIS FILE DIRECTLY | ||
# Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec' | ||
# -*- encoding: utf-8 -*- | ||
|
||
Gem::Specification.new do |s| | ||
s.name = "markovchain" | ||
s.version = "0.0.0" | ||
|
||
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version= | ||
s.authors = ["yuyat"] | ||
s.date = "2012-05-05" | ||
s.description = "TODO: longer description of your gem" | ||
s.email = "sign.of.the.wolf.pentagram@mgmail.com" | ||
s.extra_rdoc_files = [ | ||
"LICENSE.txt", | ||
"README.rdoc" | ||
] | ||
s.files = [ | ||
".document", | ||
".rspec", | ||
"Gemfile", | ||
"Gemfile.lock", | ||
"Guardfile", | ||
"LICENSE.txt", | ||
"README.rdoc", | ||
"Rakefile", | ||
"VERSION", | ||
"examples/example.rb", | ||
"lib/markovchain.rb", | ||
"lib/markovchain/corpus.rb", | ||
"spec/lib/markovchain/corpus_spec.rb", | ||
"spec/lib/markovchain_spec.rb", | ||
"spec/spec_helper.rb" | ||
] | ||
s.homepage = "http://github.com/yuya-takeyama/markovchain" | ||
s.licenses = ["MIT"] | ||
s.require_paths = ["lib"] | ||
s.rubygems_version = "1.8.10" | ||
s.summary = "TODO: one-line summary of your gem" | ||
|
||
if s.respond_to? :specification_version then | ||
s.specification_version = 3 | ||
|
||
if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then | ||
s.add_development_dependency(%q<rspec>, ["~> 2.8.0"]) | ||
s.add_development_dependency(%q<guard-rspec>, [">= 0"]) | ||
s.add_development_dependency(%q<rdoc>, ["~> 3.12"]) | ||
s.add_development_dependency(%q<bundler>, ["~> 1.1.0"]) | ||
s.add_development_dependency(%q<jeweler>, ["~> 1.8.3"]) | ||
s.add_development_dependency(%q<simplecov>, [">= 0"]) | ||
else | ||
s.add_dependency(%q<rspec>, ["~> 2.8.0"]) | ||
s.add_dependency(%q<guard-rspec>, [">= 0"]) | ||
s.add_dependency(%q<rdoc>, ["~> 3.12"]) | ||
s.add_dependency(%q<bundler>, ["~> 1.1.0"]) | ||
s.add_dependency(%q<jeweler>, ["~> 1.8.3"]) | ||
s.add_dependency(%q<simplecov>, [">= 0"]) | ||
end | ||
else | ||
s.add_dependency(%q<rspec>, ["~> 2.8.0"]) | ||
s.add_dependency(%q<guard-rspec>, [">= 0"]) | ||
s.add_dependency(%q<rdoc>, ["~> 3.12"]) | ||
s.add_dependency(%q<bundler>, ["~> 1.1.0"]) | ||
s.add_dependency(%q<jeweler>, ["~> 1.8.3"]) | ||
s.add_dependency(%q<simplecov>, [">= 0"]) | ||
end | ||
end | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
# coding: utf-8 | ||
require 'markovchain/corpus' | ||
|
||
describe Markovchain::Corpus do | ||
describe '#storage' do | ||
subject { corpus.storage } | ||
|
||
context '1-gram' do | ||
let(:corpus) { Markovchain::Corpus.new 1 } | ||
|
||
context 'by default' do | ||
it { should == {} } | ||
end | ||
|
||
context 'seeded "a"' do | ||
before { corpus.seed('a') } | ||
|
||
it { | ||
should == { | ||
non_word => {"a" => 1}, | ||
"a" => {non_word => 1}, | ||
} | ||
} | ||
end | ||
|
||
context 'seeded "ab"' do | ||
before { corpus.seed('ab') } | ||
|
||
it { | ||
should == { | ||
non_word => {'a' => 1}, | ||
'a' => {'b' => 1}, | ||
'b' => {non_word => 1}, | ||
} | ||
} | ||
end | ||
|
||
context 'seeded "abc"' do | ||
before { corpus.seed('abc') } | ||
|
||
it { | ||
should == { | ||
non_word => {'a' => 1}, | ||
'a' => {'b' => 1}, | ||
'b' => {'c' => 1}, | ||
'c' => {non_word => 1}, | ||
} | ||
} | ||
end | ||
|
||
context 'seeded "aaa"' do | ||
before { corpus.seed('aaa') } | ||
|
||
it { | ||
should == { | ||
non_word => {'a' => 1}, | ||
'a' => { | ||
'a' => 2, | ||
non_word => 1, | ||
}, | ||
} | ||
} | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
# coding: utf-8 | ||
require 'markovchain' | ||
|
||
describe Markovchain do | ||
describe '#corpus' do | ||
subject { chain.corpus.storage } | ||
|
||
context 'with 1-gram' do | ||
let(:chain) { Markovchain.new 1 } | ||
|
||
context 'by default' do | ||
it { should == {} } | ||
end | ||
|
||
context 'seeded "a"' do | ||
before { chain.seed("a") } | ||
|
||
it { | ||
should == { | ||
non_word => {"a" => 1}, | ||
"a" => {non_word => 1}, | ||
} | ||
} | ||
end | ||
|
||
context 'seeded "ab"' do | ||
before { chain.seed("ab") } | ||
|
||
it { | ||
should == { | ||
non_word => {"a" => 1}, | ||
"a" => {"b" => 1}, | ||
"b" => {non_word => 1}, | ||
} | ||
} | ||
end | ||
|
||
context 'seeded "abcde"' do | ||
before { chain.seed("abcde") } | ||
|
||
it { | ||
should == { | ||
non_word => {"a" => 1}, | ||
"a" => {"b" => 1}, | ||
"b" => {"c" => 1}, | ||
"c" => {"d" => 1}, | ||
"d" => {"e" => 1}, | ||
"e" => {non_word => 1}, | ||
} | ||
} | ||
end | ||
end | ||
end | ||
end |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters