

1.  There can no string q, since strings of lenght 1 start <B>andB> end with their only character. Since ’q’ is not in end, the string q is invalid (no matter wether ’q’ appears in start or not). 
2.  No string longer than 1 could start with ’q’ or have a ’q’ in the middle, since ’q’ is not followed by anything. This leaves only strings with length 1 and these are invalid according to rule 1. 
From now on, a ’class’ refers to all strings with the same length. The order or length of a class is the length of all strings in it.With a simple charset, each class has exactly M times more strings than the previous class (e.g. the class with a length  1). M is in this case the length of the charset.
See Math::String::Charset.
For charsets of higher order, even determining the number of all strings in a class becomes more difficult. Fortunately, there is a way to do it in N steps just like with a simple charset.
The first way is based on the observation that the number of strings in class n+1 only depends on the number of ending chars in class n, and nothing else.This is, however, not used in the current implemenation, since there is a slightly faster/simpler way based on the count of strings that start with a given character in class n, n1, n2 etc. See below for a description.
Here is for reference the example with ending char counts:
use Math::String::Charset; $cs = Math::String::Charset>new( { start => [ a, c, b, d ], bi => { a => [ b,c,a ], c => [ c,d ], b => [ a, ], d => [ a, ], } } ); Class 1: a 1 c 2 b 3 d 4 4As you can see, there is one ’a’, one ’c’, one ’b’ and one ’d’. To determine how many strings are in class 2, we must multiply the occurances of each character by the number of how many characters it is followed:
a * 3 + c * 2 + d * 1 + b * 1which equals
1 * 3 + 1 * 2 + 1 * 1 + 1 * 1If we summ this all up, we get 3+2+1+1 = 7, which is exactly the number of strings in class 2. But to determine now the number of strings in class 3, we must now how many strings in class 2 end on ’a’, how many on ’b’ etc.
We can do this in the same loop, by not only keeping a sum, but by counting all the different endings. F.i. exactly one string ended in ’a’ in class 1. Since ’a’ can be followed by 3 characters, for each character we know that it will occure at least 1 time. So we add the 1 to the character in question.
$new_count>{b} += $count>{a};This yields the amounts of strings that end in ’b’ in the next class.
We have to do this for every different starting character, and for each of the characters that follows each starting character. In the worst case this means M*M steps, while M is the length of the charset. We must repeat this for each of the classes, so that the complexity becomes O(N*M*M) in the worst case. For strings of higher order this gets worse, adding a *M for each higher order.
For our example, after processing ’a’, we will have the following counts for ending chars in class 2:
b => 1 c => 1 a => 1After processing ’c’, it is:
b => 1 c => 2 (+1) a => 1 d => 1 (+1)because ’c’ is followed by ’d’ or ’c’. When we are done with all characters, the following count’s are in our $new_count hash:
b => 1 c => 2 a => 3 d => 1When we sum them up, we get the count of strings in class 2. For class 3, we start with an empty count hash again, and then again for each character process the ones that follow it. Example for a:
b => 0 c => 0 a => 0 d => 03 times ending in ’a’ followed by ’b’,’c’ or ’d’:
b => 3 (+3) c => 3 (+3) a => 3 (+3) d => 02 times ending ’c’ followed by ’c’ or ’d’:
b => 3 c => 5 (+2) a => 3 d => 2 (+2)After processing ’b’ and ’d’ in a similiar manner we get:
b => 3 c => 5 a => 5 d => 2The sum is 15, and we know now that we have 15 different strings in class 3. The process for higher classes is the same again, reusing the counts from the lower class.
The second, and implemented method counts for each class how many strings start with a given character. This gives us two information at once:This method also has the advantage that it doesn’t need to recalculate the count for each level. If we have cached the information for class 7, we can calculate class 8 rightaway. The old method would either need to start at class 1, working up to 8 again, or cache additional information of the order N (where N is the number of different characters in the charset).
o A string of length N and a starting char of X, which number it must have at minimum (by summing up the counts of all strings that come before X) and how many strings are there starting with X (although this is not used for X, but only for all strings that come after X). o How many strings are there with a given length, by summing up all the counts for the different starting chars. Here is how the second method works, based on the example above:
start => [ a, c, b, d ], bi => { a => [ b,c,a ], c => [ c,d ], b => [ a, ], d => [ a, ], }The sequence runs as follows:
String Strings starting with this character in this level a 1 c 1 b 1 d 1 ab ac aa 3 (1+1+1) cc cd 2 (1+1) ba 1 da 1 aba acc acd aab aac aaa 6 1 (b) + 2 (c) + 3 (a) ccc ccd cda 3 2 (c) + 1 (d) bab bac baa 3 dab dac daa 3 abab abac abaa accc etcAs you can see, for length one, there is exactly one string for each starting character.
For the next class, we can find out how many strings start with a given char, by adding together all the counts of strings in the previous class.
F.i. in class 3, there are 6 strings starting with ’a’. We find this out by adding together 1 (there is 1 string starting with ’b’ in class 2), 2 (there are two strings starting with ’c’ in class 2) and 3 (three strings starting with ’a’ in class 2).
As a special case we must throw away all strings in class 2 that have invalid ending characters. By doing this, we automatically have restricted <B>allB> strings to only valid ending characters. Therefore, class 1 and 2 are setup upon creating the charset object, the others are calculated ondemand and then cached.
Since we are calculating the strings in the order of the starting characters, we can sum up all strings up to this character.
String First string in that class a 0 c 1 b 2 d 3 ab 0 ac aa cc 3 cd ba 5 da 6 aba 0 acc acd aab aac aaa ccc 6 ccd cda bab 9 bac baa dab 12 dac daa abab 0 abac abaa accc etcWhen we add to the number of the last character (f.i. 12 in case of ’d’ in class 3) the amount of strings with that character (here 3), we end up with the number of all strings in that class.
Thus in the same loop we calculate:
That should be all we need to know to convert a string to it’s number.
how many stings start with a given character in this class what is the first number of a string starting with ’x’ in that class how many strings are in this class at all
From the section above we know that we can find out which number a string of a certain class has at minimum and at maximum. But what number has the string in that range, actually?Well, given the information it is easy. First, find out which minimum number a string has with the given starting character in the class. Add this to it’s base number. Then reduce the class by one, look at the next character and repeat this. In pseudo code:
$class = length ($string); $base = base_number>[$class]; foreach ($character) { $base += $sum>[$class]>{$character}; $class ; }So, after N simple steps (where N is the number of characters in the string), we have found the number of the string.
Section not fully done yet.
It helps to imagine the strings like a couple of trees (ASCII art is crude):
class: 1 2 3 etc number 1 a 5 +ab 12  +aba 6 +ac 13  +acc 14  +acd 7 +aa 15 +aab 16 +aac 17 +aaa 2 c 8 +cc 18  +ccc 19  +ccd 9 +cd 20 +cda 3 b 10 +ba 21 +bab 22 +bac 23 +baa 4 d 11 +da 24 +dab 25 +dac 26 +daaAs you can see, there is a (independend) tree for each of the starting characters, which in turn contains independed subtrees for each string in the next class etc. It is interesting to note that each string deeper in the tree starts with the same common starting string, aka ’d’, ’da’, ’dab’ etc.
With a simple charset, all these trees contain the same number of nodes. With higher order charsets, this is no longer true.
BInew()
new();Create a new Math::String::Charset::Grouped object.
The constructor takes a HASH reference. The charset will be of order 2 or greater and type 0.
The following keys can be used:
minlen Minimum string length, inf if not defined maxlen Maximum string length, +inf if not defined bi hash, table with bigrams start array ref to list of all valid (starting) characters end array ref to list of all valid ending characters sep separator character, none if undef (only for order 1)
sep sep is a seperator string seperating the characters from each other. This is used to make characters with different lengths possible. start start contains an array reference to all valid starting characters, e.g. no valid string can start with a character not listed here. bi bi contains a hash reference, each key of the hash points to an array, which in turn contains all the valid combinations of two letters. end start contains an array reference to all valid ending characters, e.g. no valid string can end with a character not listed here. Note that strings of length 1 start <B>andB> end with their only character, so the character must be listed in end and start to produce a string with one character. Also all characters that are not followed by any other character are added silently to the end set. minlen Optional minimum string length. Any string shorter than this will be invalid. Must be shorter than maxlen. If not given is set to inf. Note that the minlen might be adjusted to a greater number, if it is set to 1 or greater, but there are not valid strings with 2,3 etc. In this case the minlen will be set to the first nonempty class of the charset.
maxlen Optional maximum string length. Any string longer than this will be invalid. Must be longer than minlen. If not given is set to +inf. BIminlen()
$charset>minlen();BImaxlen()
$charset>maxlen();BIlength()
$charset>length();Return the number of items in the charset, for higher order charsets the number of valid 1character long strings. Shortcut for $charset>class(1).
BIcount()
Returns the count of all possible strings described by the charset as a positive BigInt. Returns ’inf’ if no maxlen is defined, because there should be no upper bound on how many strings are possible. (This might change if we can calculate an upper bound  not sure if this is possible with bigrams).If maxlen is defined, forces a calculation of all possible class() values and may therefore be very slow on the first call, it also caches possible lot’s of values.
BIclass()
$charset>class($order);Return the number of items in a class.
print $charset>class(5); # how many strings with length 5?BIchar()
$charset>char($nr);Returns the character number $nr from the set, or undef.
print $charset>char(0); # first char print $charset>char(1); # second char print $charset>char(1); # last oneBIlowest()
$charset>lowest($length);Return the number of the first string of length $length. This is equivalent to (but much faster):
$str = $charset>first($length); $number = $charset>str2num($str);BIhighest()
$charset>highest($length);Return the number of the last string of length $length. This is equivalent to (but much faster):
$str = $charset>first($length+1); $number = $charset>str2num($str); $number;BIorder()
$order = $charset>order();Return the order of the charset: 2 (bigrams), 3 etc for higher orders. See also type().
BItype()
$type = $charset>type();Return the type of the charset and is always 0 for nested charsets. See also order.
BIcharlen()
$character_length = $charset>charlen();Return the length of one character in the set. 1 or greater.
BIchars()
$chars = $charset>chars( $bigint );Returns the number of characters that the string would have, when you would convert $bigint (Math::BigInt or Math::String object) back to a string. This is much faster than doing
$chars = length ("$math_string");since it does not need to actually construct the string.
BIfirst()
$charset>first( $length );Return the first string with a length of $length, according to the charset. See lowest() for the corrospending number.
BIlast()
$charset>last( $length );Return the last string with a length of $length, according to the charset. See highest() for the corrospending number.
BIis_valid()
$charset>is_valid();Check wether a string conforms to the charset set or not.
BIerror()
$charset>error();Returns "" for no error or an error message that occured if construction of the charset failed. Set $Math::String::Charset::die_on_error to 0 to get the error message, otherwise the program will die.
BIstart()
$charset>start();In list context, returns a list of all characters in the start set, for simple charsets (e.g. no bi, trigrams etc) simple returns the charset. In scalar context returns the lenght of the start set.
Note that the returned end set can be differen from what you specified upon constructing the charset, because characters that are not followed by any other character will be excluded from the start set (they can’t possible start a string longer than one character).
Think of the start set as the set of all characters that can start a string with more than one character. The set for one character strings is called <B>onesB> and you can access if via ones().
BIend()
$charset>end();In list context, returns a list of all characters in the end set, aka all characters a string can end with. For simple charsets (e.g. no bi, trigrams etc) simple returns the charset. In scalar context returns the lenght of the end set.
Note that the returned end set can be differen from what you specified upon constructing the charset, because characters that are not followed by any other character will be included in the end set, too.
BIones()
$charset>ones();In list context, returns a list of all strings consisting of one character, for simple charsets (e.g. no bi, trigrams etc) simple returns the charset. In scalar context returns the lenght of the <B>onesB> set.
This list is the cross of <B>startB> and <B>endB> that is calculated after adding characters with no followers to <B>endB>, but before removing the characters with no followers from <B>startB>.
Think of a string of only one character as if it starts with and ends in this character at the same time. For instance, if you have the following definition:
cs = { start => [ a, b, c, q ], end => [ b, c, x ], bi => { q => [ ], a => [ b, c ] b => [ a ] } }The ’q’ is not followed by any other character, so it can only end strings. And since it is not in the <B>endB> set, it is first added to this set:
cs = { start => [ a, b, c, q ], end => [ b, c, x, q ], bi => { q => [ ], a => [ b, c ] b => [ a ] } }Now the cross of start and end is build. Since only ’b’, ’c’ and ’q’ appear in both end and start, ones consists of:
_ones => [ b, c, q ]The order of the chars in ones is the same ordering as in start.
After this, any character that is not followed by an other character is removed from start:
start => [ a, b, ],Thus a string with only one character can be ’b’, ’c’, or ’q’, and any string with more than one character must start with either ’a’ or ’b’.
BIprev()
$string = Math::String>new( ); $charset>prev($string);Give the charset and a string, calculates the previous string in the sequence. This is faster than decrementing the number of the string and converting the new number to a string. This routine is mainly used internally by Math::String and updates the cache of the given Math::String.
BInext()
$string = Math::String>new( ); $charset>next($string);Give the charset and a string, calculates the next string in the sequence. This is faster than incrementing the number of the string and converting the new number to a string. This routine is mainly used internally by Math::String and updates the cache of the given Math::String.
use Math::String::Charset; # construct a charset from bigram table, and an initial set (containing # valid startcharacters) # Note: After an a, either an b, c or a can follow, in this order # After an d only an a can follow # There is no q as start character, but q can follow d! # You need to define followers for q! $bi = new Math::String::Charset ( { start => a..d, bi => { a => [ b, ], b => [ c, b ], c => [ a, c ], d => [ a, q ], q => [ a, b ], } } ); print $bi>length(),"\n"; # 4 print scalar $bi>class(2),"\n"; # count of combos with 2 chars # will be 1+2+2+2+2 => 9 my @comb = $bi>class(3); print join ("\n", @comb);This will print:
4 7 abc abb bca bcc bbc bbb cab cca ccc dab dqa dqbAnother example using characters of different lengths to find all combinations of words in a list:
#!/usr/bin/perl w # test for Math::String and Math::String::Charset BEGIN { unshift @INC, ../lib; } use Math::String; use Math::String::Charset; use strict; my $count = shift  4000; my $words = {}; open FILE, wordlist.txt or die "Cant read wordlist.txt: $!\n"; while (<FILE>) { chomp; $words>{lc($_)} ++; # clean out doubles } close FILE; my $cs = new Math::String::Charset ( { sep => , words => $words, } ); my $string = Math::String>new(,$cs); print "# Generating first $count strings:\n"; for (my $i = 0; $i < $count; $i++) { print ++$string,"\n"; } print "# Done.\n";
o Currently only bigrams are supported. This should be generic and arbitrarily deeply nested. o str2num and num2str do not work fully yet.
None doscovered yet.
If you use this module in one of your projects, then please email me. I want to hear about how my code helps you ;)This module is (C) Copyright by Tels http://bloodgate.com 20002003.
perl v5.20.3  MATH::STRING::CHARSET::NESTED (3)  20041120 
Visit the GSP FreeBSD Man Page Interface.
Output converted with manServer 1.07.