Demagic workspaceFormulas.py. NFC.

This commit is contained in:
Arthur O'Dwyer
2017-04-17 22:57:33 -07:00
parent 3732ae8475
commit 30f8c623e5
2 changed files with 36 additions and 71 deletions

View File

@ -57,16 +57,10 @@ def __getScoutSource(ctx, slipnode, relevanceMethod, typeName):
else:
logging.info('initial string selected: %s for %s',
workspace.initial, typeName)
source = chooseUnmodifiedObject('intraStringSalience', string.objects)
source = chooseUnmodifiedObject(ctx, 'intraStringSalience', string.objects)
return source
def __getBondFacet(source, destination):
bondFacet = chooseBondFacet(source, destination)
assert bondFacet
return bondFacet
def __getDescriptors(bondFacet, source, destination):
sourceDescriptor = source.getDescriptor(bondFacet)
destinationDescriptor = destination.getDescriptor(bondFacet)
@ -178,7 +172,7 @@ def bottom_up_description_scout(ctx, codelet):
coderack = ctx.coderack
random = ctx.random
workspace = ctx.workspace
chosenObject = chooseUnmodifiedObject('totalSalience', workspace.objects)
chosenObject = chooseUnmodifiedObject(ctx, 'totalSalience', workspace.objects)
assert chosenObject
__showWhichStringObjectIsFrom(chosenObject)
# choose relevant description by activation
@ -202,7 +196,7 @@ def top_down_description_scout(ctx, codelet):
random = ctx.random
workspace = ctx.workspace
descriptionType = codelet.arguments[0]
chosenObject = chooseUnmodifiedObject('totalSalience', workspace.objects)
chosenObject = chooseUnmodifiedObject(ctx, 'totalSalience', workspace.objects)
assert chosenObject
__showWhichStringObjectIsFrom(chosenObject)
descriptions = chosenObject.getPossibleDescriptions(descriptionType)
@ -244,12 +238,13 @@ def bottom_up_bond_scout(ctx, codelet):
coderack = ctx.coderack
slipnet = ctx.slipnet
workspace = ctx.workspace
source = chooseUnmodifiedObject('intraStringSalience', workspace.objects)
source = chooseUnmodifiedObject(ctx, 'intraStringSalience', workspace.objects)
__showWhichStringObjectIsFrom(source)
destination = chooseNeighbor(source)
destination = chooseNeighbor(ctx, source)
assert destination
logging.info('destination: %s', destination)
bondFacet = __getBondFacet(source, destination)
bondFacet = chooseBondFacet(ctx, source, destination)
assert bondFacet
logging.info('chosen bond facet: %s', bondFacet.get_name())
logging.info('Source: %s, destination: %s', source, destination)
bond_descriptors = __getDescriptors(bondFacet, source, destination)
@ -391,10 +386,11 @@ def top_down_bond_scout__category(ctx, codelet):
category = codelet.arguments[0]
source = __getScoutSource(ctx, category, formulas.localBondCategoryRelevance,
'bond')
destination = chooseNeighbor(source)
destination = chooseNeighbor(ctx, source)
logging.info('source: %s, destination: %s', source, destination)
assert destination
bondFacet = __getBondFacet(source, destination)
bondFacet = chooseBondFacet(ctx, source, destination)
assert bondFacet
sourceDescriptor, destinationDescriptor = __getDescriptors(
bondFacet, source, destination)
forwardBond = sourceDescriptor.getBondCategory(destinationDescriptor)
@ -421,10 +417,11 @@ def top_down_bond_scout__direction(ctx, codelet):
direction = codelet.arguments[0]
source = __getScoutSource(ctx,
direction, formulas.localDirectionCategoryRelevance, 'bond')
destination = chooseDirectedNeighbor(source, direction)
destination = chooseDirectedNeighbor(ctx, source, direction)
assert destination
logging.info('to object: %s', destination)
bondFacet = __getBondFacet(source, destination)
bondFacet = chooseBondFacet(ctx, source, destination)
assert bondFacet
sourceDescriptor, destinationDescriptor = __getDescriptors(
bondFacet, source, destination)
category = sourceDescriptor.getBondCategory(destinationDescriptor)
@ -874,9 +871,9 @@ def bottom_up_correspondence_scout(ctx, codelet):
coderack = ctx.coderack
slipnet = ctx.slipnet
workspace = ctx.workspace
objectFromInitial = chooseUnmodifiedObject('interStringSalience',
objectFromInitial = chooseUnmodifiedObject(ctx, 'interStringSalience',
workspace.initial.objects)
objectFromTarget = chooseUnmodifiedObject('interStringSalience',
objectFromTarget = chooseUnmodifiedObject(ctx, 'interStringSalience',
workspace.target.objects)
assert objectFromInitial.spansString() == objectFromTarget.spansString()
# get the posible concept mappings
@ -917,7 +914,7 @@ def important_object_correspondence_scout(ctx, codelet):
slipnet = ctx.slipnet
temperature = ctx.temperature
workspace = ctx.workspace
objectFromInitial = chooseUnmodifiedObject('relativeImportance',
objectFromInitial = chooseUnmodifiedObject(ctx, 'relativeImportance',
workspace.initial.objects)
descriptors = objectFromInitial.relevantDistinguishingDescriptors()
# choose descriptor by conceptual depth
@ -934,7 +931,7 @@ def important_object_correspondence_scout(ctx, codelet):
if description.descriptor == initialDescriptor:
targetCandidates += [objekt]
assert targetCandidates
objectFromTarget = chooseUnmodifiedObject('interStringSalience',
objectFromTarget = chooseUnmodifiedObject(ctx, 'interStringSalience',
targetCandidates)
assert objectFromInitial.spansString() == objectFromTarget.spansString()
# get the posible concept mappings

View File

@ -1,5 +1,3 @@
import logging
def __chooseObjectFromList(ctx, objects, attribute):
random = ctx.random
@ -13,8 +11,7 @@ def __chooseObjectFromList(ctx, objects, attribute):
return random.weighted_choice(objects, weights)
def chooseUnmodifiedObject(attribute, inObjects):
from context import context as ctx
def chooseUnmodifiedObject(ctx, attribute, inObjects):
workspace = ctx.workspace
objects = [o for o in inObjects if o.string != workspace.modified]
if not len(objects):
@ -22,77 +19,48 @@ def chooseUnmodifiedObject(attribute, inObjects):
return __chooseObjectFromList(ctx, objects, attribute)
def chooseNeighbor(source):
from context import context as ctx
def chooseNeighbor(ctx, source):
workspace = ctx.workspace
objects = []
for objekt in workspace.objects:
if objekt.string != source.string:
continue
if objekt.leftIndex == source.rightIndex + 1:
objects += [objekt]
elif source.leftIndex == objekt.rightIndex + 1:
objects += [objekt]
objects = [o for o in workspace.objects if o.beside(source)]
return __chooseObjectFromList(ctx, objects, "intraStringSalience")
def chooseDirectedNeighbor(source, direction):
from context import context as ctx
def chooseDirectedNeighbor(ctx, source, direction):
slipnet = ctx.slipnet
workspace = ctx.workspace
if direction == slipnet.left:
logging.info('Left')
return __chooseLeftNeighbor(source)
logging.info('Right')
return __chooseRightNeighbor(source)
def __chooseLeftNeighbor(source):
from context import context as ctx
workspace = ctx.workspace
objects = []
for o in workspace.objects:
if o.string == source.string:
if source.leftIndex == o.rightIndex + 1:
logging.info('%s is on left of %s', o, source)
objects += [o]
else:
logging.info('%s is not on left of %s', o, source)
logging.info('Number of left objects: %s', len(objects))
objects = [o for o in workspace.objects
if o.string == source.string
and source.leftIndex == o.rightIndex + 1]
else:
objects = [o for o in workspace.objects
if o.string == source.string
and o.leftIndex == source.rightIndex + 1]
return __chooseObjectFromList(ctx, objects, 'intraStringSalience')
def __chooseRightNeighbor(source):
from context import context as ctx
workspace = ctx.workspace
objects = [o for o in workspace.objects
if o.string == source.string
and o.leftIndex == source.rightIndex + 1]
return __chooseObjectFromList(ctx, objects, 'intraStringSalience')
def chooseBondFacet(source, destination):
from context import context as ctx
def chooseBondFacet(ctx, source, destination):
random = ctx.random
slipnet = ctx.slipnet
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]
supports = [__supportForDescriptionType(f, source.string)
supports = [__supportForDescriptionType(ctx, f, source.string)
for f in bondFacets]
return random.weighted_choice(bondFacets, supports)
def __supportForDescriptionType(descriptionType, string):
string_support = __descriptionTypeSupport(descriptionType, string)
def __supportForDescriptionType(ctx, descriptionType, string):
string_support = __descriptionTypeSupport(ctx, descriptionType, string)
return (descriptionType.activation + string_support) / 2
def __descriptionTypeSupport(descriptionType, string):
def __descriptionTypeSupport(ctx, descriptionType, string):
"""The proportion of objects in the string with this descriptionType"""
from context import context as ctx
workspace = ctx.workspace
described_count = total = 0
described_count = 0
total = 0
for objekt in workspace.objects:
if objekt.string == string:
total += 1