Wednesday 3 December 2014

random greetings in BKO

This time, an example of random greetings. Makes use of general rules, stored rules, merge-labels(), the self ket and pick-elt.

Let's define our greetings:
|context> => |context: greetings play>

hello |*> #=> merge-labels(|Hello, > + |_self> + |!>)
hey |*> #=> merge-labels(|Hey Ho! > + |_self> + |.>)
wat-up |*> #=> merge-labels (|Wat up my homie! > + |_self> + | right?>)
greetings |*> #=> merge-labels(|Greetings fine Sir. I believe they call you > + |_self> + |.>)
howdy |*> => |Howdy partner!>
good-morning |*> #=> merge-labels(|Good morning > + |_self> + |.>)
gday |*> #=> merge-labels(|G'day > + |_self> + |.>)
random-greet |*> #=> pick-elt ( hello |_self> + hey |_self> + wat-up |_self> + greetings |_self> + howdy |_self> + good-morning |_self> + gday |_self>)
Now, let's put it to use:
sa: random-greet |Fred>
|Greetings fine Sir. I believe they call you Fred.>

sa: random-greet |John>
|Good morning John.>

sa: random-greet |Emma>
|Hello, Emma!>

-- define who we are talking with:
sa: |you> => |Tim>
-- greet them:
sa: random-greet "" |you>
|G'day Tim.>
Next, with a little work, we can greet a list of people.
-- define this beasty first:
sa: friends-list |*> #=> extract-value list-to-words friends |_self>

-- define some friends:
sa: friends |Sam> => |Charlie> + |George> + |Emma> + |Jack> + |Robert> + |Frank> + |Julie>
sa: friends |Emma> => |Liz> + |Bob>

-- check the output of the friends-list operator:
sa: friends-list |Sam>
|Charlie, George, Emma, Jack, Robert, Frank and Julie>

sa: friends-list |Emma>
|Liz and Bob>
-- notice that "list-to-words" has converted a superposition of names into an English like list of names.
-- eg: 
sa: list-to-words (|a> + |b> + |c> + |d>)
|text: a, b, c and d>

-- now, let's greet them:
sa: random-greet friends-list |Emma>
|Wat up my homie! Liz and Bob right?>

sa: random-greet friends-list |Sam>
|G'day Charlie, George, Emma, Jack, Robert, Frank and Julie.>
Anyway, I think that is cool! More to come. Also, a lol at the vast amount of debugging noise this generates! An addition: the above is a good reason why merge-labels() should be built into the parser. One idea is to introduce the _ character to represent merge-labels():
hello |*> #=> |Hello, > _ |_self> _ |!>
hey |*> #=> |Hey Ho! > _ |_self> _ |.>
wat-up |*> #=> |Wat up my homie! > _ |_self> _ | right?>
greetings |*> #=> |Greetings fine Sir. I believe they call you > _ |_self> _ |.>
howdy |*> => |Howdy partner!>
good-morning |*> #=> |Good morning > _ |_self> _ |.>
gday |*> #=> |G'day > _ |_self> _ |.>
This is much cleaner than the merge-labels() version! Indeed, there are other possibilities. I haven't given it much thought yet.
eg: something like this perhaps:
hello |*> #=> insert-self |Hello, ${_self}!>
And then plurals would look like:
plural |word: *> #=> insert-self |${_self}s>
Anyway, some thinking to do.
Update: I'm thinking of going for something like this:
hey |*> #=> insert[|_self>] |Hey ${1}!>
hello-fred-and |*> #=> insert[|Fred>,|_self>] |Hey ${1} and ${2}! How is it going?>
A couple of notes:
1) Everywhere else in the project my numbers start from 1 not the comp-sci convention of 0. So it would break that convention if I used ${0} and ${1} here.
2) The current parser can't handle an operator like insert[] with ket parameters. On the long to-do list!

Update: Wrote an improved version of random-greet. This time the list of greet operators has been separated out. Key changes are these two lines:
|greet list> => |op: hello> + |op: hey> + |op: wat-up> + |op: greetings> + |op: howdy> + |op: good-morning> + |op: gday>
random-greet |*> #=> apply(pick-elt "" |greet list>,|_self>) 
This change has a couple of advantages:
1) we can dynamically change our list of greetings without having to redefine the random-greet operator, instead we now redefine |greet list>. I think this is an improvement.
2) the old version calculated the greetings in full, and then picked from the results. In the new method only the chosen greeting is calculated. So in this case, not a super big win, but if we use a similar technique on a larger example, then it will be a win.

No comments:

Post a Comment