Author image thrig
and 1 contributors


Lingua::Awkwords - randomly generates outputs from a given pattern


  use feature qw(say);
  use Lingua::Awkwords;
  use Lingua::Awkwords::Subpattern;

  # V is a pre-defined subpattern, ^ filters out aa from the list
  # of two vowels that the two VV generate
  my $la = Lingua::Awkwords->new( pattern => q{ [VV]^aa } );

  say $la->render for 1..10;

  # define our own C, V
      C => [qw/j k l m n p s t w/],
      V => [qw/a e i o u/],
  # and a pattern somewhat suitable for Toki Pona...

  say $la->render for 1..10;


This is a Perl implementation of

though is not an exact replica of that parser;

details the format that this code is based on. Briefly,


[] or ()

Denote a unit or group; they are identical except that (a) is equivalent to [a/]--that is, it represents the possibility of generating the empty string in addition to any other terms supplied.

Units can be nested recursively. There is an implicit unit at the top level of the pattern.


Introduces a choice within a unit; without this [Vx] would generate whatever V represents (a list of vowels by default) followed by the letter x while [V/x] by contrast generates only a vowel or the letter x.


The asterisk followed by an integer in the range 1..INT_MAX weights the current term of the alternation, if any. That is, while [a/] generates each term with equal probability, [a/*2] would generate the empty string at twice the probability of the letter a.


The caret introduces a filter that must follow a unit (there is an implicit unit at the top level of a pattern). An example would be [VV]^aa or the equivalent VV^aa that (by default) generates two vowels, but replaces aa with the empty string. More than one filter may be specified.


Capital ASCII letters denote subpatterns; several of these are set by default. See Lingua::Awkwords::Subpattern for how to customize them. V for example is by default equivalent to the more verbose [a/i/u].


Use double quotes to denote a quoted string; this prevents other characters (besides " itself) from being interpreted as some non- string value.


Anything else not otherwise accounted for above is treated as part of a string, so ["abc"/abc] generates either the string abc or the string abc, as this is two ways of saying the same thing.



Awkword pattern. Without this supplied any call to render will throw an exception.


Where the parse tree is stored.


These can be called as Lingua::Awkwords::set_filter or can be imported via

  use Lingua::Awkwords qw(weights2str weights_from);
percentize hashref

Modifies the values of the given hashref to be percentages of the sum of the values. Will croak if sum is 0. Use this to help compare weights_from different corpus.

set_filter filter-value

Utility routine for use with walk. Returns a subroutine that sets the filter_with attribute to the given value.

  $la->walk( Lingua::Awkwords::set_filter('X') );
weights2str hash-reference

Constructs an awkwords choice string from a given hash-reference of values and weights, e.g.

  use Lingua::Awkwords qw(weights2str weights_from);

  weights2str( ( weights_from("toki sin li toki pona") )[-1] )

will return a weight string of


that can then be used as a pattern for this module.

weights_from string-or-filehandle

Parses the frequency of characters appearing in the input string or filehandle, and returns four hash references, first, mid, last and all which contain the character counts of the first letters of the "words" in the input, characters that appear in the middle, end, and a tally of all three of these positions together.

"words" is used in scare quotes because there is "no generally accepted and completely satisfactory definition of what constitutes a word" (Philip Durkin. "The Oxford Guide to Etymology". p.37) and because instead syllables could be fed in and then patterns generated using those syllable-specific weights.



Constructor. Typically this should be passed a pattern argument.

parse_string pattern

Returns the parse tree of the given pattern without setting the tree attribute. "COMPLICATIONS" shows one use for this.


Returns a string render of the awkword pattern. This may be the empty string if filters have removed all the text.

walk callback

Provides a means to recurse through the parse tree, where every object in the tree will call the callback with $self as the sole argument, and then if necessary iterate through all of the possibilities contained by itself calling walk on each of those.


More complicated structures can be built by attaching parse trees to subpatterns. For example, Toki Pona could be extended to allow optional diphthongs (mostly in the second syllable) via

  use feature qw(say);
  use Lingua::Awkwords::Subpattern;
  use Lingua::Awkwords;
  my $cv  = Lingua::Awkwords->parse_string(q{
  my $cvv = Lingua::Awkwords->parse_string(q{

      A => $cv,
      B => $cvv,
      C => [qw/j k l m n p s t w/],
      V => [qw/a e i o u/],

  my $tree = Lingua::Awkwords->new( pattern => q{
      [ a[B/BA/BAA/A/AA/AAA] / [AB/ABA/ABAA/A/AA/AAA] ] [n/*5]

  say join ' ', map { $tree->render } 1 .. 10;

The default filter of the empty string can be problematical, as one may not know whether a filter has been applied to the result, or the word may be filtered into an incorrect form. Consult the eg/ directory of this module's distribution for example code that customizes the filter value.

Code that makes use of non-ASCII encodings may need appropriate settings made, e.g. to use the locale for input and output and to allow UTF-8 in the program text.

  use open IO  => ':locale';
  use utf8;

      S => [qw/... UTF-8 data here .../],


Reporting Bugs

Please report any bugs or feature requests to bug-lingua-awkwords at, or through the web interface at

Patches might best be applied towards:

Known Issues

There are various incompatibilities with the original version of the code; these are detailed in the parser module as they concern how e.g. weights are parsed.

See also the "Known Issues" section in all the other modules in this distribution.


Lingua::Awkwords::ListOf, Lingua::Awkwords::OneOf, Lingua::Awkwords::Parser, Lingua::Awkwords::String, Lingua::Awkwords::Subpattern


thrig - Jeremy Mates (cpan:JMATES) <jmates at>


Copyright (C) 2017 by Jeremy Mates

This program is distributed under the (Revised) BSD License: