Sunday 30 November 2014

learning indirectly

A common thing when having a conversation is to have a set of variables keeping track of who you are talking about. The simplest is "you".
So, in my scheme how can we learn something about you?
We can't simply do:
-- first define context:
sa: context learn you
sa: |you> => |Fred>
sa: age |you> => |age: 29>

Here is why:
sa: dump
----------------------------------------
|context> => |context: learn you>

 |you> => |Fred>
age |you> => |age: 29>
----------------------------------------
We really wanted Fred to be 29, not "you".

So, it took some coding! but we have a new construct:
OP SUPERPOSITION-1 => SUPERPOSITION-2
which maps to:
OP KET => SUPERPOSITION-2
for all kets in SUPERPOSITION-1

Example:
-- see the current definition of "you"
sa: "" |you>
|Fred>
-- so it is "Fred"

-- let's learn his age:
sa: age "" |you> => |age: 29>

-- see what we know:
sa: dump
----------------------------------------
|context> => |context: learn you>

 |you> => |Fred>
age |you> => |age: 29>

age |Fred> => |age: 29>
----------------------------------------
-- success!

Now, swap "you" to "Sam", and learn his age:
sa: |you> => |Sam>
sa: age "" |you> => |age: 34>

sa: dump
----------------------------------------
|context> => |context: learn you>

age |you> => |age: 29>
 |you> => |Sam>

age |Fred> => |age: 29>

age |Sam> => |age: 34>
----------------------------------------
-- again, success!

Now for another example, learn daily closing times for a shop:
sa: context shop closing time

sa: |weekday: _list> => |Monday> + |Tuesday> + |Wednesday> + |Thursday> + |Friday>
sa: |weekend: _list> => |Saturday> + |Sunday>

sa: closing-time "" |weekday: _list> => |time: 6pm>
sa: closing-time "" |weekend: _list> => |time: 4:30pm>

sa: dump
----------------------------------------
|context> => |context: shop closing time>

 |weekday: _list> => |Monday> + |Tuesday> + |Wednesday> + |Thursday> + |Friday>

 |weekend: _list> => |Saturday> + |Sunday>

closing-time |Monday> => |time: 6pm>

closing-time |Tuesday> => |time: 6pm>

closing-time |Wednesday> => |time: 6pm>

closing-time |Thursday> => |time: 6pm>

closing-time |Friday> => |time: 6pm>

closing-time |Saturday> => |time: 4:30pm>

closing-time |Sunday> => |time: 4:30pm>
----------------------------------------

More later!

Update: with my new pretty print table code, we can now do this:
sa: table[day,closing-time] ("" |weekday: _list> + "" |weekend: _list>)
+-----------+--------------+
| day       | closing-time |
+-----------+--------------+
| Monday    | time: 6pm    |
| Tuesday   | time: 6pm    |
| Wednesday | time: 6pm    |
| Thursday  | time: 6pm    |
| Friday    | time: 6pm    |
| Saturday  | time: 4:30pm |
| Sunday    | time: 4:30pm |
+-----------+--------------+
Update: we can tidy up the operator names a little so they are more like natural English:
  list-of |week days> => |Monday> + |Tuesday> + |Wednesday> + |Thursday> + |Friday>
  list-of |weekend days> => |Saturday> + |Sunday>
  closing-time list-of |week days> => |time: 6pm>
  closing-time list-of |weekend days> => |time: 4:30pm>
  table[day,closing-time] list-of (|week days> + |weekend days>)
+-----------+--------------+
| day       | closing-time |
+-----------+--------------+
| Monday    | 6pm          |
| Tuesday   | 6pm          |
| Wednesday | 6pm          |
| Thursday  | 6pm          |
| Friday    | 6pm          |
| Saturday  | 4:30pm       |
| Sunday    | 4:30pm       |
+-----------+--------------+
Update: we can also make the learn-you more like natural English:
  name-of |you> => |Fred>
  age-of name-of |you> => |age: 29>
  name-of |you> => |Sam>
  age-of name-of |you> => |age: 34>
  dump
----------------------------------------
|context> => |context: sw console>

name-of |you> => |Sam>

age-of |Fred> => |age: 29>

age-of |Sam> => |age: 34>
----------------------------------------
Update: another more interesting learning indirectly example. This time one extra operator deep:
  the-third |US President> => |Thomas Jefferson>
  the-party-of the-third |US President> => |party: Democratic-Republican>
  the-dissolution-date-of the-party-of the-third |US President> => |year: 1825>

sa: dump
----------------------------------------
|context> => |context: sw console>

the-third |US President> => |Thomas Jefferson>

the-party-of |Thomas Jefferson> => |party: Democratic-Republican>

the-dissolution-date-of |party: Democratic-Republican> => |year: 1825>
----------------------------------------
ie, inching ever closer to natural language. And I think we can get even closer still!

6 comments:

  1. [sye@k5 ~/script/semantic-db/src]$ python3.4 the_semantic_db_console.py
    Welcome! 欢迎 !

    sa: |context> => |context: 中文>
    head: |context>
    tail: |context: 中文>
    pipe_min: 0
    space_min: -1
    op 1:
    indirect_label 1: |context>
    els final result: |context>
    op 2:
    indirect_label 2: |context>
    parse_rule_line learn label: |context>
    els final result: |context: 中文>
    parse_rule_line learn rule: |context: 中文>
    None

    Time taken: 2 milliseconds

    ReplyDelete
    Replies
    1. Cool! You got it to work with non-ASCII.

      BTW, now do:
      sa: dump

      and it will show you all the knowledge in the current context.

      BTW, I forgot to mention. Currently my code spits out a LOT of debugging info. So all that you posted is almost entirely debugging info.

      Delete
    2. Also, in the console there is a short-cut for defining context.

      Instead of:
      sa: |context> => |context: 中文>
      you can simply do:
      sa: context 中文

      Delete
    3. Also, try saving and then loading a sw file from the console. Probably won't work with non-ASCII. Can you try?

      sa: context 中文
      sa: chinese-for |welcome> => |欢迎>
      sa: save chinese-char-trial.sw
      sa: load chinese-char-trial.sw

      I think this might bug out.

      Delete
  2. Cool! I put it in google translate. They are "welcome" and "chinese" respectively. And based on the debugging info, seems non-ASCII is working for you. BTW, you probably have to be careful of UTF symbols that have similar meaning to \r and \n. Individual learn rules MUST be one line only. So kets must not contain \r or \n.

    ReplyDelete