Class SentenceGenerator<T>

java.lang.Object
io.jenetics.ext.grammar.SentenceGenerator<T>
All Implemented Interfaces:
Generator<T,List<Cfg.Terminal<T>>>

public final class SentenceGenerator<T> extends Object implements Generator<T,List<Cfg.Terminal<T>>>
Standard implementation of a sentence generator. The generator can generate sentences by expanding the grammar in a SentenceGenerator.Expansion.LEFT_MOST or SentenceGenerator.Expansion.LEFT_TO_RIGHT order.

The following code snippet shows how to create a random sentence from a given grammar:

final Cfg<String> cfg = Bnf.parse("""
    <expr> ::= ( <expr> <op> <expr> ) | <num> | <var> |  <fun> ( <arg>, <arg> )
    <fun>  ::= FUN1 | FUN2
    <arg>  ::= <expr> | <var> | <num>
    <op>   ::= + | - | * | /
    <var>  ::= x | y
    <num>  ::= 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9
    """
);

final var random = RandomGenerator.of("L64X256MixRandom");
final var generator = new SentenceGenerator<String>(
    SymbolIndex.of(random),
    1_000
);
final List<Terminal<String>> sentence = generator.generate(cfg);
final String string = sentence.stream()
    .map(Symbol::name)
    .collect(Collectors.joining());

System.out.println(string);
Some sample output:

 > ((x-FUN1(5,5))+8)
 > (FUN2(y,5)-FUN2(0,x))
 > x
 > FUN2(x,x)
 > 5
 > FUN2(y,FUN2((FUN1(5,FUN1(y,2))*9),y))
 > ((FUN1(x,5)*9)*(x/(y*FUN2(x,y))))
 > (9-(y*(x+x)))
 >
 
Since:
7.1
Version:
7.1
See Also:
  • Constructor Details

    • SentenceGenerator

      public SentenceGenerator(SymbolIndex index, SentenceGenerator.Expansion expansion, int limit)
      Create a new sentence generator from the given parameters.
      Parameters:
      index - the symbol index function used for generating the sentences
      expansion - the sentence generation strategy to use for generating the sentences
      limit - the maximal allowed sentence length. If the generated sentence exceeds this length, the generation is interrupted and an empty sentence (empty list) is returned.
    • SentenceGenerator

      public SentenceGenerator(SymbolIndex index, int limit)
      Create a new sentence generator from the given parameters.
      Parameters:
      index - the symbol index function used for generating the sentences
      limit - the maximal allowed sentence length. If the generated sentence exceeds this length, the generation is interrupted and an empty sentence (empty list) is returned.
  • Method Details

    • generate

      public List<Cfg.Terminal<T>> generate(Cfg<? extends T> cfg)
      Generates a new sentence from the given grammar, cfg.
      Specified by:
      generate in interface Generator<T,List<Cfg.Terminal<T>>>
      Parameters:
      cfg - the generating grammar
      Returns:
      a newly created terminal list (sentence), or an empty list if the length of the sentence exceeds the defined sentence limit
    • toString

      public static String toString(List<? extends Cfg.Symbol<?>> sentence)
      Converts a list of symbols to a string, by concatenating the names of the given symbols.
      Parameters:
      sentence - the symbol list to covert
      Returns:
      the converted sentences