The Book of AI

How To Build Chatbots with The Personality Forge
Chapters
Chapter 3: Expanding on Keyphrases
1 Wildcards in Keyphrases

Wildcards are placeholders in Keyphrases that match certain kinds of things. They are essential to writing good Keyphrases as they expand the reach of what is matched.

Soft Wildcards

What if you have the Keyphrase "i like" but you'd also like it to also match "i really like"? You need the * wildcard. It will match anything or nothing. To use it, write your Keyphrase like this: "i * like". This is called a soft wildcard, because it's optional. The soft wildcard is essential in extending the reach of your Keyphrases, and should be used liberally when writing them. Before saving, read through your Keyphrase and imagine anyplace where the word "really" or "big" could fit, and put a * there.

* matches anything or nothing

Keyphrase: do you * like
Matches: do you like, do you really like

But wait, "i * like" also matches "i look like a monkey", which should have different Responses. In that case, you can use the (r) wildcard, which matches adverbs or nothing. Adverbs are words that describe actions, like "really", "quickly", and "usually".

(r) matches adverbs or nothing

Keyphrase: do you (r) like
Matches: do you like, do you really like

Reversing Words

But what about words that reverse the meaning like "not", "never", and "rarely"? You wouldn't want the Keyphrase "i * want to eat" to match "I do not want to eat". Not to worry! The AI Engine prevents * and (r) from matching words like these.

Word Type Wildcards

Keyphrases can use single and multi-word wildcards for particular types of words or groups of words. For example, the Keyphrase "you are (adjective)" will match "You are silly" and "You are brilliant", but not "You are a horse". These are very powerful wildcards and do wonders in nailing down the particular type of sentences you want your Keyphrases to match. Unless otherwise stated, all wildcards must match something (they are not optional matches.

Remember learning about verbs, nouns, prepositions, and pronouns in school? Now that information really shines, as this is how we match the parts of sentences. I'll remind you what those all mean here.

Longer Word Type Wildcards

These are the most useful word type wildcards, as they cover all sorts of forms of the kind of thing you are probably looking for. (np) and (vp) are the best to use in most situations.

(np) stands for noun phrase, which matches a thing or person. It's any series of adjectives, articles, nouns, pronouns, and prepositions.
Matches: the big wet dog by the door, my filthy hand on the table, a pumpkin of doom

Less common variations:

(adjartnoun) - any series of adjectives, determiners, nouns, and pronouns.
Matches: the big wet dog, my filthy hand, a pumpkin

(adjnoun) - any series of adjectives and nouns.
Matches: big wet dog, filthy head, pumpkin

(prenoun) - an OPTIONAL match for things that may precede a noun: adjectives, determiners, and other nouns.
Matches: big wet, filthy, nothing

(vp) stands for verb phrase, which matches an action; any series of verbs, adverbs, and prepositions.
Matches: ran very quickly over, snorted loudly, fall down suddenly

Less common variation:

(verbadv) - any series of verbs and adverbs.
Matches: ran very quickly, snorted loudly, fall

Single Word Type Wildcards

These are less-commonly used wildcards. Most often you'll be trying to match (np) and (vp), but there are times when you might want to use one of the following:

(noun) - a single noun.
Matches: book, pies, monkey

(verb) - a single verb.
Matches: run, thinking, gargle

(adj) - a single adjective.
Matches: silly, yellow, grunting

(adv) - a single adverb.
Matches: swiftly, loudly, really

(prep) - a single preposition.
Matches: under, against, between

(det) - a single determiner.
Matches: a, an, the, my, your

(name) - a single name.
Matches: Benji, Josephine, Gwendolyn, Bruce

(number) - a single number.
Matches: 14, twenty-one, 42

Word List Wildcards

You can also list words or phrases in parenthesis separated by the vertical line "|" character. The AI Engine will match any one item in the list. You can also put word-list wildcards inside other word-list wildcards.

Keyphrase: are you a (guy|man|dude|boy|male)
Matches: are you a guy, are you a man, are you a dude

Keyphrase: i (desire|want|yearn for) you
Matches: I desire you, I want you, I yearn for you

Keyphrase: can we * ((talk|chat|speak) * about|discuss) (np)
Matches: can we discuss the moon, can we please talk about bikes

Optional Word Lists

You can also specify optional word lists in your Keyphrases. Essentially, you create a list like above with the last element being blank. It matches a word in the list or nothing. Why use these instead of *? These are good if you want to narrow down the things that could match. They also increase your Keyphrase's Rank when matched.

Keyphrase: i am (adj) (that|) you
Matches: I am glad you like worms, I am happy that you are coming home.

Keyphrase: i am (adj) (when|because|that|) you
Matches: I am glad you like worms, I am happy that you are coming home, I am pleased because you are hilarious.

The Hard Wildcard

The hard wildcard (*) matches anything, as long as there is something there. It can match one or more words. This is less useful than the word type wildcards above, so use those instead when possible. But on the rare occasion, a hard wildcard is called for.

Keyphrase: i know (*)
Matches: "I know Desti.", but does NOT match "I know."

Matching the Beginning and End

You can match the beginning and end of phrases (sentences or parts of sentences separated by commas) with these. You should use them rarely - only when leaving them off would change the meaning.

^ matches the beginning of a phrase or sentence

Keyphrase: ^(do|did|are|were) you
Matches: "did you go" but not "where did you go"

$ matches the end

Keyphrase: (do|can) you * think$
Matches: "do you think" but not "do you think cats are funny"

Both:

Keyphrase: ^(vp) (np)$
Matches: "eat this peach" but not "could you eat this peach tomorrow"
Custom Plug-in Wildcards

What if you want a much longer list of options? It would be too much for you to type into a single Keyphrase. You can use the Personality Forge's Custom Plug-ins for this. Custom Plug-ins in Keyphrases match any one of the items listed. These lists can be created by you or shared by others. You can see the available ones on the Plug-ins page.

Keyphrase: i (vp) (p:park)
Matches: I've been to Yosemite, I enjoy seeing Yellowstone

Keyphrase: (p:badweather) * coming
Matches: There is a thunderstorm coming, I hope a typhoon isn't coming

Here are some that are very useful in Keyphrases:

(p:person-key) any type of person, such as "teacher" or "highlander".
(p:person-positive) a positive person, such as "hero" or "prodigy".
(p:person-negative) a negative person, such as "jerk" or "degenerate".
(p:person-crazy) a crazy person, such as "loony" or "weirdo".

(p:adj-pos) a positive adjective, such as "funny" or "dashing".
(p:adj-neg) a negative adjective, such as "smelly" or "vile".
(p:adj-amp) a crazy adjective, such as "bonkers" or "maniacal".

(p:quality-positive) a positive quality, such as "bravery" or "ingenuity".
(p:quality-negative) a negative quality, such as "vileness" or "negativity".
(p:quality-crazy) a crazy quality, such as "bizarreness" or "quirkiness".

(p:verb-action-pos) a positive verb, such as "like" or "miss".
(p:verb-action-neg) a negative verb, such as "dislike" or "blame".

(p:verb-effect-pos) a positive feeling verb, such as "inspire" or "impress".
(p:verb-effect-neg) a negative feeling verb, such as "upset" or "bother".

And thousands more on the Plug-ins page.

Wildcard Inflectors

There's a shortcut if you want to match different inflections of single words or lists of words. They're called Wildcard Inflectors. You begin with word bases, then add a marker for what inflections you also want to match. For nouns, you can use +s to match plural tense. For verbs, you can use +s to match third person tense, +ed to match past tense, and +ing to match continuous tense. For adjectives, you can use +er and +est. These can be added to the end of single words, Word List Wildcards, and Plugin-in Wildcards.

Keyphrase: what * rhyme+s with (*)
Matches: What words rhyme with possum, What rhymes with possum

Keyphrase: tell me your * reason+s+ing
Matches: Tell me your reason, Tell me your reasons, Tell me your reasoning

Keyphrase: (is|does) (np) * (like|love|enjoy)+ing (np)
Matches: Is the boar enjoying my song? Does your sister like thunderstorms?

Keyphrase: are you * (hungry|thirsty)+er+est
Matches: Are you feeling thirsty? Are you hungrier than before?

Putting Them Together

All of these can be put together to match many ways of saying something.

  • (do you * want|would you * like) * (p:food-drink)
  • have you (ever|) (gone to|been to|visited) (np)
  • what * (do you * have|have you * got)
  • we * are * (going|heading|leaving|off) (to|for) ((go|head) * to|) (np)
Lists of Keyphrases

If you want more than one Keyphrase to trigger the same set of Responses and they don't fit into Word-List wildcards like those above, you can list them. In the Language Center, this can be done either one per line or separated by commas. Wherever possible, use Word-List wildcards, as they are more efficient. Following is an example of a single Keyphrase entry as a list of Keyphrases.

Keyphrase:
let us * ((talk|chat|speak) * about|discuss) (np),
(talk|chat|speak) * (to|with) me * about (np),
(do you * want|would you * like) * to * ((talk|chat|speak) * about|discuss) (np)

Laughter

If you want to match laughter: ROFL, LOL, hahaha, etc., then create the Keyphrase "haha".

2 Keys
Keys

An incredibly powerful and engaging aspect of Keyphrases is the ability for your chatbot to return phrases and topics said to them in their Responses. This is accomplished through Keys. Each wildcard in a matched Keyphrase automatically produces a Key, which are Plug-ins you can use in your Responses. This only applies to the outer level. Wildcards inside other wildcards do not produce keys.

Keyphrase: (do you * want|would you * like) * to * ((talk|chat|speak) * about|discuss) (np)

Key Contents
(key1) (do you * want|would you * like)
(key2) *
(key3) *
(key4) ((talk|chat|speak) * about|discuss)
(key5) (np)

But what if I update my Keyphrase? Do I have to change all the Key numbers in my responses? What if I have multiple Keyphrases listed and the keys don't line up? Key Mapping to the rescue!

Key Mapping

Key Mapping lets you set your own keys in whatever order you want. And better yet - you only need to set the ones you want to use! Starting with 1, you add :1 to the end of they key, and that produces (key1).

Single Keyphrase
Most likely we're only interested in using the noun phrase (np), so we write:

(do you * want|would you * like) * to * ((talk|chat) * about|discuss) (np:1)

Key Contents
(key1) (np)

Keyphrase List

(name:1) * is * my (np:2),
my (np:2) * is * (name:1),
i * have a (np:2) * named (name:1)

Key Contents
(key1) (name)
(key2) (np)

You can only use Key Mappings on outermost Wildcards, since Wildcards inside other Wildcards do not generate Keys. Also, if you want to map a *, write it like this: *:1.

Key Mapping is so useful, I'd recommend always using it instead of the automatically generated Keys.

Prekey and Postkey

There are two more Keys generated with each Keyphrase match.

(prekey): the part of the sentence BEFORE the matched Keyphrase.

(postkey): the part of the sentence AFTER the matched Keyphrase.

3 Writing Good Keyphrases

There is an art to writing good Kephrases. By this I mean flexible, useful, successful Keyphrases that will match all the versions of what you're looking for. Here are some examples of Keyphrases with problems and how to improve them:

Too Long

Keyphrase: i once knew a (np:1) that (vp:2) (np:3) on (his|her|its) (adj:4) (np:5)

This will likely never match, because it's too long. How often will someone say something that matches this? Probably never. So let's shorten it. We'll remove the end, which could be accessed by (postkey) if there's something there. Let's remove the "a" so that the (np) could also match words like "three" or "several":

Improved: i once knew (np:1) that could (vp:2) (np:3)

Too Specific

Keyphrase: the wild dogs took my pajamas then ate them

This is too specific. Nobody is likely to say that exact phrase. So let's make it more general. Unless "wild dogs" or "pajamas" are something brough up by the chatbot, let's make them into (np) so they can match all kinds of things.

Improved: (np:1) took my (np:2)

Lacking Context

Keyphrase: pants

Sure, your Bot could say something general about pants, but a person would feel like your chatbot didn't understand what was said to it. This word could be part of many different kinds of questions or statements. If "pants" is a topic raised by your Bot, it'd be better to write Keyphrases with some context:

Improved: where are * pants
Improved: i * (like|love|adore) * pants
Improved: (prenoun) pants are * (awesome|wonderful|cute|stylish)
Improved: i * (dislike|hate|despise) * pants

4 The AI Engine

The AI Engine does a lot of things to help your Keyphrases match what you intend them to, meaning there's less work for you when writing your Keyphrases.

Meta Macros

Meta Macros take complex sentence formations and reduce them to their essential core.

Message: I think that you are amazing.
Becomes: You are amazing.

Message: I was wondering if you knew what clouds are made of.
Becomes: What are clouds made of?

Message: Do you have any idea why the badger took the blade?
Becomes: Why did the badger take the blade?

Message: What I need is for you to bring me the cheese.
Becomes: Bring me the cheese.

There are over 800 different sentence structures (and growing) that the AI Engine can reduce like this, meaning you don't need to write Keyphrases for every conceivable way of saying things - just the more essential ones.

Message: Do you have any idea why the badger took the blade?

Keyphrase: why * did (np:1) (vp:2)

Thanks to Meta Macros, this Keyphrase will match (see example above).

Now if you want to match something in the original message, you still can. The original message is looked at as well when deciding which of your Keyphrases fits best.

Message: I think that you are amazing.

Keyphrase 1: you * are (adj:1)
Keyphrase 2: i * think * you * are (adj:1)

Since the original message is also searched, Keyphrase 2 will be selected since it is a longer match.

Answer Completion

The opposite of Meta Macros is Answer Completion. It takes a short answer to a question, and turns it into a full sentence.

Bot: What kind of music do you like?
Person: Silly children's music.
Becomes: I like silly children's music

Bot: What should I do if the moon explodes?
Person: Close your eyes.
Becomes: You should close your eyes if the moon explodes.

Bot: Where is the pants festival?
Person: At my house.
Becomes: The pants festival is at my house.

These full sentences are easier for your Keyphrases to match. You'll see that these can also save you a lot of work when it comes to writing Seeks, as your Keyphrases may be able to pick up the answer when it's formed into a full sentence.

Numbers

The AI Engine simplifies matching numbers for you. All numeric words are translated to numbers. Take the number 36. It can be written 36, thirty six, or thirty-six. In your Keyphrase, you only need to match the number 36. If you like, you can also words for numbers from "zero" up to "nineteen" and they will still match numbers.