codshit 0.1.1
Sign up to get free protection for your applications and to get access to all the features.
- data/README.markdown +90 -0
- data/Rakefile +12 -0
- data/VERSION +1 -0
- data/lib/codshit.rb +111 -0
- metadata +65 -0
data/README.markdown
ADDED
@@ -0,0 +1,90 @@
|
|
1
|
+
codshit
|
2
|
+
=======
|
3
|
+
|
4
|
+
© 2010 Jason Frame [ [jason@onehackoranother.com](mailto:jason@onehackoranother.com) / [@jaz303](http://twitter.com/jaz303) ]
|
5
|
+
Released under the MIT License.
|
6
|
+
|
7
|
+
**codshit** n.
|
8
|
+
nonsense or go nowhere conversation, specifically conversations where all parties are loaded.
|
9
|
+
|
10
|
+
Codshit is a DSL for generating language based on a bunch of rules. The idea/syntax was inspired by (i.e. stolen from) http://eigenclass.org/hiki/language-generator. I suspect it could be useful for generating masses of semi-sensible test data for social media websites but I found it a lot more entertaining just to generate random insults.
|
11
|
+
|
12
|
+
Storyteller Example
|
13
|
+
-------------------
|
14
|
+
|
15
|
+
require 'codshit'
|
16
|
+
|
17
|
+
g = Codshit::Generator.new do |g|
|
18
|
+
|
19
|
+
# default non-terminal is main, can be set with "start :whatever"
|
20
|
+
main.is :salutation, " ", :name, " let me tell you a story... ", :story
|
21
|
+
|
22
|
+
# choice
|
23
|
+
salutation.is "hello" | "awrite" | "sup" | "word"
|
24
|
+
|
25
|
+
# procs are captured and are stable for the duration of the evaluation
|
26
|
+
name.is { ("jason" | "john" | "alice" | "bob" | ["old " | "young ", "man" | "woman"]).to_text }
|
27
|
+
|
28
|
+
insult.is "dickhead" | "bawbag" | "fucknut" | "scrotum"
|
29
|
+
|
30
|
+
venue.is "cinema" | "club" | "mall"
|
31
|
+
attacked.is "punched" | "kicked" | "bit" | "poked"
|
32
|
+
target.is "head" | "ass" | "balls" | "face" | "leg" | "arm"
|
33
|
+
|
34
|
+
story_venue.is { :venue }
|
35
|
+
|
36
|
+
# choice
|
37
|
+
story.define do
|
38
|
+
is "so there i was at the ", :story_venue, " when some ", :insult, " comes up to me and calls me a ", :insult, " so i ", :attacked, " that little ", :insult, " in the ", :target, ". i swear, that's the last time i'm ever going to the ", :story_venue, " again."
|
39
|
+
is "i went into a maze and ", :maze, :maze_cons
|
40
|
+
is "on second thoughts i can't be fucked."
|
41
|
+
end
|
42
|
+
|
43
|
+
# recursion
|
44
|
+
maze.is "went forward" | "went left" | "went right" | "went back" | "sat down for a rest"
|
45
|
+
maze_cons.is " then ", ([:maze, :maze_cons] | [:maze, :maze_cons] | [:maze, :maze_cons] | "found the exit" | "died of starvation")
|
46
|
+
|
47
|
+
end
|
48
|
+
|
49
|
+
puts g.generate
|
50
|
+
|
51
|
+
Possible output:
|
52
|
+
|
53
|
+
word young man let me tell you a story... so there i was at the cinema when some dickhead comes up to me and calls me a bawbag so i poked that little scrotum in the arm. i swear, that's the last time i'm ever going to the club again.
|
54
|
+
|
55
|
+
Our how about:
|
56
|
+
|
57
|
+
sup alice let me tell you a story... i went into a maze and sat down for a rest then went left then went back then sat down for a rest then went forward then went left then went left then died of starvation
|
58
|
+
|
59
|
+
Applications in the Decision Making Process
|
60
|
+
-------------------------------------------
|
61
|
+
|
62
|
+
You could even use codshit to pick your next pet-project:
|
63
|
+
|
64
|
+
g = Codshit::Generator.new do |g|
|
65
|
+
|
66
|
+
main.is "a ", :project_type, " written in ", :implementation_language
|
67
|
+
|
68
|
+
project_type.is :project_involving_parser | "CMS" | "templating language" | "web framework" | "key-value store"
|
69
|
+
|
70
|
+
project_involving_parser.is ["templating engine" | :programming_language, " parsed using ", :parser]
|
71
|
+
parser.is "regular expressions and blind faith" | "SableCC" | ["a buggy hand-written lexer" | "Ragel" | "flex", " and ", "a dubious hand-crafted recursive descent parser" | "lemon" | "bison" | "peg/leg"]
|
72
|
+
|
73
|
+
programming_language.is :typing, ", ", :compiled, " language"
|
74
|
+
typing.is "dynamic" | "statically-typed"
|
75
|
+
compiled.is "compiled" | "interpreted"
|
76
|
+
|
77
|
+
implementation_language.is :language | ["an implementation of ", :language, " running on the ", :virtual_machine]
|
78
|
+
|
79
|
+
language.is "PHP" | "Java" | "Ruby" | "Python" | "Scala" | "Erlang" | "Javascript" | "CLOS" | "Haskell" | "Factor" | "OCaml" | "IO" | "Ioke" | "Potion"
|
80
|
+
virtual_machine.is "JVM" | "CLR"
|
81
|
+
|
82
|
+
end
|
83
|
+
|
84
|
+
puts g.generate
|
85
|
+
|
86
|
+
Generates:
|
87
|
+
|
88
|
+
a statically-typed, compiled language parsed using Ragel and a dubious hand-crafted recursive descent parser written in an implementation of Ioke running on the CLR
|
89
|
+
|
90
|
+
Have fun! xxx
|
data/Rakefile
ADDED
@@ -0,0 +1,12 @@
|
|
1
|
+
begin
|
2
|
+
require 'jeweler'
|
3
|
+
Jeweler::Tasks.new do |gemspec|
|
4
|
+
gemspec.name = "codshit"
|
5
|
+
gemspec.summary = "Rule-based natural language generator"
|
6
|
+
gemspec.email = "jason@onehackoranother.com"
|
7
|
+
gemspec.homepage = "http://github.com/jaz303/codshit"
|
8
|
+
gemspec.authors = ["Jason Frame"]
|
9
|
+
end
|
10
|
+
rescue LoadError
|
11
|
+
puts "Jeweler not available. Install it with: gem install jeweler"
|
12
|
+
end
|
data/VERSION
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
0.1.1
|
data/lib/codshit.rb
ADDED
@@ -0,0 +1,111 @@
|
|
1
|
+
module Codshit
|
2
|
+
|
3
|
+
module Chooseable
|
4
|
+
def |(other)
|
5
|
+
Codshit::Choice.new(*(self.choices + other.choices))
|
6
|
+
end
|
7
|
+
|
8
|
+
def choices
|
9
|
+
[self]
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
13
|
+
class Choice
|
14
|
+
attr_reader :choices
|
15
|
+
|
16
|
+
def initialize(*choices)
|
17
|
+
@choices = choices
|
18
|
+
end
|
19
|
+
|
20
|
+
def |(right)
|
21
|
+
self.class.new(*(choices + right.choices))
|
22
|
+
end
|
23
|
+
|
24
|
+
def to_text
|
25
|
+
@choices[rand(@choices.length)].to_text
|
26
|
+
end
|
27
|
+
|
28
|
+
def <<(c)
|
29
|
+
@choices << c
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
class Generator
|
34
|
+
|
35
|
+
@@active_generator = nil
|
36
|
+
def self.active_generator
|
37
|
+
@@active_generator
|
38
|
+
end
|
39
|
+
|
40
|
+
def initialize(&block)
|
41
|
+
@rules = {}
|
42
|
+
instance_eval(&block)
|
43
|
+
end
|
44
|
+
|
45
|
+
def [](rule)
|
46
|
+
@rules[rule]
|
47
|
+
end
|
48
|
+
|
49
|
+
def method_missing(method, *args, &block)
|
50
|
+
@rules[method] ||= Rule.new
|
51
|
+
end
|
52
|
+
|
53
|
+
def generate
|
54
|
+
@@active_generator = self
|
55
|
+
@rules[:main].to_text
|
56
|
+
end
|
57
|
+
end
|
58
|
+
|
59
|
+
class Rule
|
60
|
+
def initialize
|
61
|
+
@is = Choice.new
|
62
|
+
end
|
63
|
+
|
64
|
+
def is(*stuff)
|
65
|
+
if block_given?
|
66
|
+
@is << Choice.new(yield)
|
67
|
+
else
|
68
|
+
@is << stuff
|
69
|
+
end
|
70
|
+
end
|
71
|
+
|
72
|
+
def define(&block)
|
73
|
+
instance_eval(&block)
|
74
|
+
end
|
75
|
+
|
76
|
+
def to_text
|
77
|
+
@is.to_text
|
78
|
+
end
|
79
|
+
end
|
80
|
+
|
81
|
+
end
|
82
|
+
|
83
|
+
class String
|
84
|
+
include Codshit::Chooseable
|
85
|
+
|
86
|
+
def to_text
|
87
|
+
self
|
88
|
+
end
|
89
|
+
end
|
90
|
+
|
91
|
+
class Symbol
|
92
|
+
include Codshit::Chooseable
|
93
|
+
|
94
|
+
def to_text
|
95
|
+
Codshit::Generator.active_generator[self].to_text
|
96
|
+
end
|
97
|
+
end
|
98
|
+
|
99
|
+
class Array
|
100
|
+
def |(other)
|
101
|
+
::Codshit::Choice.new(*(choices + other.choices))
|
102
|
+
end
|
103
|
+
|
104
|
+
def choices
|
105
|
+
[dup]
|
106
|
+
end
|
107
|
+
|
108
|
+
def to_text
|
109
|
+
map { |p| p.to_text }.join('')
|
110
|
+
end
|
111
|
+
end
|
metadata
ADDED
@@ -0,0 +1,65 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: codshit
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
prerelease: false
|
5
|
+
segments:
|
6
|
+
- 0
|
7
|
+
- 1
|
8
|
+
- 1
|
9
|
+
version: 0.1.1
|
10
|
+
platform: ruby
|
11
|
+
authors:
|
12
|
+
- Jason Frame
|
13
|
+
autorequire:
|
14
|
+
bindir: bin
|
15
|
+
cert_chain: []
|
16
|
+
|
17
|
+
date: 2010-03-08 00:00:00 +00:00
|
18
|
+
default_executable:
|
19
|
+
dependencies: []
|
20
|
+
|
21
|
+
description:
|
22
|
+
email: jason@onehackoranother.com
|
23
|
+
executables: []
|
24
|
+
|
25
|
+
extensions: []
|
26
|
+
|
27
|
+
extra_rdoc_files:
|
28
|
+
- README.markdown
|
29
|
+
files:
|
30
|
+
- README.markdown
|
31
|
+
- Rakefile
|
32
|
+
- VERSION
|
33
|
+
- lib/codshit.rb
|
34
|
+
has_rdoc: true
|
35
|
+
homepage: http://github.com/jaz303/codshit
|
36
|
+
licenses: []
|
37
|
+
|
38
|
+
post_install_message:
|
39
|
+
rdoc_options:
|
40
|
+
- --charset=UTF-8
|
41
|
+
require_paths:
|
42
|
+
- lib
|
43
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ">="
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
segments:
|
48
|
+
- 0
|
49
|
+
version: "0"
|
50
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
segments:
|
55
|
+
- 0
|
56
|
+
version: "0"
|
57
|
+
requirements: []
|
58
|
+
|
59
|
+
rubyforge_project:
|
60
|
+
rubygems_version: 1.3.6
|
61
|
+
signing_key:
|
62
|
+
specification_version: 3
|
63
|
+
summary: Rule-based natural language generator
|
64
|
+
test_files: []
|
65
|
+
|