Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

Re: Perl can do it, take 1 (sentence generation)

by eric256 (Parson)
on Jun 19, 2005 at 16:22 UTC ( #468146=note: print w/replies, xml ) Need Help??


in reply to Perl can do it, take 1 (sentence generation)

Here is my perl6 port of your code. I had to work around a thing with given/when...unless they fixed it and i didn't notice yet.

use v6; my %dict = ( sentence => [[qw/ noun_phrase verb_phrase /]], noun_phrase => [[qw/ Article Noun /]], verb_phrase => [[qw/ Verb noun_phrase /]], Article => [qw/ the a /], Noun => [qw/ man ball woman table/], Verb => [qw/ hit took saw liked /] ); sub generate ($phrase) { given $phrase { when .does(List) { return $phrase.map:{generate($_)} } if (defined %dict.{$_}) { return generate( %dict.{$_}.pick) } else { return [$phrase] } } } sub gen_sentence () { generate("sentence").join(" ").say }; gen_sentence for 1..5;

___________
Eric Hodges

Replies are listed 'Best First'.
Re^2: Perl can do it, take 1 (sentence generation)
by spurperl (Priest) on Jun 19, 2005 at 16:36 UTC
    Can you please explain what .does(List) means ? I understand intuitively that it asks whether $phrase is a list, but how does it work, exactly ?

      As far as I know that is all there is to tell. ;) The . by itself acts on the topic ($_) So that is the same as $_.does(List). My understanding is that does is checking the roles of the object it is called on. Evnetualy (when pugs gets more mature) we will be able to do. when List {} and it will DWIM, hopefully. BTW the given $phrase makes $phrase the current topic.


      ___________
      Eric Hodges
        OK, thanks - I figured most of it out after posting by reading the apocalypse / exegesis on objects.

        Frankly, the syntax .does(List) doesn't feel good to me, so I'm glad you're saying we'll eventually be able to just say when List. Is the latter legal Perl 6 and you didn't use it just because you wanted a working pugs-compilable code ?

Re^2: Perl can do it, take 1 (sentence generation)
by wolv (Monk) on Jun 23, 2005 at 20:02 UTC
    Here is my version of the generate sub. Won't probably run on Pugs right now.
    use v6; sub generate { when List { .map:{ generate $_ } } when %dict { generate %dict{$_}.pick } default { [ $phrase ] } }
    For some reason I'm assuming that $_ is aliased to the first argument of a sub when there is no signature specified. If that's wrong, please tell me!

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://468146]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (6)
As of 2020-06-01 05:59 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?