a bko version of categorize

Now, let's give the python for the BKO version of categorize. I'll give an example usage in the next post.

Here is the python:
# one is a superposition
# op is a string
# x is a ket
# thresh is a float
def simm_mbr(context,op,x,thresh,one):
  f = x.apply_op(context,op)
  for elt in one:
    g = elt.apply_op(context,op)
    if silent_simm(f,g) >= thresh:
      return True
  return False
# categorize[op,thresh,destination]
def categorize(context,parameters):
    op,thresh,destination = parameters.split(',')
    thresh = float(thresh)
    destination = ket(destination)
    return ket("",0)
  one = context.relevant_kets(op)                 # one is a superposition
  out_list = []                                   # out_list will be a list of superpositions.
  for x in one:                                   # x is of course a ket
    n = 0
    del_list = []                                 # del_list will be a list of integers.
    for i in range(len(out_list)):
      if simm_mbr(context,op,x,thresh,out_list[i]):
        if n == 0:
          out_list[i] += x
          idx = i
          n = 1
          out_list[idx] += out_list[i]
    if n == 0:
      out_list.append(superposition() + x)        # we use "superposition() + x" instead of just "x" so out_list is always a list of superpositions, not kets.
      out_list = [x for index,x in enumerate(out_list) if index not in del_list]

  for k, sp in enumerate(out_list):
    context.learn("category-" + str(k),destination,sp)  
  return ket("categorize")
And that's it!

previous: an implementation of categorize code
next: some categorize examples

updated: 19/12/2016
by Garry Morrison
email: garry -at- semantic-db.org