in reply to Perl can do it, take 1 (sentence generation)
my %dict = ( SENTENCE => ["NP VP"], NP => ["ART NOUN"], VP => ["VERB NP"], ART => [qw/ the a /], NOUN => [qw/ man ball woman table /], VERB => [qw/ hit took saw liked /] ); sub rand_production { my $items = $dict{+shift}; return $items->[rand @$items]; } sub generate { local $_ = shift; my $nonterminal = join "|", map quotemeta, keys %dict; 1 while s/($nonterminal)/ rand_production($1) /e; return $_; } print generate("SENTENCE"), $/;
blokhead
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Perl can do it, take 1 (sentence generation)
by spurperl (Priest) on Jun 17, 2005 at 15:23 UTC | |
by blokhead (Monsignor) on Jun 17, 2005 at 15:45 UTC | |
by spurperl (Priest) on Jun 18, 2005 at 05:09 UTC | |
by ikegami (Patriarch) on Jun 17, 2005 at 15:35 UTC | |
by BUU (Prior) on Jun 17, 2005 at 17:28 UTC |