brane 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (8) hide show
  1. data/.gitignore +17 -0
  2. data/Gemfile +3 -0
  3. data/LICENSE +22 -0
  4. data/README.md +35 -0
  5. data/Rakefile +2 -0
  6. data/brane.gemspec +17 -0
  7. data/lib/brane.rb +84 -0
  8. metadata +53 -0
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source :rubygems
2
+
3
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2012 Ross Paffett
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.
@@ -0,0 +1,35 @@
1
+ Brane is a simple [Markov chain](http://en.wikipedia.org/wiki/Markov_chain) generator which stores its data in a [Tokyo Cabinet](http://fallabs.com/tokyocabinet/) database.
2
+
3
+ ### Installation
4
+
5
+ $ gem install brane
6
+
7
+ ### Usage
8
+
9
+ By default, `Brane.new` will create a `brane.tcb` Tokyo Cabinet database in the current directory. If you'd like, you may supply a path: `Brane.new("/usr/local/etc/brane/brane.tcb")`.
10
+
11
+ ```ruby
12
+ brane = Brane.new
13
+
14
+ File.open("chatter.txt") do |f|
15
+ f.each_line do |line|
16
+ brane.add(line)
17
+ end
18
+ end
19
+
20
+ 5.times do
21
+ puts brane.sentence #=> "my legs are almost better than youtube comments"
22
+ end
23
+
24
+ brain.sleep
25
+ ```
26
+
27
+ ### License <small>(MIT)</small>
28
+
29
+ Copyright (c) 2012 Ross Paffett
30
+
31
+ Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
32
+
33
+ The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
34
+
35
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,2 @@
1
+ #!/usr/bin/env rake
2
+ require "bundler/gem_tasks"
@@ -0,0 +1,17 @@
1
+ # -*- encoding: utf-8 -*-
2
+ require File.expand_path("../lib/brane", __FILE__)
3
+
4
+ Gem::Specification.new do |gem|
5
+ gem.authors = ["Ross Paffett"]
6
+ gem.email = ["ross@rosspaffett.com"]
7
+ gem.description = "Simple Markov chain generator using Tokyo Cabinet"
8
+ gem.summary = gem.description
9
+ gem.homepage = "https://github.com/raws/brane"
10
+
11
+ gem.files = `git ls-files`.split($\)
12
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
13
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
14
+ gem.name = "brane"
15
+ gem.require_paths = ["lib"]
16
+ gem.version = Brane::VERSION
17
+ end
@@ -0,0 +1,84 @@
1
+ require "oklahoma_mixer"
2
+
3
+ class Brane
4
+ VERSION = "0.0.1"
5
+ attr_reader :db
6
+
7
+ def initialize(path = "brane.tcb")
8
+ path = File.expand_path(path.sub(/\.[^\.]*$/, ".tcb"))
9
+ @db = OklahomaMixer.open(path)
10
+ end
11
+
12
+ def add(text)
13
+ text.split(/\b[!?\.]+\s*/).each do |sentence|
14
+ sentence.split(/\s+/).map do |word|
15
+ normalize_word(word)
16
+ end.tap do |words|
17
+ return if words.length < 3
18
+ db.store("global:start", words.first, :dup)
19
+ db.store("global:end", words.last, :dup)
20
+ end.each_cons(3) do |before, current, after|
21
+ current.downcase!
22
+ db.store("before:#{current}", before, :dup)
23
+ db.store("after:#{current}", after, :dup)
24
+ end
25
+ end
26
+ end
27
+
28
+ def sentence(seed = random_word)
29
+ sentence = [seed]
30
+
31
+ while !starter?(sentence.first)
32
+ if (words = db.values("before:#{sentence.first.downcase}")) && !words.empty?
33
+ sentence.unshift(least_common(words))
34
+ else
35
+ break
36
+ end
37
+ end
38
+
39
+ while !terminator?(sentence.last)
40
+ if (words = db.values("after:#{sentence.last.downcase}")) && !words.empty?
41
+ sentence.push(least_common(words))
42
+ else
43
+ break
44
+ end
45
+ end
46
+
47
+ sentence.join(" ")
48
+ end
49
+
50
+ def size
51
+ db.keys(prefix: "after:").size
52
+ end
53
+
54
+ def sleep
55
+ db.flush
56
+ db.close
57
+ end
58
+
59
+ private
60
+ def normalize_word(word)
61
+ word.gsub("\"", "")
62
+ end
63
+
64
+ def random_word
65
+ db.keys(prefix: "after:").sample.slice(6..-1)
66
+ end
67
+
68
+ def starter?(word)
69
+ db.values("global:start").include?(word.downcase)
70
+ end
71
+
72
+ def terminator?(word)
73
+ db.values("global:end").include?(word.downcase)
74
+ end
75
+
76
+ def frequencies(words)
77
+ hash = Hash.new { |hash, key| hash[key] = 0 }
78
+ words.inject(hash) { |hash, word| hash[word] += 1; hash }
79
+ end
80
+
81
+ def least_common(words)
82
+ frequencies(words).min_by { |word, freq| freq }.first
83
+ end
84
+ end
metadata ADDED
@@ -0,0 +1,53 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: brane
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Ross Paffett
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-06-25 00:00:00.000000000 Z
13
+ dependencies: []
14
+ description: Simple Markov chain generator using Tokyo Cabinet
15
+ email:
16
+ - ross@rosspaffett.com
17
+ executables: []
18
+ extensions: []
19
+ extra_rdoc_files: []
20
+ files:
21
+ - .gitignore
22
+ - Gemfile
23
+ - LICENSE
24
+ - README.md
25
+ - Rakefile
26
+ - brane.gemspec
27
+ - lib/brane.rb
28
+ homepage: https://github.com/raws/brane
29
+ licenses: []
30
+ post_install_message:
31
+ rdoc_options: []
32
+ require_paths:
33
+ - lib
34
+ required_ruby_version: !ruby/object:Gem::Requirement
35
+ none: false
36
+ requirements:
37
+ - - ! '>='
38
+ - !ruby/object:Gem::Version
39
+ version: '0'
40
+ required_rubygems_version: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ requirements: []
47
+ rubyforge_project:
48
+ rubygems_version: 1.8.23
49
+ signing_key:
50
+ specification_version: 3
51
+ summary: Simple Markov chain generator using Tokyo Cabinet
52
+ test_files: []
53
+ has_rdoc: