## Sunday, 20 December 2015

### how well do you know X

What does it mean when you ask "how well do you know X?" One possible definition: the amount of "everything" you know about a given ket.

Here, let's drop to BKO to try and demonstrate:
```-- load some sample data:

-- define the everything operator:
sa: everything-op |*> #=> apply(supported-ops |_self>,|_self>)

-- apply it to all known kets in the sw file:
sa: map[everything-op,everything] rel-kets[*] |>

-- define operators to measure how big "everything" is:
sa: how-many-everything |*> #=> how-many everything |_self>
sa: how-many-everything-2 |*> #=> how-many everything^2 |_self>
sa: how-many-everything-3 |*> #=> how-many everything^3 |_self>

-- show a pretty table:
sa: table[ket,how-many-everything,how-many-everything-2,how-many-everything-3] rel-kets[*] |>
+-----------------------+---------------------+-----------------------+-----------------------+
| ket                   | how-many-everything | how-many-everything-2 | how-many-everything-3 |
+-----------------------+---------------------+-----------------------+-----------------------+
| _list                 | 6                   | 56                    | 8                     |
| Washington            | 12                  | 1                     | 0                     |
| George Washington     | 1                   | 0                     | 0                     |
| Adams                 | 8                   | 1                     | 0                     |
| John Adams            | 1                   | 0                     | 0                     |
| Jefferson             | 12                  | 3                     | 0                     |
| Thomas Jefferson      | 1                   | 0                     | 0                     |
| Madison               | 12                  | 3                     | 0                     |
| James Madison         | 1                   | 0                     | 0                     |
| Monroe                | 12                  | 3                     | 0                     |
| James Monroe          | 1                   | 0                     | 0                     |
| Q Adams               | 8                   | 3                     | 0                     |
| John Quincy Adams     | 1                   | 0                     | 0                     |
| Democratic-Republican | 2                   | 0                     | 0                     |
| *                     | 0                   | 0                     | 0                     |
+-----------------------+---------------------+-----------------------+-----------------------+
```
Now, another example:
```-- load another set of data:

-- process this data:
sa: everything-op |*> #=> apply(supported-ops |_self>,|_self>)
sa: map[everything-op,everything] rel-kets[*] |>
sa: how-many-everything |*> #=> how-many everything |_self>
sa: how-many-everything-2 |*> #=> how-many everything^2 |_self>
sa: how-many-everything-3 |*> #=> how-many everything^3 |_self>

-- show a pretty table:
sa: table[ket,how-many-everything,how-many-everything-2,how-many-everything-3] rel-kets[*] |>
+---------+---------------------+-----------------------+-----------------------+
| ket     | how-many-everything | how-many-everything-2 | how-many-everything-3 |
+---------+---------------------+-----------------------+-----------------------+
| Bella   | 19                  | 0                     | 0                     |
| Emma    | 19                  | 0                     | 0                     |
| Madison | 22                  | 38                    | 0                     |
| *       | 0                   | 0                     | 0                     |
+---------+---------------------+-----------------------+-----------------------+
```
So not a bad start at defining "how well you do know X?".

Now, lets try to automate this a bit, by making use of the fact that loading sw files can do computation.

So first, let's create this sw file (NB: we must not define a context since we want it to be applied to the current context, instead of its own):
```\$ cat sw-examples/how-well-do-you-know.sw
-- process the data:
everything-op |*> #=> apply(supported-ops |_self>,|_self>)
|null> => map[everything-op,everything] rel-kets[*] |>

how-many-everything |*> #=> how-many everything |_self>
how-many-everything-2 |*> #=> how-many everything^2 |_self>
how-many-everything-3 |*> #=> how-many everything^3 |_self>

-- show a pretty table:
|null> => table[ket,how-many-everything,how-many-everything-2,how-many-everything-3] rel-kets[*] |>
```
NB: the "|null> => ..." is needed because only valid learn rules are processed during sw file loading. So we just use |null> as a dummy variable. If you leave them out, then our map and table lines would not be processed.

Now, put it to use:
```\$ ./the_semantic_db_console.py
Welcome!

+------+---------------------+-----------------------+-----------------------+
| ket  | how-many-everything | how-many-everything-2 | how-many-everything-3 |
+------+---------------------+-----------------------+-----------------------+
| Fred | 8                   | 0                     | 0                     |
| Sam  | 7                   | 0                     | 0                     |
| *    | 0                   | 0                     | 0                     |
| null | 0                   | 0                     | 0                     |
+------+---------------------+-----------------------+-----------------------+
```
And another example:
```sa: load breakfast-menu.sw
+-----------------------------+---------------------+-----------------------+-----------------------+
| ket                         | how-many-everything | how-many-everything-2 | how-many-everything-3 |
+-----------------------------+---------------------+-----------------------+-----------------------+
| breakfast                   | 5                   | 19                    | 0                     |
| Belgian Waffles             | 4                   | 0                     | 0                     |
| Strawberry Belgian Waffles  | 4                   | 0                     | 0                     |
| Berry-Berry Belgian Waffles | 4                   | 0                     | 0                     |
| French Toast                | 4                   | 0                     | 0                     |
| Homestyle Breakfast         | 4                   | 0                     | 0                     |
| waffles                     | 1                   | 0                     | 0                     |
| belgian                     | 1                   | 0                     | 0                     |
| strawberries                | 2                   | 0                     | 0                     |
| berries                     | 2                   | 0                     | 0                     |
| french                      | 1                   | 0                     | 0                     |
| toast                       | 1                   | 0                     | 0                     |
| breakfast                   | 1                   | 0                     | 0                     |
| egg                         | 1                   | 0                     | 0                     |
| eggs                        | 1                   | 0                     | 0                     |
| bacon                       | 1                   | 0                     | 0                     |
| sausage                     | 1                   | 0                     | 0                     |
| two                         | 1                   | 0                     | 0                     |
| cream                       | 1                   | 0                     | 0                     |
| *                           | 0                   | 0                     | 0                     |
| null                        | 0                   | 0                     | 0                     |
+-----------------------------+---------------------+-----------------------+-----------------------+
```
I think that is kind of cool!