Skip to content
This repository has been archived by the owner on May 17, 2022. It is now read-only.

Commit

Permalink
Merge branch 'release/v0.0.0'
Browse files Browse the repository at this point in the history
  • Loading branch information
yuya-takeyama committed May 5, 2012
2 parents 378e439 + 8eb1a1d commit 8ae50ff
Show file tree
Hide file tree
Showing 15 changed files with 302 additions and 13 deletions.
2 changes: 1 addition & 1 deletion .rspec
Original file line number Diff line number Diff line change
@@ -1 +1 @@
--color
--color --require spec_helper
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ source "http://rubygems.org"
# Include everything needed to run rake, tests, features, etc.
group :development do
gem "rspec", "~> 2.8.0"
gem "guard-rspec"
gem "rdoc", "~> 3.12"
gem "bundler", "~> 1.1.0"
gem "jeweler", "~> 1.8.3"
Expand Down
8 changes: 8 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,13 @@ GEM
remote: http://rubygems.org/
specs:
diff-lcs (1.1.3)
ffi (1.0.11)
git (1.2.5)
guard (1.0.1)
ffi (>= 0.5.0)
thor (~> 0.14.6)
guard-rspec (0.7.0)
guard (>= 0.10.0)
jeweler (1.8.3)
bundler (~> 1.0)
git (>= 1.2.5)
Expand All @@ -25,12 +31,14 @@ GEM
multi_json (~> 1.3)
simplecov-html (~> 0.5.3)
simplecov-html (0.5.3)
thor (0.14.6)

PLATFORMS
ruby

DEPENDENCIES
bundler (~> 1.1.0)
guard-rspec
jeweler (~> 1.8.3)
rdoc (~> 3.12)
rspec (~> 2.8.0)
Expand Down
6 changes: 6 additions & 0 deletions Guardfile
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

2 changes: 1 addition & 1 deletion LICENSE.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
Copyright (c) 2012 yuyat
Copyright (c) 2012 Yuya Takeyama

Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
Expand Down
5 changes: 2 additions & 3 deletions README.rdoc
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
= markovchain

Description goes here.
Random text generator using Markov Chain algorithm.

== Contributing to markovchain

Expand All @@ -14,6 +14,5 @@ Description goes here.

== Copyright

Copyright (c) 2012 yuyat. See LICENSE.txt for
Copyright (c) 2012 Yuya Takeyama. See LICENSE.txt for
further details.

1 change: 1 addition & 0 deletions VERSION
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
0.0.0
11 changes: 11 additions & 0 deletions examples/example.rb
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
39 changes: 39 additions & 0 deletions lib/markovchain.rb
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
40 changes: 40 additions & 0 deletions lib/markovchain/corpus.rb
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
69 changes: 69 additions & 0 deletions markovchain.gemspec
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

66 changes: 66 additions & 0 deletions spec/lib/markovchain/corpus_spec.rb
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
54 changes: 54 additions & 0 deletions spec/lib/markovchain_spec.rb
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
7 changes: 0 additions & 7 deletions spec/markovchain_spec.rb

This file was deleted.

4 changes: 3 additions & 1 deletion spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,7 @@
Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each {|f| require f}

RSpec.configure do |config|

def non_word
Markovchain::NON_WORD
end
end

0 comments on commit 8ae50ff

Please sign in to comment.