GithubHelp home page GithubHelp logo

blazerw / spintax_parser Goto Github PK

View Code? Open in Web Editor NEW
4.0 1.0 0.0 24 KB

Crystal Shard that provides a mixin to parse "spintax", a text format used for automated article generation. Can handle nested spintax, and can count the total number of unique variations.

License: MIT License

Crystal 100.00%
crystal crystal-language crystal-lang crystal-shards crystal-shard spintax

spintax_parser's Introduction

spintax_parser

A mixin to parse "spintax", a text format used for automated article generation. Can handle nested spintax, and can count the total number of unique variations.

Based on Ruby version, GitHub (So, is this README. :^) )

Docs GitHub release

Installation

  1. Add the dependency to your shard.yml:
dependencies:
  spintax_parser:
    github: blazerw/spintax_parser
  1. Run shards install

Usage

require "spintax_parser"

class String
  include SpintaxParser
end

Then you can safely call #unspin on any string in your application:

spintext = "{Hello|Hi} {{world|worlds}|planet}{!|.|?}"
10.times do
  puts spintext.unspin
end

Run the code above, and you will end up with several random variations of the same text, such as:

Hi worlds.
Hi planet?
Hello world?
Hi planet?
Hi world?
Hi world!
Hi world.
Hello world.
Hello world!
Hello worlds.

And don't worry: calling #unspin on a string with no spintax will safely return an unaffected copy of the string.

Also, note that the #unspin method doesn't really care if the class you mix it into is a descendant of String or not, as long as its #to_s method returns a string written in spintax.

Consistent unspinning

Got a special project that requires unspinning the same spintax the same way in certain circumstances? No problem. If you're using a Ruby version >= 1.9.3, you can pass a pre-seeded random number generator to the #unspin method just like you would to the Array#sample method. Et voila! Consistent unspinning!

seed = Random::PCG32.new.new_seed
spintext.unspin Random.new(seed)  # => "Hello world!"
spintext.unspin Random.new(seed)  # => "Hello world!"

Counting total variations

You can also count the total number of unique variations of a spintax string. If you've mixed the SpintaxParser into your String class like above, just call the #count_spintax_variations method on any string as shown below:

spintext = "{Hello|Hi} {{world|worlds}|planet}{!|.|?}"
spintext.count_spintax_variations  # => 18

NOTE: The following currently fails:

expect("{one|two|}".count_spintax_variations).to eq 3

Development

Follow Crystal's guidelines: https://crystal-lang.org/reference/guides/writing_shards.html and realize that Spec2 is used for testing, specifically this fork

Contributing

  1. Fork it (https://github.com/blazerw/spintax_parser/fork)
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Add some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create a new Pull Request

Contributors

spintax_parser's People

Contributors

blazerw avatar

Stargazers

 avatar  avatar  avatar  avatar

Watchers

 avatar

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.