TEACH HAIKU-DEMO.TXT
This teach file is based on TEACH HAIKU-EXAMPLES.TXT
The latter gives code examples to be run by the learner.
This file shows the results of the examples running, but without the
sound generator.

Online video tutorial:
A Demo Using 'recordmydesktop' on 15 Nov 2011 is available on Youtube,
here:
    http://www.youtube.com/watch?v=j0oaK59SSM0
(The recording unfortunately had 'grammatical' mis-spellt as 'gramattical'!)

Aaron Sloman
School of Computer Science
University of Birmingham, UK
http://www.cs.bham.ac.uk/~axs


This is a sequel to the demo of linux, espeak and pop11 on 13th Nov 2011
http://www.cs.bham.ac.uk/research/projects/cogaff/tutorials/pop-espeak1.ogv
also on youtube.

We shall use pop11's grammar package, described in the TEACH GRAMMAR
file in the poplog system.

Declare a variable 'mygram' and initialise it with alist or lists of
lists, specifying a simple context-free grammar:

    vars mygram =
    [
        ;;; start a list of grammatical rules
        ;;; a sentence is a NP then a VP
        [s [np vp]]


        ;;; a noun phrase is a determiner followed by a noun
        ;;; or a determiner + adjective + noun
        [np [det noun] [det adj noun]]

        ;;; verb phrase = verb followed by NP
        [vp [verb np]]
    ] ;

In principle the grammar could allow alternative forms of sentence, noun
phrase, verb phrase, etc. But not in this toy grammar.

However, the lexicon defined below gives words of different types, and
does allow alternatives for each type, e.g. "noun", "verb", etc:

    vars mylex =
      [       ;;; start a list of lexical categories
        ;;; nouns
        [noun  man girl number computer cup battle room car garage]

        ;;; verbs
        [verb  hated stroked kissed teased married taught added]

        ;;; adjectives
        [adj  big blue lonely clever excellent angry]

        ;;; determiners
        [det   the a every each one some]
     ];

;;; Print out the two lists:
mygram ==>
** [[s [np vp]] [np [det noun] [det adj noun]] [vp [verb np]]]

mylex ==>
** [[noun man girl number computer cup battle room car garage]
    [verb hated stroked kissed teased married taught added]
    [adj big blue lonely clever excellent angry]
    [det the a every each one some]]

Load the pop11 grammar library:

    uses grammar

Use one of its procedures to generate three sentences:

    generate(mygram, mylex) ==>
    ** [the lonely garage married each car]

    generate(mygram, mylex) ==>
    ** [the battle married some lonely girl]

    generate(mygram, mylex) ==>
    ** [some car teased each lonely cup]

We can also ask the program to parse these sentences. But first
we must set up a parser based on the grammar (mygram) and lexicon
(mylex):

    setup(mygram,mylex);

That creates parsing procedures, s, np, vp, etc.

    s([the lonely garage married each car]) ==>

That prints out (in a narrow text window):

** [s [np [det the] [adj lonely] [noun garage]]
          [vp [verb married] [np [det each] [noun car]]]]

    s([the battle married some lonely girl]) ==>
    ** [s [np [det the] [noun battle]]
          [vp [verb married]
              [np [det some] [adj lonely] [noun girl]]]]


    s([some car teased each lonely cup]) ==>
    ** [s [np [det some] [noun car]]
          [vp [verb teased]
              [np [det each] [adj lonely] [noun cup]]]]

That shows the grammatical structures of the generated sentences.



Let's generate some simple Haikus to be printed out.


-- Generating Haikus --------------------------------------------------

A haiku (A Japanese literary form) is a three line poem of a highly
constrained form. Examples (from Margaret Boden's book on creativity) are

    All green in the leaves
    I smell dark pools in the trees
    Crash moon has fled

    Eons deep in the ice
    I paint all time in a whorl
    Bang the sludge has cracked

These are of the form

    All [1] in the [2]
    I [3] [4] [5] in the [6]
    [7] the [8] has [9]

We specify a grammar, which is a set of templates, to generate haikus
as follows

    vars haiku_gram =
        [

            ;;; a haiku has three parts separated by newlines
            [haiku [part1 . ^newline part2 . ^newline part3 . ]]

            ;;; We now define the permitted forms for each part

            [ part1 [start_word adj in np]]

            ;;; Example: All green in the leaves


            [ part2 [I verb1 adj noun in np]]

            ;;; Example: I smell dark pools in the trees

            ;;; part3 has two forms, one with a singular noun phrase
            ;;; followed by "has" and the other with a plural noun
            ;;; phrase followed by "have"

            [ part3 [exclaim , sing_np has verb2]
                    [exclaim , plural_np have verb2]]

            ;;; Example: Crash moon has fled

            ;;; different types of noun phrases, singular and plural

            [np [sing_np][plural_np]]

            [sing_np [sing_det sing_noun]]

            [plural_np [plural_det plural_noun]]

            ;;; Nouns can be singular or plural, defined in the
            ;;; lexicon
            [noun [sing_noun] [plural_noun]]
        ];


;;; An example lexicon, for use with the above grammar

    vars haiku_lex =
        [
            ;;; adjectives
            [adj abrupt acrid crass crazy dark deep
                 flossy ghostly goulish greenish magenta
                 poetic purest rapt smelly tinkling tiny
                vicious welling white zany zealous
            ]

            ;;; Words to start part 1
            [start_word All Many Most So What How Days But]

            ;;; Singular and plural determiners

            [sing_det the some one every each my her your our their this]

            [plural_det the some all most many my your our his their
                these those two three four]

            ;;; Singular and plural nouns.

            [sing_noun acorn age anchor angel anguish bridge
                canopy cosmos dawn daylight death dew foal grass hatching
                laughter moon night ocean power
                heart spring sunset tiger winter zoo]

            [plural_noun
                ancestors autumns births collisions dancers devils
                echoes evenings forms galaxies ghosts
                heavens hosts poets raindrops rivers seas spirits
                storms summers tangles tempests torments sheep
                trees verses vessels waves watchers winters
            ]

            [verb1 abandon burn compose dangle detach
                engage expect fetch grasp greet hug mourn
                praise press sip slice spy stroke
                taste tear twist urge watch wipe
            ]

            [verb2 aged arisen bloomed blinked burst
                chimed come cracked drowned drooped eaten ended
                faded fallen fetched floundered frozen
                gone gripped gushed held hated loomed lost
                missed nursed oiled opened oozed riddled ripped rode
                sang slept smouldered swirled swarmed thawed unzipped
            ]

            ;;; words for an exclamation
            [exclaim
                Aha Alas Bang Crash Forever Ha Huh Joy Nay
                No Ouch Oh See So Ugh Woe Yea Yes Yippee]
        ];

These two commands will print out the grammar and the lexicon without
the comments:
haiku_gram ==>
haiku_lex ==>

We make the grammar library available using this command

    uses grammar

and compile an extension to allow us to generate other things than just
individual sentences (the default):

    uses generate_category

    ;;; set the maximum recursion level for the generator
    20 -> maxlevel;

    ;;; Generate 3 haikus, using the above grammar and lexicon
    repeat 3 times
        generate_category("haiku", haiku_gram, haiku_lex) ==>
        pr(newline);
    endrepeat;

Sample output generated by that command:

** [Days crazy in this dawn .
 I sip white anchor in our seas .
 Bang , one spring has eaten .]

** [Days rapt in my foal .
 I slice crass angel in the sunset .
 Alas , your winter has hated .]

** [So crass in their heavens .
 I urge white heavens in your heavens .
 Bang , every winter has lost .]


===================================================================
Here are more examples of what the program can print out

** [But crazy in your night .
        I watch poetic laughter in four waves .
        Aha , four heavens have riddled .]

** [So flossy in their spring .
       I engage crazy autumns in many heavens .
       So , his verses have gone .]

** [How magenta in his dancers .
        I hug crazy age in those collisions .
        Alas , one daylight has loomed .]

If you run this program live you can turn the output list into a string
of words to give to pop11 to speak out loud, using 'espeak'.

-- The procedure that makes haikus

Now let's create a procedure to generate, then print out, a number of
haikus, using a given grammar, lexicon, and the number of haikus
desired.

define make_haikus(gram, lex, num);

    lvars output, string;

    repeat num times

        generate_category("haiku", gram, lex) -> output;

        ;;; print out the output, including newlines

        output ==>
        ;;; add a gap
        pr(newline)

    endrepeat;

enddefine;


make_haikus =>
** <procedure make_haikus>

make_haikus(haiku_gram, haiku_lex, 1);

** [How welling in our seas .
 I twist ghostly verses in her dawn .
 Yes , this bridge has held .]

;;; When a list is printed out in pop11, the elements are
;;; separated by spaces. This applies also to elements that are
;;; single-character words like "." and ",". It would be possible
;;; use a slightly different mode of printing that omitted the
;;; space just before one of those.

;;; So now let's generate and print out five more haikus

make_haikus(haiku_gram, haiku_lex, 5);

** [But flossy in the torments .
        I mourn tinkling autumns in every power .
        Bang , some anguish has cracked .]

** [Many crazy in the spirits .
        I spy poetic grass in his waves .
        So , two poets have fallen .]

** [But crazy in their watchers .
        I taste dark collisions in the death .
        Oh , my trees have bloomed .]

** [So magenta in their power .
        I wipe goulish winter in your forms .
        Yippee , these dancers have hated .]

** [What white in many rivers .
        I fetch goulish ocean in this bridge .
        Nay , my dancers have come .]

That's all absolute rubbish, but by a careful choice of contents of
the grammar and lexicon it would be possible to improve some of
the grammar and continuity.
For more on this see these Pop11 teach files:

    TEACH GRAMMAR

    TEACH STORYGRAMMAR

--- $usepop/pop/teach/haiku-demo.txt