Royce  Reinger

Royce Reinger

1659126300

Generate Strings That Match A Given Regular Expression

regexp-examples 

Extends the Regexp class with the methods: Regexp#examples and Regexp#random_example

Regexp#examples generates a list of all* strings that will match the given regular expression.

Regexp#random_example returns one, random string (from all possible strings!!) that matches the regex.

* If the regex has an infinite number of possible strings that match it, such as /a*b+c{2,}/, or a huge number of possible matches, such as /.\w/, then only a subset of these will be listed. For more detail on this, see configuration options.

If you'd like to understand how/why this gem works, please check out my blog post about it.

Usage

Regexp#examples

/a*/.examples #=> ['', 'a', 'aa']
/ab+/.examples #=> ['ab', 'abb', 'abbb']
/this|is|awesome/.examples #=> ['this', 'is', 'awesome']
/https?:\/\/(www\.)?github\.com/.examples #=> ['http://github.com',
  # 'http://www.github.com', 'https://github.com', 'https://www.github.com']
/(I(N(C(E(P(T(I(O(N)))))))))*/.examples #=> ["", "INCEPTION", "INCEPTIONINCEPTION"]
/\x74\x68\x69\x73/.examples #=> ["this"]
/what about (backreferences\?) \1/.examples
  #=> ['what about backreferences? backreferences?']
/
  \u{28}\u2022\u{5f}\u2022\u{29}
  |
  \u{28}\u{20}\u2022\u{5f}\u2022\u{29}\u{3e}\u2310\u25a0\u{2d}\u25a0\u{20}
  |
  \u{28}\u2310\u25a0\u{5f}\u25a0\u{29}
/x.examples #=> ["(•_•)", "( •_•)>⌐■-■ ", "(⌐■_■)"]

Regexp#random_example

Obviously, you will get different (random) results if you try these yourself!

/\w{10}@(hotmail|gmail)\.com/.random_example #=> "TTsJsiwzKS@gmail.com"
/5[1-5][0-9]{14}/.random_example #=> "5224028604559821" (A valid MasterCard number)
/\p{Greek}{80}/.random_example
  #=> "ΖΆΧͷᵦμͷηϒϰΟᵝΔ΄θϔζΌψΨεκᴪΓΕπι϶ονϵΓϹᵦΟπᵡήϴϜΦϚϴϑ͵ϴΉϺ͵ϹϰϡᵠϝΤΏΨϹϊϻαώΞΰϰΑͼΈΘͽϙͽξΆΆΡΡΉΓς"
/written by tom lord/i.random_example #=> "WrITtEN bY tOM LORD"

Supported ruby versions

MRI 2.4.0 (oldest non-EOL version) --> 3.0.0 (latest stable version)

MRI 2.0.0 --> 2.3.x were supported until version 1.5.0 of this library. Support was dropped primarily because of the need to use RbConfig::CONFIG['UNICODE_VERSION'], which was added to ruby version 2.4.0.

MRI versions ≤ 1.9.3 were never supported by this library. This is primarily because MRI 2.0.0 introduced a new regexp engine (Oniguruma was replaced by Onigmo -- For example, named properties like /\p{Alpha}/ are illegal syntax on MRI 1.9.3.). Whilst most of this gem could be made to work with MRI 1.9.x (or even 1.8.x), I considered the changes too significant to implement backwards compatability (especially since long-term support for MRI 1.9.3 has long ended).

Other implementations, such as JRuby, could probably work fine - but I haven't fully tried/tested it. Pull requests are welcome.

Installation

Add this line to your application's Gemfile:

gem 'regexp-examples'

And then execute:

$ bundle

Or install it yourself as:

$ gem install regexp-examples

Supported syntax

Short answer: Everything is supported, apart from "irregular" aspects of the regexp language -- see impossible features.

Long answer:

All forms of repeaters (quantifiers), e.g. /a*/, /a+/, /a?/, /a{1,4}/, /a{3,}/, /a{,2}/

  • Reluctant and possissive repeaters work fine, too, e.g. /a*?/, /a*+/

Boolean "Or" groups, e.g. /a|b|c/

Character sets, e.g. /[abc]/ - including:

  • Ranges, e.g./[A-Z0-9]/
  • Negation, e.g. /[^a-z]/
  • Escaped characters, e.g. /[\w\s\b]/
  • POSIX bracket expressions, e.g. /[[:alnum:]]/, /[[:^space:]]/
    • ...Taking the current ruby version into account - e.g. the definition of /[[:punct:]]/ changed in version 2.4.0.
  • Set intersection, e.g. /[[a-h]&&[f-z]]/

Escaped characters, e.g. /\n/, /\w/, /\D/ (and so on...)

Capture groups, e.g. /(group)/

  • Including named groups, e.g. /(?<name>group)/
  • And backreferences(!!!), e.g. /(this|that) \1/ /(?<name>foo) \k<name>/
  • ...even for the more "obscure" syntax, e.g. /(?<future>the) \k'future'/, /(a)(b) \k<-1>/
  • ...and even if nested or optional, e.g. /(even(this(works?))) \1 \2 \3/, /what about (this)? \1/
  • Non-capture groups, e.g. /(?:foo)/
  • Comment groups, e.g. /foo(?#comment)bar/
  • Absent operator groups, e.g. /(?~exp)/ This feature is available in ruby version >= 2.4.1. However, support in this gem is limited.

Control characters, e.g. /\ca/, /\cZ/, /\C-9/

Escape sequences, e.g. /\x42/, /\x5word/, /#{"\x80".force_encoding("ASCII-8BIT")}/

Unicode characters, e.g. /\u0123/, /\uabcd/, /\u{789}/

Octal characters, e.g. /\10/, /\177/

Named properties, e.g. /\p{L}/ ("Letter"), /\p{Arabic}/ ("Arabic character") , /\p{^Ll}/ ("Not a lowercase letter"), /\P{^Canadian_Aboriginal}/ ("Not not a Canadian aboriginal character")

  • ...Even between different ruby versions!! (e.g. /\p{Arabic}/.examples(max_group_results: 999) will give you a different answer in ruby v2.1.x and v2.2.x)

Arbitrarily complex combinations of all the above!

Regexp options can also be used:

  • Case insensitive examples: /cool/i.examples #=> ["cool", "cooL", "coOl", "coOL", ...]
  • Multiline examples: /./m.examples #=> ["\n", "a", "b", "c", "d"]
  • Extended form examples: /line1 #comment \n line2/x.examples #=> ["line1line2"]
  • Options toggling supported: /before(?imx-imx)after/, /before(?imx-imx:subexpr)after/

Configuration Options

When generating examples, the gem uses 3 configurable values to limit how many examples are listed:

max_repeater_variance (default = 2) restricts how many examples to return for each repeater. For example:

  • .* is equivalent to .{0,2}
  • .+ is equivalent to .{1,3}
  • .{2,} is equivalent to .{2,4}
  • .{,3} is equivalent to .{0,2}
  • .{3,8} is equivalent to .{3,5}

max_group_results (default = 5) restricts how many characters to return for each "set". For example:

  • \d is equivalent to [01234]
  • \w is equivalent to [abcde]
  • [h-s] is equivalent to [hijkl]
  • (1|2|3|4|5|6|7|8) is equivalent to [12345]

max_results_limit (default = 10000) restricts the maximum number of results that can possibly be generated. For example:

  • /c+r+a+z+y+ * B+I+G+ * r+e+g+e+x+/i.examples.length <= 10000 -- Attempting this will NOT freeze your system, even though (by the above rules) this "should" attempt to generate 117546246144 examples.

Rexexp#examples makes use of all these options; Rexexp#random_example only uses max_repeater_variance, since the other options are redundant.

Defining custom configuration values

To use an alternative value, you can either pass the configuration option as a parameter:

/a*/.examples(max_repeater_variance: 5)
  #=> [''. 'a', 'aa', 'aaa', 'aaaa' 'aaaaa']
/[F-X]/.examples(max_group_results: 10)
  #=> ['F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O']
/[ab]{10}/.examples(max_results_limit: 64).length == 64 # NOT 1024
/[slow]{9}/.examples(max_results_limit: 9999999).length == 4 ** 9 == 262144 # Warning - this will take a while!
/.*/.random_example(max_repeater_variance: 50)
  #=> "A very unlikely result!"

Or, set an alternative value within a block:

RegexpExamples::Config.with_configuration(max_repeater_variance: 5) do
  # ...
end

Or, globally set a different default value:

# e.g In a rails project, you may wish to place this in
# config/initializers/regexp_examples.rb
RegexpExamples::Config.max_repeater_variance = 5
RegexpExamples::Config.max_group_results = 10
RegexpExamples::Config.max_results_limit = 20000

A sensible use case might be, for example, to generate all 1-5 digit strings:

/\d{1,5}/.examples(max_repeater_variance: 4, max_group_results: 10, max_results_limit: 100000)
  #=> ['0', '1', '2', ..., '99998', '99999']

Configuration Notes

Due to code optimisation, Regexp#random_example runs pretty fast even on very complex patterns. (I.e. It's typically a lot faster than using /pattern/.examples.sample(1).) For instance, the following takes no more than ~ 1 second on my machine:

/.*\w+\d{100}/.random_example(max_repeater_variance: 1000)

All forms of configuration mentioned above are thread safe.

Bugs and TODOs

There are no known major bugs with this library. However, there are a few obscure issues that you may encounter.

All known bugs/missing features are documented in GitHub. Please discuss known issues there, or raise a new issue if required. Pull requests are welcome!

Some of the most obscure regexp features are not even mentioned in the ruby docs. However, full documentation on all the intricate obscurities in the ruby (version 2.x) regexp parser can be found here.

Impossible features ("illegal syntax")

The following features in the regex language can never be properly implemented into this gem because, put simply, they are not technically "regular"! If you'd like to understand this in more detail, check out what I had to say in my blog post about this gem.

Using any of the following will raise a RegexpExamples::IllegalSyntax exception:

  • Lookarounds, e.g. /foo(?=bar)/, /foo(?!bar)/, /(?<=foo)bar/, /(?<!foo)bar/
  • Anchors (\b, \B, \G, ^, \A, $, \z, \Z), e.g. /\bword\b/, /line1\n^line2/
    • Anchors are really just special cases of lookarounds!
    • However, a special case has been made to allow ^, \A and \G at the start of a pattern; and to allow $, \z and \Z at the end of pattern. In such cases, the characters are effectively just ignored.
  • Subexpression calls (\g), e.g. /(?<name> ... \g<name>* )/

(Note: Backreferences are not really "regular" either, but I got these to work with a bit of hackery.)

Contributing

  1. Fork it ( https://github.com/tom-lord/regexp-examples/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
  6. Don't forget to add tests!!

Author: tom-lord
Source Code: https://github.com/tom-lord/regexp-examples 
License: MIT license

#ruby #data #random #string 

What is GEEK

Buddha Community

Generate Strings That Match A Given Regular Expression
Brook  Legros

Brook Legros

1659199883

String Pattern: Generate Strings Supplying A Simple Pattern in Ruby

StringPattern

With this gem, you can easily generate strings supplying a very simple pattern. Even generate random words in English or Spanish. Also, you can validate if a text fulfills a specific pattern or even generate a string following a pattern and returning the wrong length, value... for testing your applications. Perfect to be used in test data factories.

Also you can use regular expressions (Regexp) to generate strings: /[a-z0-9]{2,5}\w+/.gen

To do even more take a look at nice_hash gem

Installation

Add this line to your application's Gemfile:

gem 'string_pattern'

And then execute:

$ bundle

Or install it yourself as:

$ gem install string_pattern

Usage

What is a string pattern?

A pattern is a string where we supply these elements "a-b:c" where a is min_length, b is max_length (optional) and c is a set of symbol_type

min_length: minimum length of the string

max_length (optional): maximum length of the string. If not provided, the result will be with the min_length provided

symbol_type: The type of the string we want.
    x: from a to z (lowercase)
    X: A to Z (capital letters)
    L: A to Z and a to z
    T: National characters defined on StringPattern.national_chars
    n or N: for numbers. 0 to 9
    $: special characters, $%&#...  (includes blank space)
    _: blank space
    *: all characters
    0: empty string will be accepted.  It needs to be at the beginning of the symbol_type string
        @: It will generate a valid email following the official algorithm. It cannot be used with other symbol_type
        W: for English words, capital and lower. It cannot be used with other symbol_type
        w: for English words only lower and words separated by underscore. It cannot be used with other symbol_type
        P: for Spanish words, capital and lower. It cannot be used with other symbol_type
        p: for Spanish words only lower and words separated by underscore. It cannot be used with other symbol_type
    

How to generate a string following a pattern

To generate a string following a pattern you can do it using directly the StringPattern class or the generate method in the class, be aware you can always use also the alias method: gen

require 'string_pattern'

#StringPattern class
p StringPattern.generate "10:N"
#>3448910834
p StringPattern.gen "5:X"
#>JDDDK

#String class
p "4:Nx".gen
#>xaa3

#Symbol class
p :"10:T".generate
#>AccBdjklñD

#Array class
p [:"3:N", "fixed", :"3:N"].gen
#>334fixed920
p "(,3:N,) ,3:N,-,2:N,-,2:N".split(',').generate 
#>(937) 980-65-05

#Kernel
p gen "3:N"
#>443

Generating unique strings

If you want to generate for example 1000 strings and be sure all those strings are different you can use:

StringPattern.dont_repeat = true #default: false
1000.times {
    puts :"6-20:L/N/".gen
}
StringPattern.cache_values = Hash.new() #to clean the generated values from memory

Using dont_repeat all the generated string during the current run will be unique.

In case you just want one particular string to be unique but not the rest then add to the pattern just in the end the symbol: &

The pattern needs to be a symbol object.

1000.times {
    puts :"6-20:L/N/&".gen #will be unique
    puts :"10:N".gen
}

Generate words randomly in English or Spanish

To generate a string of the length you want that will include only real words, use the symbol types:

  • W: generates English words following CamelCase ('ExampleOutput')
  • w: generates English words following snake_case ('example_output')
  • P: generates Spanish words following CamelCase ('EjemploSalida')
  • p: generates Spanish words following snake_case ('ejemplo_salida')
require 'string_pattern'

puts '10-30:W'.gen
#> FirstLieutenant
puts '10-30:w'.gen
#> paris_university
puts '10-30:P'.gen
#> SillaMetalizada
puts '10-30:p'.gen
#> despacho_grande

If you want to use a different word separator than "_" when using 'w' or 'p':

# blank space for example
require 'string_pattern'

StringPattern.word_separator = ' '

puts '10-30:w'.gen
#> paris university
puts '10-30:p'.gen
#> despacho grande

The word list is loaded on the first request to generate words, after that the speed to generate words increases amazingly. 85000 English words and 250000 Spanish words. The vocabularies are a sample of public open sources.

Generate strings using Regular Expressions (Regexp)

Take in consideration this feature is not supporting all possibilities for Regular expressions but it is fully functional. If you find any bug or limitation please add it to issues: https://github.com/MarioRuiz/string_pattern/issues

In case you want to change the default maximum for repetitions when using * or +: StringPattern.default_infinite = 30 . By default is 10.

If you want to translate a regular expression into an StringPattern use the method we added to Regexp class: to_sp

Examples:

/[a-z0-9]{2-5}\w+/.to_sp
#> ["2-5:nx", "1-10:Ln_"]

#regular expression for UUID v4
/[0-9A-F]{8}-[0-9A-F]{4}-4[0-9A-F]{3}-[89AB][0-9A-F]{3}-[0-9A-F]{12}/.to_sp
#> ["8:n[ABCDEF]", "-", "4:n[ABCDEF]", "-4", "3:n[ABCDEF]", "-", "1:[89AB]", "3:n[ABCDEF]", "-", "12:n[ABCDEF]"]

If you want to generate a random string following the regular expression, you can do it like a normal string pattern:


regexp = /[0-9A-F]{8}-[0-9A-F]{4}-4[0-9A-F]{3}-[89AB][0-9A-F]{3}-[0-9A-F]{12}/

# using StringPattern class
puts StringPattern.generate(regexp)

# using Kernel
puts generate(regexp)

# using generate method added to Regexp class
puts regexp.generate

#using the alias 'gen'
puts regexp.gen 

# output:
#>7009574B-6F2F-436E-BB7A-EA5FDA6B4E47
#>5FB1718F-108A-4F62-8170-33C43FD86B1D
#>05745B6F-93BA-475F-8118-DD56E5EAC4D1
#>2D6FC189-8D50-45A8-B182-780193838502

String patterns

How to generate one or another string

In case you need to specify that the string is generated selecting one or another fixed string or pattern, you can do it by using Array of patterns and in the position you want you can add an array with the possible values

p ["uno:", :"5:N", ['.red','.green', :'3:L'] ].gen

# first position a fixed string: "uno:"
# second position 5 random numbers
# third position one of these values: '.red', '.green' or 3 letters

# example output: 
# 'uno:34322.red'
# 'uno:44432.green'
# 'uno:34322.red'
# 'uno:28795xAB'

Take in consideration that this is only available to generate successful strings but not for validation

Custom characters

Also, it's possible to provide the characters we want. To do that we'll use the symbol_type [characters]

If we want to add the character ] we have to write ]]

Examples

# four chars from the ones provided: asDF9
p "4:[asDF9]".gen    #> aaaa, asFF, 9sFD

# from 2 to 20 chars, capital and lower chars (Xx) and also valid the characters $#6
p "2-20:[$#6]Xx".gen    #> aaaa, asFF, 66, B$DkKL#9aDD
 
# four chars from these: asDF]9
p "4:[asDF]]9]".gen    #> aa]a, asFF, 9s]D

Required characters or symbol types

We'll use the symbol / to specify which characters or symbols we want to be included on the resulting string as required values /symbols or characters/

If we need to add the character / we'll use //

Examples:

# four characters. optional: capitals and numbers, required: lower
"4:XN/x/".gen    # aaaa, FF9b, j4em, asdf, ADFt

# from 6 to 15 chars. optional: numbers, capitals and the chars $ and Æ. required the chars: 23abCD
"6-15:[/23abCD/$Æ]NX".gen    # bCa$D32, 32DJIOKLaCb, b23aD568C
 
# from 4 to 9 chars. optional: numbers and capitals. required: lowers and the characters $ and 5
"4-9:[/$5/]XN/x/".generate    # aa5$, F5$F9b, j$4em5, a5sdf$, $ADFt5 

Excluded characters

If we want to exclude a few characters in the result, we'll use the symbol %characters%

If you need to exclude the character %, you should use %%

Examples:

# from 2 to 20 characters. optional: Numbers and characters A, B and C. excluded: the characters 8 and 3
"2-20:[%83%ABC]N".gen    # B49, 22900, 9CAB, 22, 11CB6270C26C4572A50C

# 10 chars. optional: Letters (capital and lower). required: numbers. excluded: the characters 0 and WXYzZ
"10:L/n/[%0WXYzZ%]".gen    # GoO2ukCt4l, Q1Je2remFL, qPg1T92T2H, 4445556781

Not fulfilling a pattern

If we want our resulting string doesn't fulfill the pattern we supply, then we'll use the symbol ! at the beginning

Examples:

"!4:XN/x/".gen    # a$aaa, FF9B, j4DDDem, as, 2345

"!10:N".gen     # 123, 34899Add34, 3434234234234008, AAFj#kd2x

Generate a string with specific expected errors

Usually, for testing purposes you need to generate strings that don't fulfill a specific pattern, then you can supply as a parameter expected_errors (alias: errors)

The possible values you can specify is one or more of these ones: :length, :min_length, :max_length, :value, :required_data, :excluded_data, :string_set_not_allowed

:length: wrong length, minimum or maximum
:min_length: wrong minimum length
:max_length: wrong maximum length
:value: wrong resultant value
:required_data: the output string won't include all necessary required data. It works only if required data supplied on the pattern.
:excluded_data: the resultant string will include one or more characters that should be excluded. It works only if excluded data supplied on the pattern.
:string_set_not_allowed: it will include one or more characters that are not supposed to be on the string.

Examples:

"10-20:N".gen errors: [:min_length]
#> 627, 098262, 3408

"20:N".gen errors: [:length, :value]
#> |13, tS1b)r-1)<RT65202eTo6bV0g~, 021400323<2ahL0NP86a698063*56076

"10:L/n/".gen errors: [:value]
#> 1hwIw;v{KQ, mpk*l]!7:!, wocipgZt8@

Validate if a string is following a pattern

If you need to validate if a specific text is fulfilling the pattern you can use the validate method.

If a string pattern supplied and no other parameters supplied the output will be an array with the errors detected.

Possible output values, empty array (validation without errors detected) or one or more of: :min_length, :max_length, :length, :value, :string_set_not_allowed, :required_data, :excluded_data

In case an array of patterns supplied it will return only true or false

Examples:

#StringPattern class
StringPattern.validate((text: "This text will be validated", pattern: :"10-20:Xn")
#> [:max_length, :length, :value, :string_set_not_allowed]

#String class
"10:N".validate "333444"
#> [:min_length, :length]

#Symbol class
:"10:N".validate("333444")
#> [:min_length, :length]

#Array class
["5:L","3:xn","4-10:n"].validate "DjkljFFc343444390"
#> false

If we want to validate a string with a pattern and we are expecting to get specific errors, you can supply the parameter expected_errors (alias: errors) or not_expected_errors (aliases: non_expected_errors, not_errors).

In this case, the validate method will return true or false.

Examples:

"10:N".val "3445", errors: [:min_length]
#> true

"10:N/[09]/".validate "4434039440", errors: [:value]
#> false

"10-12:XN/x/".validate "FDDDDDAA343434", errors: [:max_length, :required_data]
#> true

Configure

SP_ADD_TO_RUBY

This gem adds the methods generate (alias: gen) and validate (alias: val) to the Ruby classes: String, Array, and Symbol.

Also adds the method generate (alias: gen) to Kernel. By default (true) it is always added.

In case you don't want to be added, just before requiring the library set:

SP_ADD_TO_RUBY = false
require 'string_pattern'

In case it is set to true (default) then you will be able to use:

require 'string_pattern'

#String object
"20-30:@".gen 
#>dkj34MljjJD-df@jfdluul.dfu

"10:L/N/[/-./%d%]".validate("12ds6f--.s") 
#>[:value, :string_set_not_allowed]

"20-40:@".validate(my_email)

#Kernel
gen "10:N"
#>3433409877

#Array object
"(,3:N,) ,3:N,-,2:N,-,2:N".split(",").generate 
#>(937) 980-65-05

%w{( 3:N ) 1:_ 3:N - 2:N - 2:N}.gen 
#>(045) 448-63-09

["1:L", "5-10:LN", "-", "3:N"].gen 
#>zqWihV-746

national_chars

To specify which national characters will be used when using the symbol type: T, you use StringPattern.national_chars, by default is the English alphabet

StringPattern.national_chars = (('a'..'z').to_a + ('A'..'Z').to_a).join + "áéíóúÁÉÍÓÚüÜñÑ"
"10-20:Tn".gen #>AAñ34Ef99éNOP

optimistic

If true it will check on the strings of the array positions supplied if they have the pattern format and assume in that case that is a pattern. If not it will assume the patterns on the array will be supplied as symbols. By default is set to true.

StringPattern.optimistic = false
["5:X","fixedtext", "3:N"].generate
#>5:Xfixedtext3:N
[:"5:X","fixedtext", :"3:N"].generate
#>AUJKJfixedtext454

StringPattern.optimistic = true
["5:X","fixedtext", "3:N"].generate
#>KKDMEfixedtext344
[:"5:X","fixedtext", :"3:N"].generate
#>SAAERfixedtext988

Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/marioruiz/string_pattern.

License

The gem is available as open source under the terms of the MIT License.


Author: MarioRuiz
Source code: https://github.com/MarioRuiz/string_pattern
License: MIT license

#ruby  #ruby-on-rails 

Mad Libs: Using regular expressions

From Tiny Python Projects by Ken Youens-Clark

Everyone loves Mad Libs! And everyone loves Python. This article shows you how to have fun with both and learn some programming skills along the way.


Take 40% off Tiny Python Projects by entering fccclark into the discount code box at checkout at manning.com.


When I was a wee lad, we used to play at Mad Libs for hours and hours. This was before computers, mind you, before televisions or radio or even paper! No, scratch that, we had paper. Anyway, the point is we only had Mad Libs to play, and we loved it! And now you must play!

We’ll write a program called mad.py  which reads a file given as a positional argument and finds all the placeholders noted in angle brackets like <verb>  or <adjective> . For each placeholder, we’ll prompt the user for the part of speech being requested like “Give me a verb” and “Give me an adjective.” (Notice that you’ll need to use the correct article.) Each value from the user replaces the placeholder in the text, and if the user says “drive” for “verb,” then <verb>  in the text replaces with drive . When all the placeholders have been replaced with inputs from the user, print out the new text.

#python #regular-expressions #python-programming #python3 #mad libs: using regular expressions #using regular expressions

amelia jones

1591340335

How To Take Help Of Referencing Generator

APA Referencing Generator

Many students use APA style as the key citation style in their assignment in university or college. Although, many people find it quite difficult to write the reference of the source. You ought to miss the names and dates of authors. Hence, APA referencing generator is important for reducing the burden of students. They can now feel quite easy to do the assignments on time.

The functioning of APA referencing generator

If you are struggling hard to write the APA referencing then you can take the help of APA referencing generator. It will create an excellent list. You are required to enter the information about the source. Just ensure that the text is credible and original. If you will copy references then it is a copyright violation.

You can use a referencing generator in just a click. It will generate the right references for all the sources. You are required to organize in alphabetical order. The generator will make sure that you will get good grades.

How to use APA referencing generator?

Select what is required to be cited such as journal, book, film, and others. You can choose the type of required citations list and enter all the required fields. The fields are dates, author name, title, editor name, and editions, name of publishers, chapter number, page numbers, and title of journals. You can click for reference to be generated and you will get the desired result.

Chicago Referencing Generator

Do you require the citation style? You can rely on Chicago Referencing Generator and will ensure that you will get the right citation in just a click. The generator is created to provide solutions to students to cite their research paper in Chicago style. It has proved to be the quickest and best citation generator on the market. The generator helps to sort the homework issues in few seconds. It also saves a lot of time and energy.

This tool helps researchers, professional writers, and students to manage and generate text citation essays. It will help to write Chicago style in a fast and easy way. It also provides details and directions for formatting and cites resources.

So, you must stop wasting the time and can go for Chicago Referencing Generator or APA referencing generator. These citation generators will help to solve the problem of citation issues. You can easily create citations by using endnotes and footnotes.

So, you can generate bibliographies, references, in-text citations, and title pages. These are fully automatic referencing style. You are just required to enter certain details about the citation and you will get the citation in the proper and required format.

So, if you are feeling any problem in doing assignment then you can take the help of assignment help.
If you require help for Assignment then livewebtutors is the right place for you. If you see our prices, you will observe that they are actually very affordable. Also, you can always expect a discount. Our team is capable and versatile enough to offer you exactly what you need, the best services for the prices you can afford.

read more:- Are you struggling to write a bibliography? Use Harvard referencing generator

#apa referencing generator #harvard referencing generator #chicago referencing generator #mla referencing generator #deakin referencing generator #oxford referencing generator

Royce  Reinger

Royce Reinger

1659126300

Generate Strings That Match A Given Regular Expression

regexp-examples 

Extends the Regexp class with the methods: Regexp#examples and Regexp#random_example

Regexp#examples generates a list of all* strings that will match the given regular expression.

Regexp#random_example returns one, random string (from all possible strings!!) that matches the regex.

* If the regex has an infinite number of possible strings that match it, such as /a*b+c{2,}/, or a huge number of possible matches, such as /.\w/, then only a subset of these will be listed. For more detail on this, see configuration options.

If you'd like to understand how/why this gem works, please check out my blog post about it.

Usage

Regexp#examples

/a*/.examples #=> ['', 'a', 'aa']
/ab+/.examples #=> ['ab', 'abb', 'abbb']
/this|is|awesome/.examples #=> ['this', 'is', 'awesome']
/https?:\/\/(www\.)?github\.com/.examples #=> ['http://github.com',
  # 'http://www.github.com', 'https://github.com', 'https://www.github.com']
/(I(N(C(E(P(T(I(O(N)))))))))*/.examples #=> ["", "INCEPTION", "INCEPTIONINCEPTION"]
/\x74\x68\x69\x73/.examples #=> ["this"]
/what about (backreferences\?) \1/.examples
  #=> ['what about backreferences? backreferences?']
/
  \u{28}\u2022\u{5f}\u2022\u{29}
  |
  \u{28}\u{20}\u2022\u{5f}\u2022\u{29}\u{3e}\u2310\u25a0\u{2d}\u25a0\u{20}
  |
  \u{28}\u2310\u25a0\u{5f}\u25a0\u{29}
/x.examples #=> ["(•_•)", "( •_•)>⌐■-■ ", "(⌐■_■)"]

Regexp#random_example

Obviously, you will get different (random) results if you try these yourself!

/\w{10}@(hotmail|gmail)\.com/.random_example #=> "TTsJsiwzKS@gmail.com"
/5[1-5][0-9]{14}/.random_example #=> "5224028604559821" (A valid MasterCard number)
/\p{Greek}{80}/.random_example
  #=> "ΖΆΧͷᵦμͷηϒϰΟᵝΔ΄θϔζΌψΨεκᴪΓΕπι϶ονϵΓϹᵦΟπᵡήϴϜΦϚϴϑ͵ϴΉϺ͵ϹϰϡᵠϝΤΏΨϹϊϻαώΞΰϰΑͼΈΘͽϙͽξΆΆΡΡΉΓς"
/written by tom lord/i.random_example #=> "WrITtEN bY tOM LORD"

Supported ruby versions

MRI 2.4.0 (oldest non-EOL version) --> 3.0.0 (latest stable version)

MRI 2.0.0 --> 2.3.x were supported until version 1.5.0 of this library. Support was dropped primarily because of the need to use RbConfig::CONFIG['UNICODE_VERSION'], which was added to ruby version 2.4.0.

MRI versions ≤ 1.9.3 were never supported by this library. This is primarily because MRI 2.0.0 introduced a new regexp engine (Oniguruma was replaced by Onigmo -- For example, named properties like /\p{Alpha}/ are illegal syntax on MRI 1.9.3.). Whilst most of this gem could be made to work with MRI 1.9.x (or even 1.8.x), I considered the changes too significant to implement backwards compatability (especially since long-term support for MRI 1.9.3 has long ended).

Other implementations, such as JRuby, could probably work fine - but I haven't fully tried/tested it. Pull requests are welcome.

Installation

Add this line to your application's Gemfile:

gem 'regexp-examples'

And then execute:

$ bundle

Or install it yourself as:

$ gem install regexp-examples

Supported syntax

Short answer: Everything is supported, apart from "irregular" aspects of the regexp language -- see impossible features.

Long answer:

All forms of repeaters (quantifiers), e.g. /a*/, /a+/, /a?/, /a{1,4}/, /a{3,}/, /a{,2}/

  • Reluctant and possissive repeaters work fine, too, e.g. /a*?/, /a*+/

Boolean "Or" groups, e.g. /a|b|c/

Character sets, e.g. /[abc]/ - including:

  • Ranges, e.g./[A-Z0-9]/
  • Negation, e.g. /[^a-z]/
  • Escaped characters, e.g. /[\w\s\b]/
  • POSIX bracket expressions, e.g. /[[:alnum:]]/, /[[:^space:]]/
    • ...Taking the current ruby version into account - e.g. the definition of /[[:punct:]]/ changed in version 2.4.0.
  • Set intersection, e.g. /[[a-h]&&[f-z]]/

Escaped characters, e.g. /\n/, /\w/, /\D/ (and so on...)

Capture groups, e.g. /(group)/

  • Including named groups, e.g. /(?<name>group)/
  • And backreferences(!!!), e.g. /(this|that) \1/ /(?<name>foo) \k<name>/
  • ...even for the more "obscure" syntax, e.g. /(?<future>the) \k'future'/, /(a)(b) \k<-1>/
  • ...and even if nested or optional, e.g. /(even(this(works?))) \1 \2 \3/, /what about (this)? \1/
  • Non-capture groups, e.g. /(?:foo)/
  • Comment groups, e.g. /foo(?#comment)bar/
  • Absent operator groups, e.g. /(?~exp)/ This feature is available in ruby version >= 2.4.1. However, support in this gem is limited.

Control characters, e.g. /\ca/, /\cZ/, /\C-9/

Escape sequences, e.g. /\x42/, /\x5word/, /#{"\x80".force_encoding("ASCII-8BIT")}/

Unicode characters, e.g. /\u0123/, /\uabcd/, /\u{789}/

Octal characters, e.g. /\10/, /\177/

Named properties, e.g. /\p{L}/ ("Letter"), /\p{Arabic}/ ("Arabic character") , /\p{^Ll}/ ("Not a lowercase letter"), /\P{^Canadian_Aboriginal}/ ("Not not a Canadian aboriginal character")

  • ...Even between different ruby versions!! (e.g. /\p{Arabic}/.examples(max_group_results: 999) will give you a different answer in ruby v2.1.x and v2.2.x)

Arbitrarily complex combinations of all the above!

Regexp options can also be used:

  • Case insensitive examples: /cool/i.examples #=> ["cool", "cooL", "coOl", "coOL", ...]
  • Multiline examples: /./m.examples #=> ["\n", "a", "b", "c", "d"]
  • Extended form examples: /line1 #comment \n line2/x.examples #=> ["line1line2"]
  • Options toggling supported: /before(?imx-imx)after/, /before(?imx-imx:subexpr)after/

Configuration Options

When generating examples, the gem uses 3 configurable values to limit how many examples are listed:

max_repeater_variance (default = 2) restricts how many examples to return for each repeater. For example:

  • .* is equivalent to .{0,2}
  • .+ is equivalent to .{1,3}
  • .{2,} is equivalent to .{2,4}
  • .{,3} is equivalent to .{0,2}
  • .{3,8} is equivalent to .{3,5}

max_group_results (default = 5) restricts how many characters to return for each "set". For example:

  • \d is equivalent to [01234]
  • \w is equivalent to [abcde]
  • [h-s] is equivalent to [hijkl]
  • (1|2|3|4|5|6|7|8) is equivalent to [12345]

max_results_limit (default = 10000) restricts the maximum number of results that can possibly be generated. For example:

  • /c+r+a+z+y+ * B+I+G+ * r+e+g+e+x+/i.examples.length <= 10000 -- Attempting this will NOT freeze your system, even though (by the above rules) this "should" attempt to generate 117546246144 examples.

Rexexp#examples makes use of all these options; Rexexp#random_example only uses max_repeater_variance, since the other options are redundant.

Defining custom configuration values

To use an alternative value, you can either pass the configuration option as a parameter:

/a*/.examples(max_repeater_variance: 5)
  #=> [''. 'a', 'aa', 'aaa', 'aaaa' 'aaaaa']
/[F-X]/.examples(max_group_results: 10)
  #=> ['F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O']
/[ab]{10}/.examples(max_results_limit: 64).length == 64 # NOT 1024
/[slow]{9}/.examples(max_results_limit: 9999999).length == 4 ** 9 == 262144 # Warning - this will take a while!
/.*/.random_example(max_repeater_variance: 50)
  #=> "A very unlikely result!"

Or, set an alternative value within a block:

RegexpExamples::Config.with_configuration(max_repeater_variance: 5) do
  # ...
end

Or, globally set a different default value:

# e.g In a rails project, you may wish to place this in
# config/initializers/regexp_examples.rb
RegexpExamples::Config.max_repeater_variance = 5
RegexpExamples::Config.max_group_results = 10
RegexpExamples::Config.max_results_limit = 20000

A sensible use case might be, for example, to generate all 1-5 digit strings:

/\d{1,5}/.examples(max_repeater_variance: 4, max_group_results: 10, max_results_limit: 100000)
  #=> ['0', '1', '2', ..., '99998', '99999']

Configuration Notes

Due to code optimisation, Regexp#random_example runs pretty fast even on very complex patterns. (I.e. It's typically a lot faster than using /pattern/.examples.sample(1).) For instance, the following takes no more than ~ 1 second on my machine:

/.*\w+\d{100}/.random_example(max_repeater_variance: 1000)

All forms of configuration mentioned above are thread safe.

Bugs and TODOs

There are no known major bugs with this library. However, there are a few obscure issues that you may encounter.

All known bugs/missing features are documented in GitHub. Please discuss known issues there, or raise a new issue if required. Pull requests are welcome!

Some of the most obscure regexp features are not even mentioned in the ruby docs. However, full documentation on all the intricate obscurities in the ruby (version 2.x) regexp parser can be found here.

Impossible features ("illegal syntax")

The following features in the regex language can never be properly implemented into this gem because, put simply, they are not technically "regular"! If you'd like to understand this in more detail, check out what I had to say in my blog post about this gem.

Using any of the following will raise a RegexpExamples::IllegalSyntax exception:

  • Lookarounds, e.g. /foo(?=bar)/, /foo(?!bar)/, /(?<=foo)bar/, /(?<!foo)bar/
  • Anchors (\b, \B, \G, ^, \A, $, \z, \Z), e.g. /\bword\b/, /line1\n^line2/
    • Anchors are really just special cases of lookarounds!
    • However, a special case has been made to allow ^, \A and \G at the start of a pattern; and to allow $, \z and \Z at the end of pattern. In such cases, the characters are effectively just ignored.
  • Subexpression calls (\g), e.g. /(?<name> ... \g<name>* )/

(Note: Backreferences are not really "regular" either, but I got these to work with a bit of hackery.)

Contributing

  1. Fork it ( https://github.com/tom-lord/regexp-examples/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
  6. Don't forget to add tests!!

Author: tom-lord
Source Code: https://github.com/tom-lord/regexp-examples 
License: MIT license

#ruby #data #random #string 

Royce  Reinger

Royce Reinger

1658977500

A Ruby Library for Generating Text with Recursive Template Grammars

Calyx

Calyx provides a simple API for generating text with declarative recursive grammars.

Install

Command Line

gem install calyx

Gemfile

gem 'calyx'

Examples

The best way to get started quickly is to install the gem and run the examples locally.

Any Gradient

Requires Roda and Rack to be available.

gem install roda

Demonstrates how to use Calyx to construct SVG graphics. Any Gradient generates a rectangle with a linear gradient of random colours.

Run as a web server and preview the output in a browser (http://localhost:9292):

ruby examples/any_gradient.rb

Or generate SVG files via a command line pipe:

ruby examples/any_gradient > gradient1.xml

Tiny Woodland Bot

Requires the Twitter client gem and API access configured for a specific Twitter handle.

gem install twitter

Demonstrates how to use Calyx to make a minimal Twitter bot that periodically posts unique tweets. See @tiny_woodland on Twitter and the writeup here.

TWITTER_CONSUMER_KEY=XXX-XXX
TWITTER_CONSUMER_SECRET=XXX-XXX
TWITTER_ACCESS_TOKEN=XXX-XXX
TWITTER_CONSUMER_SECRET=XXX-XXX
ruby examples/tiny_woodland_bot.rb

Faker

Faker is a popular library for generating fake names and associated sample data like internet addresses, company names and locations.

This example demonstrates how to use Calyx to reproduce the same functionality using custom lists defined in a YAML configuration file.

ruby examples/faker.rb

Usage

Require the library and inherit from Calyx::Grammar to construct a set of rules to generate a text.

require 'calyx'

class HelloWorld < Calyx::Grammar
  start 'Hello world.'
end

To generate the text itself, initialize the object and call the generate method.

hello = HelloWorld.new
hello.generate
# > "Hello world."

Obviously, this hardcoded sentence isn’t very interesting by itself. Possible variations can be added to the text by adding additional rules which provide a named set of text strings. The rule delimiter syntax ({}) can be used to substitute the generated content of other rules.

class HelloWorld < Calyx::Grammar
  start '{greeting} world.'
  greeting 'Hello', 'Hi', 'Hey', 'Yo'
end

Each time #generate runs, it evaluates the tree and randomly selects variations of rules to construct a resulting string.

hello = HelloWorld.new

hello.generate
# > "Hi world."

hello.generate
# > "Hello world."

hello.generate
# > "Yo world."

By convention, the start rule specifies the default starting point for generating the final text. You can start from any other named rule by passing it explicitly to the generate method.

class HelloWorld < Calyx::Grammar
  hello 'Hello world.'
end

hello = HelloWorld.new
hello.generate(:hello)

Block Constructors

As an alternative to subclassing, you can also construct rules unique to an instance by passing a block when initializing the class:

hello = Calyx::Grammar.new do
  start '{greeting} world.'
  greeting 'Hello', 'Hi', 'Hey', 'Yo'
end

hello.generate

Template Expressions

Basic rule substitution uses single curly brackets as delimiters for template expressions:

fruit = Calyx::Grammar.new do
  start '{colour} {fruit}'
  colour 'red', 'green', 'yellow'
  fruit 'apple', 'pear', 'tomato'
end

6.times { fruit.generate }
# => "yellow pear"
# => "red apple"
# => "green tomato"
# => "red pear"
# => "yellow tomato"
# => "green apple"

Nesting and Substitution

Rules are recursive. They can be arbitrarily nested and connected to generate larger and more complex texts.

class HelloWorld < Calyx::Grammar
  start '{greeting} {world_phrase}.'
  greeting 'Hello', 'Hi', 'Hey', 'Yo'
  world_phrase '{happy_adj} world', '{sad_adj} world', 'world'
  happy_adj 'wonderful', 'amazing', 'bright', 'beautiful'
  sad_adj 'cruel', 'miserable'
end

Nesting and hierarchy can be manipulated to balance consistency with novelty. The exact same word atoms can be combined in a variety of ways to produce strikingly different resulting texts.

module HelloWorld
  class Sentiment < Calyx::Grammar
    start '{happy_phrase}', '{sad_phrase}'
    happy_phrase '{happy_greeting} {happy_adj} world.'
    happy_greeting 'Hello', 'Hi', 'Hey', 'Yo'
    happy_adj 'wonderful', 'amazing', 'bright', 'beautiful'
    sad_phrase '{sad_greeting} {sad_adj} world.'
    sad_greeting 'Goodbye', 'So long', 'Farewell'
    sad_adj 'cruel', 'miserable'
  end

  class Mixed < Calyx::Grammar
    start '{greeting} {adj} world.'
    greeting 'Hello', 'Hi', 'Hey', 'Yo', 'Goodbye', 'So long', 'Farewell'
    adj 'wonderful', 'amazing', 'bright', 'beautiful', 'cruel', 'miserable'
  end
end

Random Sampling

By default, the outcomes of generated rules are selected with Ruby’s built-in pseudorandom number generator (as seen in methods like Kernel.rand and Array.sample). To seed the random number generator, pass in an integer seed value as the first argument to the constructor:

grammar = Calyx::Grammar.new(seed: 12345) do
  # rules...
end

Alternatively, you can pass a preconfigured instance of Ruby’s stdlib Random class:

random = Random.new(12345)

grammar = Calyx::Grammar.new(rng: random) do
  # rules...
end

When a random seed isn’t supplied, Time.new.to_i is used as the default seed, which makes each run of the generator relatively unique.

Weighted Choices

Choices can be weighted so that some rules have a greater probability of expanding than others.

Weights are defined by passing a hash instead of a list of rules where the keys are strings or symbols representing the grammar rules and the values are weights.

Weights can be represented as floats, integers or ranges.

  • Floats must be in the interval 0..1 and the given weights for a production must sum to 1.
  • Ranges must be contiguous and cover the entire interval from 1 to the maximum value of the largest range.
  • Integers (Fixnums) will produce a distribution based on the sum of all given numbers, with each number being a fraction of that sum.

The following definitions produce an equivalent weighting of choices:

Calyx::Grammar.new do
  start 'heads' => 1, 'tails' => 1
end

Calyx::Grammar.new do
  start 'heads' => 0.5, 'tails' => 0.5
end

Calyx::Grammar.new do
  start 'heads' => 1..5, 'tails' => 6..10
end

Calyx::Grammar.new do
  start 'heads' => 50, 'tails' => 50
end

There’s a lot of interesting things you can do with this. For example, you can model the triangular distribution produced by rolling 2d6:

Calyx::Grammar.new do
  start(
    '2' => 1,
    '3' => 2,
    '4' => 3,
    '5' => 4,
    '6' => 5,
    '7' => 6,
    '8' => 5,
    '9' => 4,
    '10' => 3,
    '11' => 2,
    '12' => 1
  )
end

Or reproduce Gary Gygax’s famous generation table from the original Dungeon Master’s Guide (page 171):

Calyx::Grammar.new do
  start(
    :empty => 0.6,
    :monster => 0.1,
    :monster_treasure => 0.15,
    :special => 0.05,
    :trick_trap => 0.05,
    :treasure => 0.05
  )
  empty 'Empty'
  monster 'Monster Only'
  monster_treasure 'Monster and Treasure'
  special 'Special'
  trick_trap 'Trick/Trap.'
  treasure 'Treasure'
end

String Modifiers

Dot-notation is supported in template expressions, allowing you to call any available method on the String object returned from a rule. Formatting methods can be chained arbitrarily and will execute in the same way as they would in native Ruby code.

greeting = Calyx::Grammar.new do
  start '{hello.capitalize} there.', 'Why, {hello} there.'
  hello 'hello', 'hi'
end

4.times { greeting.generate }
# => "Hello there."
# => "Hi there."
# => "Why, hello there."
# => "Why, hi there."

You can also extend the grammar with custom modifiers that provide useful formatting functions.

Filters

Filters accept an input string and return the transformed output:

greeting = Calyx::Grammar.new do
  filter :shoutycaps do |input|
    input.upcase
  end

  start '{hello.shoutycaps} there.', 'Why, {hello.shoutycaps} there.'
  hello 'hello', 'hi'
end

4.times { greeting.generate }
# => "HELLO there."
# => "HI there."
# => "Why, HELLO there."
# => "Why, HI there."

Mappings

The mapping shortcut allows you to specify a map of regex patterns pointing to their resulting substitution strings:

green_bottle = Calyx::Grammar.new do
  mapping :pluralize, /(.+)/ => '\\1s'
  start 'One green {bottle}.', 'Two green {bottle.pluralize}.'
  bottle 'bottle'
end

2.times { green_bottle.generate }
# => "One green bottle."
# => "Two green bottles."

Modifier Mixins

In order to use more intricate rewriting and formatting methods in a modifier chain, you can add methods to a module and embed it in a grammar using the modifier classmethod.

Modifier methods accept a single argument representing the input string from the previous step in the expression chain and must return a string, representing the modified output.

module FullStop
  def full_stop(input)
    input << '.'
  end
end

hello = Calyx::Grammar.new do
  modifier FullStop
  start '{hello.capitalize.full_stop}'
  hello 'hello'
end

hello.generate
# => "Hello."

To share custom modifiers across multiple grammars, you can include the module in Calyx::Modifiers. This will make the methods available to all subsequent instances:

module FullStop
  def full_stop(input)
    input << '.'
  end
end

class Calyx::Modifiers
  include FullStop
end

Monkeypatching String

Alternatively, you can combine methods from existing Gems that monkeypatch String:

require 'indefinite_article'

module FullStop
  def full_stop
    self << '.'
  end
end

class String
  include FullStop
end

noun_articles = Calyx::Grammar.new do
  start '{fruit.with_indefinite_article.capitalize.full_stop}'
  fruit 'apple', 'orange', 'banana', 'pear'
end

4.times { noun_articles.generate }
# => "An apple."
# => "An orange."
# => "A banana."
# => "A pear."

Memoized Rules

Rule expansions can be ‘memoized’ so that multiple references to the same rule return the same value. This is useful for picking a noun from a list and reusing it in multiple places within a text.

The @ sigil is used to mark memoized rules. This evaluates the rule and stores it in memory the first time it’s referenced. All subsequent references to the memoized rule use the same stored value.

# Without memoization
grammar = Calyx::Grammar.new do
  start '{name} <{name.downcase}>'
  name 'Daenerys', 'Tyrion', 'Jon'
end

3.times { grammar.generate }
# => Daenerys <jon>
# => Tyrion <daenerys>
# => Jon <tyrion>

# With memoization
grammar = Calyx::Grammar.new do
  start '{@name} <{@name.downcase}>'
  name 'Daenerys', 'Tyrion', 'Jon'
end

3.times { grammar.generate }
# => Tyrion <tyrion>
# => Daenerys <daenerys>
# => Jon <jon>

Note that the memoization symbol can only be used on the right hand side of a production rule.

Unique Rules

Rule expansions can be marked as ‘unique’, meaning that multiple references to the same rule always return a different value. This is useful for situations where the same result appearing twice would appear awkward and messy.

Unique rules are marked by the $ sigil.

grammar = Calyx::Grammar.new do
  start "{$medal}, {$medal}, {$medal}"
  medal 'Gold', 'Silver', 'Bronze'
end

grammar.generate
# => Silver, Bronze, Gold

Dynamically Constructing Rules

Template expansions can be dynamically constructed at runtime by passing a context map of rules to the #generate method:

class AppGreeting < Calyx::Grammar
  start 'Hi {username}!', 'Welcome back {username}...', 'Hola {username}'
end

context = {
  username: UserModel.username
}

greeting = AppGreeting.new
greeting.generate(context)

External File Formats

In addition to defining grammars in pure Ruby, you can load them from external JSON and YAML files:

hello = Calyx::Grammar.load('hello.yml')
hello.generate

The format requires a flat map with keys representing the left-hand side named symbols and the values representing the right hand side substitution rules.

In JSON:

{
  "start": "{greeting} world.",
  "greeting": ["Hello", "Hi", "Hey", "Yo"]
}

In YAML:

---
start: "{greeting} world."
greeting:
  - Hello
  - Hi
  - Hey
  - Yo

Accessing the Raw Generated Tree

Calling #evaluate on the grammar instance will give you access to the raw generated tree structure before it gets flattened into a string.

The tree is encoded as an array of nested arrays, with the leading symbols labeling the choices and rules selected, and the trailing terminal leaves encoding string values.

This may not make a lot of sense unless you’re familiar with the concept of s-expressions. It’s a fairly speculative feature at this stage, but it leads to some interesting possibilities.

grammar = Calyx::Grammar.new do
  start 'Riddle me ree.'
end

grammar.evaluate
# => [:start, [:choice, [:concat, [[:atom, "Riddle me ree."]]]]]

Roadmap

Rough plan for stabilising the API and features for a 1.0 release.

VersionFeatures planned
0.6block constructor
0.7support for template context map passed to generate
0.8method missing metaclass API
0.9return grammar tree from #evaluate, with flattened string from #generate being separate
0.10inject custom string functions for parameterised rules, transforms and mappings
0.11support YAML format (and JSON?)
0.12API documentation
0.13Support for unique rules
0.14Support for Ruby 2.4
0.15Options config and ‘strict mode’ error handling
0.16Improve representation of weighted probability selection
0.17Return result object from #generate calls

Credits

Author & Maintainer

Contributors

Author: Maetl
Source Code: https://github.com/maetl/calyx 
License: MIT license

#ruby #text