# Rule-Based Prediction AlgorithmsΒΆ

A rule-based prediction program consists of a collection of rules. Each rule has the structure of an if-then statement. The if part checks for some Boolean condition on the features of an item. When the condition is true, the then part provides something to be used in the final output. In the programs we consider, the rules will be collected in an ordered list, so that rules earlier in the list take precedence over rules later in the list if they produce conflicting outputs.

Consider, first, the classification task of automatically labeling people’s names with genders: “male” or “female”. In the code below, the variables males and females are set to hold some pre-labeled data. (If you’re wondering why these names are so unusual, they are a random sample from a corpus of names that is distributed with the NLTK (Natural Language ToolKit) python package.)

If you look at these samples of names, one thing that might jump out at you is that most, though not all, of the names ending in i, e, and a are female. So, we could make a crude classifier as follows.

Note the structure of the classify() function. It checks each of the three rules, in turn. Each rule checks for an indicator of whether the name is female. If any of them match, it returns “female”. If none of the rules matches, it returns male. Think of “return male” as the default rule or “male” as the default label for this classifier. Given that structure, we might implement things a little more cleanly. We can think of each rule as having a boolean function (the if part) and an outcome (“male” or “female”). This is represented as a tuple in the code below. rls is a list of such tuples. The function iterates through all the rules. It applies the boolean function to the name s and, if it evaluates to True, it returns the label (for all three rules, “female”).

Here’s the same thing in codelens, so you can step through it one line at a time.

(prediction_3a)

For those of you who preferred lambda expressions when passing a function for the key parameter when sorting, you may find the following, equivalent code, easier to understand.

When we call the classify function we can pass a different set of rules. For example, with the rules we have used so far, “Enrique” is incorrectly classified as female. Before checking whether the last letter is e, we can check whether the first two letters are “En”. This leads to correct classification not only of “Enrique” but also “Ender”, “Engelbert”, “Enoch”, and “Enrico”. (Unfortunately, it leads to incorrect classification of “Enrica” and “Enya”.)

Note here how important the order of the rules is. If the check for whether the word starts with “En” is not placed at the beginning of the list, the match on the ending letter ‘e’ will cause the classify function to return “female” without ever considering the rule that checks whether the name starts with “En”.

prediction-1: What is the type of rules[0]?
• (A) list
• rules is a list, but each of the elements is not.
• (B) tuple
• rules is a list of tuples.
• (C) integer
• The first element of the list is not an integer.
• (D) string
• The first element of the list is not a string.
• (E) function
• The lambda expression evaluates to a function object, but the lambda expression is not the entirety of the first item.
prediction-2: What is the type of rules[0][0]?
• (A) list
• The first element of each rule tuple is not a list.
• (B) tuple
• rules[0] is a tuple, but its first element is not.
• (C) integer
• The first element of each rule tuple is not an integer.
• (D) string
• The first element of each rule tuple is not a string.
• (E) function
• The lambda expression evaluates to a function object.
prediction-3: What expression would you use to pick out the string “male”?
• (A) rules[1][1]
• That's "female" (position 1 is the second item in the list).
• (B) rules[1][0]
• That's a function object.
• (C) rules[0][1]
• The second element of the first tuple is "male".
• (D) rules[0][0]
• That's a function object.
Next Section - The Shannon Game: Guessing the Next Letter in a Text