Example #1
0
def makeAnalog(analog, memory):
    # you want to keep track of all the Ps, RBs, and POs you've made in this analog so that you can reuse POs among the propositions in the analog (e.g., use the same John in loves(John, Mary) and loves(Mary, John)).
    # make a new analog object and add it to memory.analogs (but make sure the analog isn't empty).
    if len(analog) > 0:
        new_analog = dataTypes.Analog()
        memory.analogs.append(new_analog)
    # find the current analog.
    # iterate through each element of the analog, which is a proposition:
    for prop in analog:
        # create a group unit if you should.
        ######################## HERE ########################

        # create the P unit if you should (i.e., if prop['name'] != 'non_exist').
        if prop['name'] != 'non_exist':
            newP = dataTypes.PUnit(prop['name'], prop['set'], prop['analog'], False, new_analog)
            # if the newP does exist in the current analog, set newP = to the P already in currentPs to which newP should correspond (e.g., if the newP is LJM, and LJM is the 3rd P in currentPs, set newP = currentP[2]). Otherwise, put the newP in the currentPs list.
            add_new_P = True
            for myP in memory.Ps:
                if (newP.set == myP.set)  and (newP.myanalog == myP.myanalog):
                    if newP.name == myP.name: # if the P already exists, then set newP to P, and set add_new_P to False.
                        newP = myP
                        add_new_P = False
                        break
            if add_new_P:
                # add the P to the memory and the new_analog object.
                memory.Ps.append(newP)
                new_analog.myPs.append(newP)
        else:
            newP = 'non_exist'
        # now make the new RBs.
        # for each RB in prop, create the RB, hook it up to it's parent P, make it's pred (and pred semantics) and object (and object semantics), hook it up to it's pred and object (and if it is higher order, it's child P).
        for myRB in prop['RBs']:
            # make the myRB. First check if you should make the RB (i.e., if RB['pred_name] != 'non_exist').
            if myRB['pred_name'] != 'non_exist':
                # get the RB's name.
                if myRB['higher_order']:
                    RB_name = myRB['pred_name']+myRB['P']
                else:
                    RB_name = myRB['pred_name']+myRB['object_name']
                # make the myRB.
                newRB = dataTypes.RBUnit(RB_name, prop['set'], prop['analog'], False, new_analog)
                # put the new RB in memory and in the new_analog.
                memory.RBs.append(newRB)
                new_analog.myRBs.append(newRB)
                # hook up the RB to the newP and vise versa if there is a newP.
                if newP !='non_exist':
                    newRB.myParentPs.append(newP)
                    newP.myRBs.append(newRB)
            else:
                newRB = 'non_exist'
            # make the RB's pred and object.
            # if newRB != 'non_exist', then make the pred.
            if newRB != 'non_exist':
                # make the pred.
                newPred = dataTypes.POUnit(myRB['pred_name'], prop['set'], prop['analog'], False, new_analog, 1)
                # check to make sure the pred doesn't already exist in the currentPreds.
                add_new_pred = True
                for pred in memory.POs:
                    if (newPred.set == pred.set) and (newPred.myanalog == pred.myanalog):
                        if (newPred.name == pred.name) and (pred.predOrObj == 1): # if that pred already exists and is a pred (i.e., pred.predOrObj == 1), then set newPred = pred, and set add_new_pred to False.
                            newPred = pred
                            add_new_pred = False
                            break
                if add_new_pred and newPred != 'non_exist':
                    # else, if the newPred is actually new and has been made (i.e., the newPred != 'non_exist'), add it to currentPreds and make its semantics.
                    # add the new Pred to memory and to the new_analog.
                    memory.POs.append(newPred)
                    new_analog.myPOs.append(newPred)
                    # make the newPred's semantics.
                    # make sure that no semantics are repeated in the list of pred_sem (i.e., the same semantic should not be listed twice).
                    # NOTE: you could also accomplisth the loop below with list(set(RB['pred_sem'])), but that would not maintain order. While order of the items does not functionally matter here, I've chosen to use a method that maintains order here.
                    pred_sem_list = []
                    for item in myRB['pred_sem']:
                        if item not in pred_sem_list:
                            pred_sem_list.append(item)
                    for semantic in pred_sem_list:
                        # check if the semantic should be made (i.e., it doesn't already exist).
                        makeNewSem = True
                        # is the semantic in a list or not?
                        if (type(semantic) is list):
                            for oldsemantic in memory.semantics:
                                if oldsemantic.name == semantic[0]:
                                    makeNewSem = False
                                    newSem = oldsemantic
                                    break
                        else:
                            for oldsemantic in memory.semantics:
                                if oldsemantic.name == semantic:
                                    makeNewSem = False
                                    newSem = oldsemantic
                                    break
                        # if makeNewSem is True, then make the new semantic unit and add it to memory.semantics, otherwise, connect the newSem (which has already been set directly above to the value of the semantic in memory.semantics that the newPred should be connected to) to newPred.
                        if makeNewSem:
                            # create the new semantic and the newLink.
                            # check wheter semantic codes a dimension or not.
                            if (type(semantic) is list) and (len(semantic) > 4):
                                newSem = dataTypes.Semantic(semantic[0], semantic[2], semantic[3], semantic[4])
                                newLink = dataTypes.Link(newPred, [], newSem, semantic[1])
                            elif (type(semantic) is list) and (len(semantic) > 2):
                                newSem = dataTypes.Semantic(semantic[0], semantic[2], semantic[3])
                                newLink = dataTypes.Link(newPred, [], newSem, semantic[1])
                            elif type(semantic) is list:
                                newSem = dataTypes.Semantic(semantic[0])
                                newLink = dataTypes.Link(newPred, [], newSem, semantic[1])
                            else:
                                # default to a semantic with a weight of 1. 
                                newSem = dataTypes.Semantic(semantic)
                                newLink = dataTypes.Link(newPred, [], newSem, 1)
                            # add newLink to newSem and newPred, and add newLink to currentLinks, and newSem to memory.semantics.
                            newSem.myPOs.append(newLink)
                            newPred.mySemantics.append(newLink)
                            memory.Links.append(newLink)
                            memory.semantics.append(newSem)
                        else:
                            # create the newLink. 
                            # if semantic is a list, then use semantic[1] for the weight, else default to a weight of 1. 
                            if type(semantic) is list:
                                newLink = dataTypes.Link(newPred, [], newSem, semantic[1])
                            else:
                                newLink = dataTypes.Link(newPred, [], newSem, 1)
                            # add newLink to newSem and newPred, and add newLink to currentLinks, (don't need to add newSem to memory.semantics because it is already there (remember that makeNewSem == False)).
                            newSem.myPOs.append(newLink)
                            newPred.mySemantics.append(newLink)
                            memory.Links.append(newLink)
                # hook the newPred to the newRB and viseversa.
                newPred.myRBs.append(newRB)
                newRB.myPred.append(newPred)
            # if the RB is not higher-order, make the newobject.
            if not myRB['higher_order']:
                newObject = dataTypes.POUnit(myRB['object_name'], prop['set'], prop['analog'], False, new_analog, 0)
                # check to make sure the newObject doesn't already exist in the currentObjects.
                make_new_obj = True
                for obj in memory.POs:
                    if (newObject.set == obj.set) and (newObject.myanalog == obj.myanalog):
                        if (newObject.name == obj.name) and (obj.predOrObj == 0): # if the object already exists and is an object (i.e., newObject.predOrObj == 0), then set newObject to obj, and make_new_obj to False
                            newObject = obj
                            make_new_obj = False
                            break
                if make_new_obj:
                    # else, the newObject is actually new, add it to memory and the new_analog and make its semantics.
                    new_analog.myPOs.append(newObject)
                    memory.POs.append(newObject)
                    # make the newobject's semantics.
                    # make sure that no semantics are repeated in the list of object_sem (i.e., the same semantic should not be listed twice).
                    # NOTE: you could also accomplisth the loop below with list(set(RB['object_sem'])), but that would not maintain order. While order of the items does not functionally matter here, I've chosen to use a method that maintains order here.
                    obj_sem_list = []
                    for item in myRB['object_sem']:
                        if item not in obj_sem_list:
                            obj_sem_list.append(item)
                    for semantic in obj_sem_list:
                        # check if the semantic should be made (i.e., it doesn't already exist).
                        makeNewSem = True
                        # is the semantic in a list or not?
                        if (type(semantic) is list):
                            for oldsemantic in memory.semantics:
                                if oldsemantic.name == semantic[0]:
                                    makeNewSem = False
                                    newSem = oldsemantic
                                    break
                        else:
                            for oldsemantic in memory.semantics:
                                if oldsemantic.name == semantic:
                                    makeNewSem = False
                                    newSem = oldsemantic
                                    break
                        # if makeNewSem is True, then make the new semantic unit and add it to memory.semantics, otherwise, connect the newSem (which has already been set directly above to the value of the semantic in memory.semantics that the newPred should be connected to) to newObject.
                        if makeNewSem:
                            # create the new semantic and the newLink.
                            # check wheter semantic codes a dimension or not.
                            if (type(semantic) is list) and (len(semantic) > 4):
                                newSem = dataTypes.Semantic(semantic[0], semantic[2], semantic[3], semantic[4])
                                newLink = dataTypes.Link(newObject, [], newSem, semantic[1])
                            elif (type(semantic) is list) and (len(semantic) > 2):
                                newSem = dataTypes.Semantic(semantic[0], semantic[2], semantic[3])
                                newLink = dataTypes.Link(newObject, [], newSem, semantic[1])
                            elif type(semantic) is list:
                                newSem = dataTypes.Semantic(semantic[0])
                                newLink = dataTypes.Link(newObject, [], newSem, semantic[1])
                            else:
                                # default to a semantic with a weight of 1. 
                                newSem = dataTypes.Semantic(semantic)
                                newLink = dataTypes.Link(newObject, [], newSem, 1)
                            # add newLink to newSem and newPred, and add newLink to currentLinks, and newSem to memory.semantics.
                            newSem.myPOs.append(newLink)
                            newObject.mySemantics.append(newLink)
                            memory.Links.append(newLink)
                            memory.semantics.append(newSem)
                        else:
                            # create the newLink. 
                            # if semantic is a list, then use semantic[1] for the weight, else default to a weight of 1. 
                            if type(semantic) is list:
                                newLink = dataTypes.Link(newObject, [], newSem, semantic[1])
                            else:
                                newLink = dataTypes.Link(newObject, [], newSem, 1)
                            # add newLink to newSem and newObject, and add newLink to currentLinks, (don't need to add newSem to memory.semantics because it is already there (remember that makeNewSem == False)).
                            newSem.myPOs.append(newLink)
                            newObject.mySemantics.append(newLink)
                            memory.Links.append(newLink)
                # hook the newObject up to the new RB and viseversa (if you have actually made a newRB; i.e., newRB != 'non_exist').
                if newRB != 'non_exist':
                    newObject.myRBs.append(newRB)
                    newRB.myObj.append(newObject)
                # hook up the pred and object in the same_RB_POs field of both pred and obj if there is a newPred also.
                if newRB != 'non_exist': # if newRB != 'non_exist' it means that a new pred has been made.
                    newPred.same_RB_POs.append(newObject)
                    newObject.same_RB_POs.append(newPred)
            elif myRB['higher_order']:
                # connect the RB to it's child P and viseversa.
                # child P should already have been made. It should be the P in the myRB.Pth place .
                # if the RB takes a child P unit, then you must find the P unit that connects to the current myRB. It will be the last P unit you made with the name RB['P'].
                foundChildP = False
                for myP in reversed(memory.Ps):
                    if myP.name == myRB['P']:
                        # connect the current RB to that P and vise versa.
                        newRB.myChildP.append(P)
                        myP.myParentRBs.append(newRB)
                        foundChildP = True
                        break
                if not foundChildP:
                    print('You are trying to create a higher-order proposition with a child P that does not exist. Please check your sym file.')
    # now make sure all the weights in all the links are represented as floats. 
    for Link in memory.Links:
        Link.weight = float(Link.weight)
    # done.
    return memory
Example #2
0
def make_new_prop(obj_analog, objs, preds, memory):
    # make copies of the pred tokens.
    new_preds = []
    for pred in preds:
        # make a new PO.
        name = 'po_' + str(random.random())
        new_pred = dataTypes.POUnit(name, 'memory', obj_analog, False,
                                    obj_analog, 1)
        # make new links for all semantics and add links to memory.
        for link in pred.mySemantics:
            # make a new link.
            new_link = dataTypes.Link(new_pred, None, link.mySemantic,
                                      link.weight)
            # add the link to the PO.
            new_pred.mySemantics.append(new_link)
            # add the link to the semantic.
            link.mySemantic.myPOs.append(new_link)
            # add new_link to memory.
            memory.Links.append(new_link)
        # add pred to memory.
        memory.POs.append(new_pred)
        # add pred to new_preds.
        new_preds.append(new_pred)
    # apply preds to objects.
    # check if you need a P unit (preds share a P), and if so make one and add to memory and obj_analog.
    need_P = check_need_P(preds)
    # check if you need an RB unit (pred) has an RB.
    need_RB = check_need_RB(preds)
    if need_P == True:
        new_P = dataTypes.PUnit('new_P', 'memory', obj_analog, False,
                                obj_analog)
        memory.Ps.append(new_P)
        obj_analog.myPs.append(new_P)
        # do predicate appliction.
        for i in range(len(new_preds)):
            # make the RB.
            name = 'RB_' + str(random.random())
            new_RB = dataTypes.RBUnit(name, 'memory', obj_analog, False,
                                      obj_analog)
            # add pred and obj to RB.
            new_RB.myPred.append(new_preds[i])
            new_RB.myObj.append(objs[i])
            # add RB to pred and obj.
            new_preds[i].myRBs.append(new_RB)
            objs[i].myRBs.append(new_RB)
            # add RB to memory and analog.
            memory.RBs.append(new_RB)
            obj_analog.myRBs.append(new_RB)
            # add new_RB to P unit.
            new_P.myRBs.append(new_RB)
            new_RB.myParentPs.append(new_P)
    elif need_RB:
        for i in range(len(new_preds)):
            # make the RB.
            name = 'RB_' + str(random.random())
            new_RB = dataTypes.RBUnit(name, 'memory', obj_analog, False,
                                      obj_analog)
            # add pred and obj to RB.
            new_RB.myPred.append(new_preds[i])
            new_RB.myObj.append(objs[i])
            # add RB to pred and obj.
            new_preds[i].myRBs.append(new_RB)
            objs[i].myRBs.append(new_RB)
            # add RB to memory and analog.
            memory.RBs.append(new_RB)
            obj_analog.myRBs.append(new_RB)
    # add all new_preds to obj_analog.
    for pred in new_preds:
        obj_analog.myPOs.append(pred)
    # returns.
    return obj_analog, memory