From 25d73785de793102261c1fce50c9427c93eb55f0 Mon Sep 17 00:00:00 2001 From: Arthur O'Dwyer Date: Mon, 1 May 2017 13:07:19 -0700 Subject: [PATCH] Further Pythonicity. NFC. --- copycat/codeletMethods.py | 37 +++++++++++++++++++++++++++++++----- copycat/formulas.py | 28 ++++++++++++--------------- copycat/slipnet.py | 6 ------ copycat/workspaceFormulas.py | 31 ------------------------------ 4 files changed, 44 insertions(+), 58 deletions(-) diff --git a/copycat/codeletMethods.py b/copycat/codeletMethods.py index e7d6a0a..669b4db 100644 --- a/copycat/codeletMethods.py +++ b/copycat/codeletMethods.py @@ -4,14 +4,13 @@ import logging import formulas from workspaceFormulas import chooseDirectedNeighbor from workspaceFormulas import chooseNeighbor +from workspaceFormulas import chooseUnmodifiedObject from workspaceObject import WorkspaceObject from letter import Letter from replacement import Replacement from group import Group from bond import Bond from correspondence import Correspondence -from workspaceFormulas import chooseUnmodifiedObject -from workspaceFormulas import chooseBondFacet def codelet(name): @@ -234,6 +233,34 @@ def description_builder(ctx, codelet): description.build() +def __supportForDescriptionType(ctx, descriptionType, string): + workspace = ctx.workspace + described_count = 0 + total = 0 + for o in workspace.objects: + if o.string == string: + total += 1 + described_count += sum(1 for d in o.descriptions if d.descriptionType == descriptionType) + string_support = described_count / float(total) + return (descriptionType.activation + string_support) / 2 + + +def __chooseBondFacet(ctx, source, destination): + random = ctx.random + slipnet = ctx.slipnet + + # specify the descriptor types that bonds can form between + b = [ + slipnet.letterCategory, + slipnet.length, + ] + + sourceFacets = [d.descriptionType for d in source.descriptions if d.descriptionType in b] + bondFacets = [d.descriptionType for d in destination.descriptions if d.descriptionType in sourceFacets] + supports = [__supportForDescriptionType(ctx, f, source.string) for f in bondFacets] + return random.weighted_choice(bondFacets, supports) + + @codelet('bottom-up-bond-scout') def bottom_up_bond_scout(ctx, codelet): coderack = ctx.coderack @@ -244,7 +271,7 @@ def bottom_up_bond_scout(ctx, codelet): destination = chooseNeighbor(ctx, source) assert destination logging.info('destination: %s', destination) - bondFacet = chooseBondFacet(ctx, 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) @@ -384,7 +411,7 @@ def top_down_bond_scout__category(ctx, codelet): destination = chooseNeighbor(ctx, source) logging.info('source: %s, destination: %s', source, destination) assert destination - bondFacet = chooseBondFacet(ctx, source, destination) + bondFacet = __chooseBondFacet(ctx, source, destination) assert bondFacet sourceDescriptor, destinationDescriptor = __getDescriptors( bondFacet, source, destination) @@ -415,7 +442,7 @@ def top_down_bond_scout__direction(ctx, codelet): destination = chooseDirectedNeighbor(ctx, source, direction) assert destination logging.info('to object: %s', destination) - bondFacet = chooseBondFacet(ctx, source, destination) + bondFacet = __chooseBondFacet(ctx, source, destination) assert bondFacet sourceDescriptor, destinationDescriptor = __getDescriptors( bondFacet, source, destination) diff --git a/copycat/formulas.py b/copycat/formulas.py index 8793e11..8df8691 100644 --- a/copycat/formulas.py +++ b/copycat/formulas.py @@ -12,21 +12,13 @@ def weightedAverage(values): return total / totalWeights -def __relevantCategory(objekt, slipnode): - return objekt.rightBond and objekt.rightBond.category == slipnode - - -def __relevantDirection(objekt, slipnode): - return objekt.rightBond and objekt.rightBond.directionCategory == slipnode - - -def __localRelevance(string, slipnode, relevance): +def __localRelevance(string, isRelevant): numberOfObjectsNotSpanning = 0.0 numberOfMatches = 0.0 - for objekt in string.objects: - if not objekt.spansString(): + for o in string.objects: + if not o.spansString(): numberOfObjectsNotSpanning += 1.0 - if relevance(objekt, slipnode): + if isRelevant(o): numberOfMatches += 1.0 if numberOfObjectsNotSpanning == 1: return 100.0 * numberOfMatches @@ -34,13 +26,17 @@ def __localRelevance(string, slipnode, relevance): def localBondCategoryRelevance(string, category): + def isRelevant(o): + return o.rightBond and o.rightBond.category == category if len(string.objects) == 1: return 0.0 - return __localRelevance(string, category, __relevantCategory) + return __localRelevance(string, isRelevant) def localDirectionCategoryRelevance(string, direction): - return __localRelevance(string, direction, __relevantDirection) + def isRelevant(o): + return o.rightBond and o.rightBond.directionCategory == direction + return __localRelevance(string, isRelevant) def getMappings(objectFromInitial, objectFromTarget, @@ -49,8 +45,8 @@ def getMappings(objectFromInitial, objectFromTarget, for initial in initialDescriptions: for target in targetDescriptions: if initial.descriptionType == target.descriptionType: - if (initial.descriptor == target.descriptor or - initial.descriptor.slipLinked(target.descriptor)): + if (initial.descriptor == target.descriptor or + initial.descriptor.slipLinked(target.descriptor)): mapping = ConceptMapping( initial.descriptionType, target.descriptionType, diff --git a/copycat/slipnet.py b/copycat/slipnet.py index 5ed42f8..bc7801c 100644 --- a/copycat/slipnet.py +++ b/copycat/slipnet.py @@ -108,12 +108,6 @@ class Slipnet(object): self.objectCategory = self.__addNode('objectCategory', 90.0) self.bondFacet = self.__addNode('bondFacet', 90.0) - # specify the descriptor types that bonds can form between - self.bondFacets = [ - self.letterCategory, - self.length, - ] - # some factors are considered "very relevant" a priori self.initiallyClampedSlipnodes = [ self.letterCategory, diff --git a/copycat/workspaceFormulas.py b/copycat/workspaceFormulas.py index b116db2..40d991d 100644 --- a/copycat/workspaceFormulas.py +++ b/copycat/workspaceFormulas.py @@ -35,34 +35,3 @@ def chooseDirectedNeighbor(ctx, source, direction): if o.string == source.string and o.leftIndex == source.rightIndex + 1] return __chooseObjectFromList(ctx, objects, 'intraStringSalience') - - -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(ctx, f, source.string) - for f in bondFacets] - return random.weighted_choice(bondFacets, supports) - - -def __supportForDescriptionType(ctx, descriptionType, string): - string_support = __descriptionTypeSupport(ctx, descriptionType, string) - return (descriptionType.activation + string_support) / 2 - - -def __descriptionTypeSupport(ctx, descriptionType, string): - """The proportion of objects in the string with this descriptionType""" - workspace = ctx.workspace - described_count = 0 - total = 0 - for objekt in workspace.objects: - if objekt.string == string: - total += 1 - for description in objekt.descriptions: - if description.descriptionType == descriptionType: - described_count += 1 - return described_count / float(total)