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

View File

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