This file probabilistically generates strings from a JSGF grammar. It takes advantage of weights assigned to alternatives (separated by pipes) by choosing to expand higher weighted alternatives with greater probability. For sets of alternatives without weights, each alternative is equally likely to be expanded. For optional groups, the elements in the group have a 50% chance of being expanded.
It requires two arguments: the path to the JSGF Grammar file, and the number of strings to generate. You can run this on the included grammar Ideas.gram:
python ProbabilisticGenerator.py Ideas.gram 20
This will generate 20 sentences based on the public rule(s) in Ideas.gram, using the weights if they are provided.
Combines sets of strings by taking the cross product of the sets and concatenating the elements in the resulting tuples
Parameters: | listOfSets – 2-D list of strings |
---|---|
Returns: | a list of strings |
Chooses either a random disjunct (for alternatives without weights) or a disjunct based on defined weights.
Finds the rule expansion for a nonterminal and returns its expansion.
Processes the optional element 50% of the time, skips it the other 50% of the time
Combines adjacent elements in a sequence.
Chooses an element of a list based on its weight
Parameters: | listOfTuples – a list of (element, weight) tuples, where the element can be a JSGF expression object, string, or list, and the weight is a float |
---|---|
Returns: | the first element of a chosen tuple |