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): try: op,thresh,destination = parameters.split(',') thresh = float(thresh) destination = ket(destination) except: return ket("",0) one = context.relevant_kets(op) # one is a superposition print("one:",one) 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 else: out_list[idx] += out_list[i] del_list.append(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. else: out_list = [x for index,x in enumerate(out_list) if index not in del_list] for k, sp in enumerate(out_list): print("sp:",sp) context.learn("category-" + str(k),destination,sp) return ket("categorize")And that's it!
No comments:
Post a Comment