Here is the python:
def list_2_sp(one): r = superposition() if type(one) == list: for x in one: # what do we want to do if type(x) is not int, float or string? if type(x) == int or type(x) == float: r += ket("number: " + str(x)) elif type(x) == str: r += ket(x) return rAnd a quick demonstration of it in action:
# define some example lists: list1 = [2,3,5,7,11,13] list2 = ["cat","dog","horse","rat","mouse","lion","horse"] list3 = ["a","b",37,2.1828,"a","fish","a",37] # test learn code: context.learn("list-of","small primes",list1) context.learn("list-of","common animals",list2) context.learn("list-of","test elements",list3) # see what we have learnt: context.print_universe()Outputs (NB: the repeated elements with coeff > 1):
list-of |small primes> => |number: 2> + |number: 3> + |number: 5> + |number: 7> + |number: 11> + |number: 13> list-of |common animals> => |cat> + |dog> + 2|horse> + |rat> + |mouse> + |lion> list-of |test elements> => 3|a> + |b> + 2|number: 37> + |number: 2.1828> + |fish>So, simple enough. Just makes using the python context.learn() a little cleaner. eg, instead of:
context.learn("friends","Fred",ket("Sam") + ket("Frank") + ket("Jim") + ket("Rob"))we can now do:
context.learn("friends","Fred",["Sam","Frank","Jim","Rob"])Of course, if you need your kets to have coeff other than 1, then you need to do it the old way, with the explicit ket(string,value). Though I suspect most of the time we can get away with just using lists.
No comments:
Post a Comment