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
Some sample output:
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);
> ((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:
-
Nested Class Summary
Modifier and TypeClassDescriptionstatic enum
Defines the expansion strategy used when generating the sentences. -
Constructor Summary
ConstructorDescriptionSentenceGenerator
(SymbolIndex index, int limit) Create a new sentence generator from the given parameters.SentenceGenerator
(SymbolIndex index, SentenceGenerator.Expansion expansion, int limit) Create a new sentence generator from the given parameters. -
Method Summary
Modifier and TypeMethodDescriptionGenerates a new sentence from the given grammar, cfg.static String
toString
(List<? extends Cfg.Symbol<?>> sentence) Converts a list of symbols to a string, by concatenating the names of the given symbols.
-
Constructor Details
-
SentenceGenerator
Create a new sentence generator from the given parameters.- Parameters:
index
- the symbol index function used for generating the sentencesexpansion
- the sentence generation strategy to use for generating the sentenceslimit
- 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
Create a new sentence generator from the given parameters.- Parameters:
index
- the symbol index function used for generating the sentenceslimit
- 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
Generates a new sentence from the given grammar, cfg. -
toString
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
-