Further Pythonicity. NFC.

This commit is contained in:
Arthur O'Dwyer
2017-05-01 13:07:19 -07:00
parent ceaf640147
commit 25d73785de
4 changed files with 44 additions and 58 deletions

View File

@ -4,14 +4,13 @@ import logging
import formulas import formulas
from workspaceFormulas import chooseDirectedNeighbor from workspaceFormulas import chooseDirectedNeighbor
from workspaceFormulas import chooseNeighbor from workspaceFormulas import chooseNeighbor
from workspaceFormulas import chooseUnmodifiedObject
from workspaceObject import WorkspaceObject from workspaceObject import WorkspaceObject
from letter import Letter from letter import Letter
from replacement import Replacement from replacement import Replacement
from group import Group from group import Group
from bond import Bond from bond import Bond
from correspondence import Correspondence from correspondence import Correspondence
from workspaceFormulas import chooseUnmodifiedObject
from workspaceFormulas import chooseBondFacet
def codelet(name): def codelet(name):
@ -234,6 +233,34 @@ def description_builder(ctx, codelet):
description.build() 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') @codelet('bottom-up-bond-scout')
def bottom_up_bond_scout(ctx, codelet): def bottom_up_bond_scout(ctx, codelet):
coderack = ctx.coderack coderack = ctx.coderack
@ -244,7 +271,7 @@ def bottom_up_bond_scout(ctx, codelet):
destination = chooseNeighbor(ctx, source) destination = chooseNeighbor(ctx, source)
assert destination assert destination
logging.info('destination: %s', destination) logging.info('destination: %s', destination)
bondFacet = chooseBondFacet(ctx, source, destination) bondFacet = __chooseBondFacet(ctx, source, destination)
assert bondFacet 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)
@ -384,7 +411,7 @@ def top_down_bond_scout__category(ctx, codelet):
destination = chooseNeighbor(ctx, 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 = chooseBondFacet(ctx, source, destination) bondFacet = __chooseBondFacet(ctx, source, destination)
assert bondFacet assert bondFacet
sourceDescriptor, destinationDescriptor = __getDescriptors( sourceDescriptor, destinationDescriptor = __getDescriptors(
bondFacet, source, destination) bondFacet, source, destination)
@ -415,7 +442,7 @@ def top_down_bond_scout__direction(ctx, codelet):
destination = chooseDirectedNeighbor(ctx, source, direction) destination = chooseDirectedNeighbor(ctx, source, direction)
assert destination assert destination
logging.info('to object: %s', destination) logging.info('to object: %s', destination)
bondFacet = chooseBondFacet(ctx, source, destination) bondFacet = __chooseBondFacet(ctx, source, destination)
assert bondFacet assert bondFacet
sourceDescriptor, destinationDescriptor = __getDescriptors( sourceDescriptor, destinationDescriptor = __getDescriptors(
bondFacet, source, destination) bondFacet, source, destination)

View File

@ -12,21 +12,13 @@ def weightedAverage(values):
return total / totalWeights return total / totalWeights
def __relevantCategory(objekt, slipnode): def __localRelevance(string, isRelevant):
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):
numberOfObjectsNotSpanning = 0.0 numberOfObjectsNotSpanning = 0.0
numberOfMatches = 0.0 numberOfMatches = 0.0
for objekt in string.objects: for o in string.objects:
if not objekt.spansString(): if not o.spansString():
numberOfObjectsNotSpanning += 1.0 numberOfObjectsNotSpanning += 1.0
if relevance(objekt, slipnode): if isRelevant(o):
numberOfMatches += 1.0 numberOfMatches += 1.0
if numberOfObjectsNotSpanning == 1: if numberOfObjectsNotSpanning == 1:
return 100.0 * numberOfMatches return 100.0 * numberOfMatches
@ -34,13 +26,17 @@ def __localRelevance(string, slipnode, relevance):
def localBondCategoryRelevance(string, category): def localBondCategoryRelevance(string, category):
def isRelevant(o):
return o.rightBond and o.rightBond.category == category
if len(string.objects) == 1: if len(string.objects) == 1:
return 0.0 return 0.0
return __localRelevance(string, category, __relevantCategory) return __localRelevance(string, isRelevant)
def localDirectionCategoryRelevance(string, direction): 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, def getMappings(objectFromInitial, objectFromTarget,
@ -49,8 +45,8 @@ def getMappings(objectFromInitial, objectFromTarget,
for initial in initialDescriptions: for initial in initialDescriptions:
for target in targetDescriptions: for target in targetDescriptions:
if initial.descriptionType == target.descriptionType: if initial.descriptionType == target.descriptionType:
if (initial.descriptor == target.descriptor or if (initial.descriptor == target.descriptor or
initial.descriptor.slipLinked(target.descriptor)): initial.descriptor.slipLinked(target.descriptor)):
mapping = ConceptMapping( mapping = ConceptMapping(
initial.descriptionType, initial.descriptionType,
target.descriptionType, target.descriptionType,

View File

@ -108,12 +108,6 @@ class Slipnet(object):
self.objectCategory = self.__addNode('objectCategory', 90.0) self.objectCategory = self.__addNode('objectCategory', 90.0)
self.bondFacet = self.__addNode('bondFacet', 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 # some factors are considered "very relevant" a priori
self.initiallyClampedSlipnodes = [ self.initiallyClampedSlipnodes = [
self.letterCategory, self.letterCategory,

View File

@ -35,34 +35,3 @@ def chooseDirectedNeighbor(ctx, source, direction):
if o.string == source.string if o.string == source.string
and o.leftIndex == source.rightIndex + 1] and o.leftIndex == source.rightIndex + 1]
return __chooseObjectFromList(ctx, objects, 'intraStringSalience') 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)