Git Product home page Git Product logo

succubus's Introduction

A random generator based on a generalised Backus-Naur Form grammar

Build StatusCoverage Status

Succubus is a generator which takes stochastic paths through a generalised Backus-Naur Form grammar to produce random text. For instance, the following:

grammar = Succubus::Grammar.new do
  add_rule :base, "I have a <colour> <pet>"
  add_rule :colour, "black", "brown", "white"
  add_rule :pet, "cat", "dog", "rabbit"
end
puts grammar.execute :base

...might output I have a black dog. Or I have a brown rabbit. Or any of the other 7 possible combinations of colour and animal.

See the examples/ folder for more sample generators!

Installation

Succubus is a RubyGem. That means it's really easy to install. Simply run:

gem install succubus

and you're done.

Compatability

Succubus is tested against Ruby 1.9.3 and Ruby 2.0 (by Travis - see the little widget up to check we're still passing). I know of no reason it shouldn't work with Ruby 1.8.7 or 1.9.2 - I just haven't got a test environment that works there yet!

Usage

Everything you need to make and run your own generators is defined in the Succubus::Grammar class.

To get started:

  1. Require in Succubus:

     require 'succubus'
    
  2. Create a new instance of Succubus::Grammar, passing it a block:

     require 'succubus'
     grammar = Succubus::Grammar.new do
       # See step 3 for what goes here
     end
    
  3. Call add_rule within the block. add_rule takes a symbol which names the rule, followed by one or more strings. When the rule is invoked during grammar execution, exactly one of the strings will be chosen and included in the result text. Text in each string is treated literally, except for instances of <foo>, which instead paste in the result of invoking the rule :foo. Rules can be nested as far as you like!

     require 'succubus'
     grammar = Succubus::Grammar.new do
       add_rule :silly_name, "<title> <adjective><noun>"
       add_rule :title, "Mr.", "Mrs.", "Professor", "Little Miss"
       add_rule :adjective "<smelladj>", "<colour>", "Smarty"
       add_rule :smelladj, "Smelly", "Poopy", "Floral"
       add_rule :colour, "Green", "Purple"
       add_rule :noun, "pants", "brain", "banana", "nose"
     end
    
  4. Call execute(<rule>) on your grammar, where <rule> is the symbol naming the top-level rule you want to invoke:

     require 'succubus'
     grammar = Succubus::Grammar.new do
       add_rule :silly_name, "<title> <adjective><noun>"
       add_rule :title, "Mr.", "Mrs.", "Professor", "Little Miss"
       add_rule :adjective, "<smelladj>", "<colour>", "Smarty"
       add_rule :smelladj, "Smelly", "Poopy", "Floral"
       add_rule :colour, "Green", "Purple"
       add_rule :noun, "pants", "brain", "banana", "nose"
     end
     
     puts grammar.execute(:silly_name)
     # Professor Purplepants
    

About that name...?

Oh, why Succubus? Well, Backus-Naur is usually used to describe or verify legal sentences; what you have here is something that uses Backup-Naur to generate sentences. Which is kinda backwards. "Backus", backwards, is "Sukcab"; it's just a short hop from there to Succubus.

I did consider "Bacchus" - a sort of drunken Backus - but the gem name was taken.

succubus's People

Contributors

asilano avatar

Stargazers

 avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

Forkers

loadedsith

succubus's Issues

[Broken] on 1.8.7, The default OSX Ruby in Mountain Lion

I've been able to get succubus to run, but when i run the default examples it returns null. I assume this is not a big deal, as upgrading is easy, but if you can take a look at what I've done and spot what I'm missing, it might be worth the time.

https://github.com/loadedsith/succubus

I made my changes to grammar.rb because of this stackoverflow question

I've tried to verify my changes to generator.rb on rubular.com, as evidenced in the screen shots.
screen shot 2013-07-18 at 10 57 11 pm
screen shot 2013-07-18 at 10 57 05 pm

Thanks for making the version that works on 2!

Improve Grammar::new

Would instance_eval work? Or changing binding?

Also, could probably just pass a Hash from names to String array.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.