예제 #1
0
def rule_scout(codelet):
    assert workspace.numberOfUnreplacedObjects() == 0
    changedObjects = [o for o in workspace.initial.objects if o.changed]
    #assert len(changedObjects) < 2
    # if there are no changed objects, propose a rule with no changes
    if not changedObjects:
        return coderack.proposeRule(None, None, None, None, codelet)

    changed = changedObjects[-1]
    # generate a list of distinguishing descriptions for the first object
    # ie. string-position (left-,right-most,middle or whole) or letter category
    # if it is the only one of its type in the string
    objectList = []
    position = changed.getDescriptor(slipnet.stringPositionCategory)
    if position:
        objectList += [position]
    letter = changed.getDescriptor(slipnet.letterCategory)
    otherObjectsOfSameLetter = [o for o in workspace.initial.objects
                                if not o != changed
                                and o.getDescriptionType(letter)]
    if not len(otherObjectsOfSameLetter):
        objectList += [letter]
    # if this object corresponds to another object in the workspace
    # objectList = the union of this and the distingushing descriptors
    if changed.correspondence:
        targetObject = changed.correspondence.objectFromTarget
        newList = []
        slippages = workspace.slippages()
        for node in objectList:
            node = node.applySlippages(slippages)
            if targetObject.described(node):
                if targetObject.distinguishingDescriptor(node):
                    newList += [node]
        objectList = newList  # surely this should be +=
                              # "union of this and distinguishing descriptors"
    assert objectList and len(objectList)
    # use conceptual depth to choose a description
    valueList = []
    for node in objectList:
        depth = node.conceptualDepth
        value = formulas.temperatureAdjustedValue(depth)
        valueList += [value]
    i = formulas.selectListPosition(valueList)
    descriptor = objectList[i]
    # choose the relation (change the letmost object to "successor" or "d"
    objectList = []
    if changed.replacement.relation:
        objectList += [changed.replacement.relation]
    objectList += [changed.replacement.objectFromModified.getDescriptor(
        slipnet.letterCategory)]
    # use conceptual depth to choose a relation
    valueList = []
    for node in objectList:
        depth = node.conceptualDepth
        value = formulas.temperatureAdjustedValue(depth)
        valueList += [value]
    i = formulas.selectListPosition(valueList)
    relation = objectList[i]
    coderack.proposeRule(slipnet.letterCategory, descriptor,
                         slipnet.letter, relation, codelet)
예제 #2
0
def rule_scout(codelet):
    assert workspace.numberOfUnreplacedObjects() == 0
    changedObjects = [o for o in workspace.initial.objects if o.changed]
    # assert len(changedObjects) < 2
    # if there are no changed objects, propose a rule with no changes
    if not changedObjects:
        return coderack.proposeRule(None, None, None, None, codelet)

    changed = changedObjects[-1]
    # generate a list of distinguishing descriptions for the first object
    # ie. string-position (left-,right-most,middle or whole) or letter category
    # if it is the only one of its type in the string
    objectList = []
    position = changed.getDescriptor(slipnet.stringPositionCategory)
    if position:
        objectList += [position]
    letter = changed.getDescriptor(slipnet.letterCategory)
    otherObjectsOfSameLetter = [o for o in workspace.initial.objects
                                if not o != changed and
                                o.getDescriptionType(letter)]
    if not len(otherObjectsOfSameLetter):
        objectList += [letter]
    # if this object corresponds to another object in the workspace
    # objectList = the union of this and the distingushing descriptors
    if changed.correspondence:
        targetObject = changed.correspondence.objectFromTarget
        newList = []
        slippages = workspace.slippages()
        for node in objectList:
            node = node.applySlippages(slippages)
            if targetObject.described(node):
                if targetObject.distinguishingDescriptor(node):
                    newList += [node]
        objectList = newList  # should this be += ??
    assert objectList
    # use conceptual depth to choose a description
    valueList = []
    for node in objectList:
        depth = node.conceptualDepth
        value = formulas.temperatureAdjustedValue(depth)
        valueList += [value]
    i = formulas.selectListPosition(valueList)
    descriptor = objectList[i]
    # choose the relation (change the letmost object to "successor" or "d"
    objectList = []
    if changed.replacement.relation:
        objectList += [changed.replacement.relation]
    objectList += [changed.replacement.objectFromModified.getDescriptor(
        slipnet.letterCategory)]
    # use conceptual depth to choose a relation
    valueList = []
    for node in objectList:
        depth = node.conceptualDepth
        value = formulas.temperatureAdjustedValue(depth)
        valueList += [value]
    i = formulas.selectListPosition(valueList)
    relation = objectList[i]
    coderack.proposeRule(slipnet.letterCategory, descriptor,
                         slipnet.letter, relation, codelet)
예제 #3
0
def chooseBondFacet(source, destination):
    sourceFacets = [d.descriptionType for d in source.descriptions if d.descriptionType in slipnet.bondFacets]
    bondFacets = [d.descriptionType for d in destination.descriptions if d.descriptionType in sourceFacets]
    if not bondFacets:
        return None
    supports = [__supportForDescriptionType(f, source.string) for f in bondFacets]
    i = formulas.selectListPosition(supports)
    return bondFacets[i]
예제 #4
0
def top_down_description_scout(codelet):
    descriptionType = codelet.arguments[0]
    chosenObject = chooseUnmodifiedObject('totalSalience', workspace.objects)
    assert chosenObject
    __showWhichStringObjectIsFrom(chosenObject)
    descriptions = chosenObject.getPossibleDescriptions(descriptionType)
    assert descriptions
    values = [n.activation for n in descriptions]
    i = formulas.selectListPosition(values)
    chosenProperty = descriptions[i]
    coderack.proposeDescription(chosenObject, chosenProperty.category(),
                                chosenProperty, codelet)
예제 #5
0
def bottom_up_description_scout(codelet):
    chosenObject = chooseUnmodifiedObject('totalSalience', workspace.objects)
    assert chosenObject
    __showWhichStringObjectIsFrom(chosenObject)
    description = formulas.chooseRelevantDescriptionByActivation(chosenObject)
    assert description
    sliplinks = formulas.similarPropertyLinks(description.descriptor)
    assert sliplinks
    values = [sliplink.degreeOfAssociation() * sliplink.destination.activation
              for sliplink in sliplinks]
    i = formulas.selectListPosition(values)
    chosen = sliplinks[i]
    chosenProperty = chosen.destination
    coderack.proposeDescription(chosenObject, chosenProperty.category(),
                                chosenProperty, codelet)
예제 #6
0
def chooseBondFacet(source, destination):
    sourceFacets = [
        d.descriptionType for d in source.descriptions
        if d.descriptionType in slipnet.bondFacets
    ]
    bondFacets = [
        d.descriptionType for d in destination.descriptions
        if d.descriptionType in sourceFacets
    ]
    if not bondFacets:
        return None
    supports = [
        __supportForDescriptionType(f, source.string) for f in bondFacets
    ]
    i = formulas.selectListPosition(supports)
    return bondFacets[i]
예제 #7
0
def top_down_group_scout__direction(codelet):
    direction = codelet.arguments[0]
    source = __getScoutSource(direction,
                              formulas.localDirectionCategoryRelevance,
                              'direction')
    logging.info('source chosen = %s', source)
    assert not source.spansString()
    if source.leftmost:
        mydirection = slipnet.right
    elif source.rightmost:
        mydirection = slipnet.left
    else:
        activations = [slipnet.left.activation]
        activations += [slipnet.right.activation]
        if not formulas.selectListPosition(activations):
            mydirection = slipnet.left
        else:
            mydirection = slipnet.right
    if mydirection == slipnet.left:
        firstBond = source.leftBond
    else:
        firstBond = source.rightBond
    if not firstBond:
        logging.info('no firstBond')
    else:
        logging.info('firstBond: %s', firstBond)
    if firstBond and not firstBond.directionCategory:
        direction = None
    if not firstBond or firstBond.directionCategory != direction:
        if mydirection == slipnet.right:
            firstBond = source.leftBond
        else:
            firstBond = source.rightBond
        if not firstBond:
            logging.info('no firstBond2')
        else:
            logging.info('firstBond2: %s', firstBond)
        if firstBond and not firstBond.directionCategory:
            direction = None
        assert firstBond
        assert firstBond.directionCategory == direction
    logging.info('possible group: %s', firstBond)
    category = firstBond.category
    assert category
    groupCategory = category.getRelatedNode(slipnet.groupCategory)
    logging.info('trying from %s to %s', source, category.name)
    bondFacet = None
    # find leftmost object in group with these bonds
    search = True
    while search:
        search = False
        if not source.leftBond:
            continue
        if source.leftBond.category != category:
            continue
        if source.leftBond.directionCategory != direction:
            if source.leftBond.directionCategory:
                continue
        if not bondFacet or bondFacet == source.leftBond.facet:
            bondFacet = source.leftBond.facet
            direction = source.leftBond.directionCategory
            source = source.leftBond.leftObject
            search = True
    destination = source
    search = True
    while search:
        search = False
        if not destination.rightBond:
            continue
        if destination.rightBond.category != category:
            continue
        if destination.rightBond.directionCategory != direction:
            if destination.rightBond.directionCategory:
                continue
        if not bondFacet or bondFacet == destination.rightBond.facet:
            bondFacet = destination.rightBond.facet
            direction = source.rightBond.directionCategory
            destination = destination.rightBond.rightObject
            search = True
    assert destination != source
    logging.info('proposing group from %s to %s', source, destination)
    objects = [source]
    bonds = []
    while source != destination:
        bonds += [source.rightBond]
        objects += [source.rightBond.rightObject]
        source = source.rightBond.rightObject
    coderack.proposeGroup(objects, bonds, groupCategory,
                          direction, bondFacet, codelet)
예제 #8
0
def top_down_group_scout__category(codelet):
    groupCategory = codelet.arguments[0]
    category = groupCategory.getRelatedNode(slipnet.bondCategory)
    assert category
    source = __getScoutSource(category, formulas.localBondCategoryRelevance,
                              'group')
    assert source
    assert not source.spansString()
    if source.leftmost:
        direction = slipnet.right
    elif source.rightmost:
        direction = slipnet.left
    else:
        activations = [slipnet.left.activation]
        activations += [slipnet.right.activation]
        if not formulas.selectListPosition(activations):
            direction = slipnet.left
        else:
            direction = slipnet.right
    if direction == slipnet.left:
        firstBond = source.leftBond
    else:
        firstBond = source.rightBond
    if not firstBond or firstBond.category != category:
        # check the other side of object
        if direction == slipnet.right:
            firstBond = source.leftBond
        else:
            firstBond = source.rightBond
        if not firstBond or firstBond.category != category:
            if category == slipnet.sameness and isinstance(source, Letter):
                group = Group(source.string, slipnet.samenessGroup,
                              None, slipnet.letterCategory, [source], [])
                probability = group.singleLetterGroupProbability()
                assert random.random() >= probability
                coderack.proposeSingleLetterGroup(source, codelet)
        return
    direction = firstBond.directionCategory
    search = True
    bondFacet = None
    # find leftmost object in group with these bonds
    while search:
        search = False
        if not source.leftBond:
            continue
        if source.leftBond.category != category:
            continue
        if source.leftBond.directionCategory != direction:
            if source.leftBond.directionCategory:
                continue
        if not bondFacet or bondFacet == source.leftBond.facet:
            bondFacet = source.leftBond.facet
            direction = source.leftBond.directionCategory
            source = source.leftBond.leftObject
            search = True
    # find rightmost object in group with these bonds
    search = True
    destination = source
    while search:
        search = False
        if not destination.rightBond:
            continue
        if destination.rightBond.category != category:
            continue
        if destination.rightBond.directionCategory != direction:
            if destination.rightBond.directionCategory:
                continue
        if not bondFacet or bondFacet == destination.rightBond.facet:
            bondFacet = destination.rightBond.facet
            direction = source.rightBond.directionCategory
            destination = destination.rightBond.rightObject
            search = True
    assert destination != source
    objects = [source]
    bonds = []
    while source != destination:
        bonds += [source.rightBond]
        objects += [source.rightBond.rightObject]
        source = source.rightBond.rightObject
    coderack.proposeGroup(objects, bonds, groupCategory,
                          direction, bondFacet, codelet)