def group_scout__whole_string(codelet): string = workspace.initial if random.random() > 0.5: string = workspace.target logging.info('target string selected: %s' % workspace.target) else: logging.info('initial string selected: %s' % workspace.initial) # find leftmost object & the highest group to which it belongs leftmost = None for objekt in string.objects: if objekt.leftmost: leftmost = objekt while leftmost.group and leftmost.group.bondCategory == slipnet.sameness: leftmost = leftmost.group if leftmost.spansString(): # the object already spans the string - propose this object group = leftmost coderack.proposeGroup(group.objectList, group.bondList, group.groupCategory, group.directionCategory, group.facet, codelet) return bonds = [] objects = [leftmost] while leftmost.rightBond: bonds += [leftmost.rightBond] leftmost = leftmost.rightBond.rightObject objects += [leftmost] assert leftmost.rightmost # choose a random bond from list chosenBond = random.choice(bonds) category = chosenBond.category directionCategory = chosenBond.directionCategory bondFacet = chosenBond.facet bonds = possibleGroupBonds(category, directionCategory, bondFacet, bonds) assert bonds groupCategory = category.getRelatedNode(slipnet.groupCategory) coderack.proposeGroup(objects, bonds, groupCategory, directionCategory, bondFacet, codelet)
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)
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)