Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

Sentence format

by Kickstart (Pilgrim)
on Feb 21, 2001 at 21:24 UTC ( [id://59972]=perlquestion: print w/replies, xml ) Need Help??

Kickstart has asked for the wisdom of the Perl Monks concerning the following question:

Hi all,

I would like to write a script that spews sentences from a word list in proper english formats, but am having trouble figuring out what those formats are and how I'd do it...the pseudoperl below may illustrate:

@sentenceformats = ( 'article verb noun', 'article verb noun verb noun' ) ***Here would be a section that takes a random one of @sentenceformats + and picks some articles, verb and nouns from a word list and puts th +em in the format supplied, thereby making a nonsensical, but valid st +ructurally english sentence.

Any ideas would be appreciated.

Kickstart

Replies are listed 'Best First'.
Re: Sentence format
by TheoPetersen (Priest) on Feb 21, 2001 at 21:40 UTC
    See Coy for an example of how to do this. (And it generates better haiku than a lot of monks :)
Re: Sentence format
by arturo (Vicar) on Feb 21, 2001 at 21:32 UTC

    You've got a bunch of lists; the basic idea seems pretty simple, if you lay your data out right. Select random form, then, for each wordtype in the form, select a random word of that type.

    Here's one way :store your words in a hash of array references, e.g.

    $word{noun} = [ qw(bird dog table dollar econonmy ennui) ]; $word{verb] = [ qw(eat drink fire annoy) ]; $word{article}= [ qw(a the an) ];

    (this *can* be done more elegantly =)

    Then, you can specify your formats however you'd like, say, as an array of strings. Use rand on both the array of sentence forms, and on the anonymous arrays of each word type.

    To get the last index in an array: $#arrayname, and remember, Perl array indices start at 0!

    HTH then

    Philosophy can be made out of anything. Or less -- Jerry A. Fodor

Re: Sentence format
by marius (Hermit) on Feb 21, 2001 at 21:37 UTC
    Assuming you keep your articles/verbs/nouns in different files, this wouldn't be terribly difficult:
    my(@sentenceformats) = ( 'a v n', 'a v n v n' ); #pick a sentence format -- there's probably a much #easier way to do this! my($sent) = int(rand($#sentenceformats))); my(@format) = split(/ /, $sentenceformat[$sent]); for (@format) { if (/a/) { #pick a article } elsif (/v/) { #pick a verb } elsif (/n/) { #pick a noun } else { warn "Unknown sentence fragment\n"; }; };

    Of course, this is all untested, and I /think/ it should work.. YMMV.

    -marius
Re: Sentence format
by merlyn (Sage) on Feb 21, 2001 at 22:52 UTC
Re: Sentence format
by SilverB1rd (Scribe) on Feb 21, 2001 at 23:27 UTC
    Many years ago a friend and I put together a program like yours in Qbasic the best line we ever got out of it was "Corrupt president shoots crooked politician "
Re: Sentence format
by InfiniteSilence (Curate) on Feb 21, 2001 at 21:49 UTC
    Grammar only requires a subject and an intransitive verb for completeness:
    perl -e "$word{'noun'} = [qw(I you we)];$word{'verb'}=[qw(kill walk sm +ile)]; print $word{noun}->[2] . q( ) . $word{verb}->[1];"

    Can return 'we kill' or 'I smile', both valid sentences. Transitive verbs are something like 'grace' in "I grace the scene" and requires some object to complete the sentence. That's it for grammar review.

    Celebrate Intellectual Diversity

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://59972]
Approved by root
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others about the Monastery: (5)
As of 2024-03-28 22:54 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found