diff --git a/copycat/bond.py b/copycat/bond.py index 8ebe899..8df6634 100644 --- a/copycat/bond.py +++ b/copycat/bond.py @@ -5,9 +5,8 @@ class Bond(WorkspaceStructure): # pylint: disable=too-many-arguments def __init__(self, source, destination, bondCategory, bondFacet, sourceDescriptor, destinationDescriptor): - from context import context as ctx - slipnet = ctx.slipnet WorkspaceStructure.__init__(self) + slipnet = self.ctx.slipnet self.source = source self.string = self.source.string self.destination = destination @@ -30,8 +29,7 @@ class Bond(WorkspaceStructure): self.directionCategory = None def flippedVersion(self): - from context import context as ctx - slipnet = ctx.slipnet + slipnet = self.ctx.slipnet return Bond( self.destination, self.source, self.category.getRelatedNode(slipnet.opposite), @@ -45,8 +43,7 @@ class Bond(WorkspaceStructure): self.category.name, self.leftObject, self.rightObject) def buildBond(self): - from context import context as ctx - workspace = ctx.workspace + workspace = self.ctx.workspace workspace.structures += [self] self.string.bonds += [self] self.category.buffer = 100.0 @@ -61,8 +58,7 @@ class Bond(WorkspaceStructure): self.breakBond() def breakBond(self): - from context import context as ctx - workspace = ctx.workspace + workspace = self.ctx.workspace if self in workspace.structures: workspace.structures.remove(self) if self in self.string.bonds: @@ -77,8 +73,7 @@ class Bond(WorkspaceStructure): def getIncompatibleCorrespondences(self): # returns a list of correspondences that are incompatible with # self bond - from context import context as ctx - workspace = ctx.workspace + workspace = self.ctx.workspace incompatibles = [] if self.leftObject.leftmost and self.leftObject.correspondence: correspondence = self.leftObject.correspondence @@ -105,8 +100,7 @@ class Bond(WorkspaceStructure): return incompatibles def updateInternalStrength(self): - from context import context as ctx - slipnet = ctx.slipnet + slipnet = self.ctx.slipnet # bonds between objects of same type(ie. letter or group) are # stronger than bonds between different types sourceGap = self.source.leftIndex != self.source.rightIndex @@ -157,8 +151,7 @@ class Bond(WorkspaceStructure): # returns a rough measure of the density in the string # of the same bond-category and the direction-category of # the given bond - from context import context as ctx - workspace = ctx.workspace + workspace = self.ctx.workspace slotSum = 0.0 supportSum = 0.0 for object1 in workspace.objects: @@ -189,10 +182,9 @@ class Bond(WorkspaceStructure): def possibleGroupBonds(bondCategory, directionCategory, bondFacet, bonds): - from context import context as ctx - slipnet = ctx.slipnet result = [] for bond in bonds: + slipnet = bond.ctx.slipnet if (bond.category == bondCategory and bond.directionCategory == directionCategory): result += [bond] diff --git a/copycat/correspondence.py b/copycat/correspondence.py index e4702b0..b525d6a 100644 --- a/copycat/correspondence.py +++ b/copycat/correspondence.py @@ -45,8 +45,7 @@ class Correspondence(WorkspaceStructure): return initialBond def getIncompatibleBond(self): - from context import context as ctx - slipnet = ctx.slipnet + slipnet = self.ctx.slipnet initialBond = self.extract_initial_bond() if not initialBond: return None @@ -68,8 +67,7 @@ class Correspondence(WorkspaceStructure): return None def getIncompatibleCorrespondences(self): - from context import context as ctx - workspace = ctx.workspace + workspace = self.ctx.workspace return [o.correspondence for o in workspace.initial.objects if o and self.incompatible(o.correspondence)] @@ -102,8 +100,7 @@ class Correspondence(WorkspaceStructure): return False def support(self): - from context import context as ctx - workspace = ctx.workspace + workspace = self.ctx.workspace if isinstance(self.objectFromInitial, Letter): if self.objectFromInitial.spansString(): return 100.0 @@ -166,8 +163,7 @@ class Correspondence(WorkspaceStructure): return False def buildCorrespondence(self): - from context import context as ctx - workspace = ctx.workspace + workspace = self.ctx.workspace workspace.structures += [self] if self.objectFromInitial.correspondence: self.objectFromInitial.correspondence.breakCorrespondence() @@ -202,8 +198,7 @@ class Correspondence(WorkspaceStructure): self.breakCorrespondence() def breakCorrespondence(self): - from context import context as ctx - workspace = ctx.workspace + workspace = self.ctx.workspace workspace.structures.remove(self) self.objectFromInitial.correspondence = None self.objectFromTarget.correspondence = None diff --git a/copycat/description.py b/copycat/description.py index 65a2d2a..5f32521 100644 --- a/copycat/description.py +++ b/copycat/description.py @@ -15,8 +15,7 @@ class Description(WorkspaceStructure): def __str__(self): s = 'description(%s) of %s' % (self.descriptor.get_name(), self.object) - from context import context as ctx - workspace = ctx.workspace + workspace = self.ctx.workspace if self.object.string == workspace.initial: s += ' in initial string' else: @@ -31,8 +30,7 @@ class Description(WorkspaceStructure): self.descriptionType.activation) / 2 def localSupport(self): - from context import context as ctx - workspace = ctx.workspace + workspace = self.ctx.workspace described_like_self = 0 for other in workspace.objects: if self.object == other: @@ -55,8 +53,7 @@ class Description(WorkspaceStructure): self.object.descriptions += [self] def breakDescription(self): - from context import context as ctx - workspace = ctx.workspace + workspace = self.ctx.workspace if self in workspace.structures: workspace.structures.remove(self) self.object.descriptions.remove(self) diff --git a/copycat/group.py b/copycat/group.py index 96371d2..ac02d37 100644 --- a/copycat/group.py +++ b/copycat/group.py @@ -9,10 +9,9 @@ class Group(WorkspaceObject): # pylint: disable=too-many-instance-attributes def __init__(self, string, groupCategory, directionCategory, facet, objectList, bondList): - from context import context as ctx - slipnet = ctx.slipnet # pylint: disable=too-many-arguments WorkspaceObject.__init__(self, string) + slipnet = self.ctx.slipnet self.groupCategory = groupCategory self.directionCategory = directionCategory self.facet = facet @@ -42,7 +41,7 @@ class Group(WorkspaceObject): self.clampSalience = False self.name = '' - from description import Description + from description import Description # gross if self.bondList and len(self.bondList): firstFacet = self.bondList[0].facet self.addBondDescription( @@ -74,8 +73,7 @@ class Group(WorkspaceObject): def add_length_description_category(self): #check whether or not to add length description category - from context import context as ctx - slipnet = ctx.slipnet + slipnet = self.ctx.slipnet probability = self.lengthDescriptionProbability() if random.random() < probability: length = len(self.objectList) @@ -101,8 +99,7 @@ class Group(WorkspaceObject): self.bondDescriptions += [description] def singleLetterGroupProbability(self): - from context import context as ctx - slipnet = ctx.slipnet + slipnet = self.ctx.slipnet numberOfSupporters = self.numberOfLocalSupportingGroups() if not numberOfSupporters: return 0.0 @@ -115,11 +112,10 @@ class Group(WorkspaceObject): support = self.localSupport() / 100.0 activation = slipnet.length.activation / 100.0 supportedActivation = (support * activation) ** exp - return formulas.temperatureAdjustedProbability(ctx, supportedActivation) + return formulas.temperatureAdjustedProbability(self.ctx, supportedActivation) def flippedVersion(self): - from context import context as ctx - slipnet = ctx.slipnet + slipnet = self.ctx.slipnet flippedBonds = [b.flippedversion() for b in self.bondList] flippedGroup = self.groupCategory.getRelatedNode(slipnet.flipped) flippedDirection = self.directionCategory.getRelatedNode( @@ -128,8 +124,7 @@ class Group(WorkspaceObject): self.facet, self.objectList, flippedBonds) def buildGroup(self): - from context import context as ctx - workspace = ctx.workspace + workspace = self.ctx.workspace workspace.objects += [self] workspace.structures += [self] self.string.objects += [self] @@ -144,15 +139,14 @@ class Group(WorkspaceObject): description.descriptor.buffer = 100.0 def lengthDescriptionProbability(self): - from context import context as ctx - slipnet = ctx.slipnet + slipnet = self.ctx.slipnet length = len(self.objectList) if length > 5: return 0.0 cubedlength = length ** 3 fred = cubedlength * (100.0 - slipnet.length.activation) / 100.0 probability = 0.5 ** fred - value = formulas.temperatureAdjustedProbability(ctx, probability) + value = formulas.temperatureAdjustedProbability(self.ctx, probability) if value < 0.06: value = 0.0 # otherwise 1/20 chance always return value @@ -161,8 +155,7 @@ class Group(WorkspaceObject): self.breakGroup() def breakGroup(self): - from context import context as ctx - workspace = ctx.workspace + workspace = self.ctx.workspace while len(self.descriptions): description = self.descriptions[-1] description.breakDescription() @@ -184,8 +177,7 @@ class Group(WorkspaceObject): self.rightBond.breakBond() def updateInternalStrength(self): - from context import context as ctx - slipnet = ctx.slipnet + slipnet = self.ctx.slipnet relatedBondAssociation = self.groupCategory.getRelatedNode( slipnet.bondCategory).degreeOfAssociation() bondWeight = relatedBondAssociation ** 0.98 @@ -247,8 +239,7 @@ class Group(WorkspaceObject): return True def morePossibleDescriptions(self, node): - from context import context as ctx - slipnet = ctx.slipnet + slipnet = self.ctx.slipnet result = [] for i, number in enumerate(slipnet.numbers, 1): if node == number and len(self.objects) == i: diff --git a/copycat/letter.py b/copycat/letter.py index 7f4c2df..0b4fd48 100644 --- a/copycat/letter.py +++ b/copycat/letter.py @@ -3,9 +3,8 @@ from workspaceObject import WorkspaceObject class Letter(WorkspaceObject): def __init__(self, string, position, length): - from context import context as ctx - workspace = ctx.workspace WorkspaceObject.__init__(self, string) + workspace = self.ctx.workspace workspace.objects += [self] string.objects += [self] self.leftIndex = position @@ -14,8 +13,7 @@ class Letter(WorkspaceObject): self.rightmost = self.rightIndex == length def describe(self, position, length): - from context import context as ctx - slipnet = ctx.slipnet + slipnet = self.ctx.slipnet if length == 1: self.addDescription(slipnet.stringPositionCategory, slipnet.single) diff --git a/copycat/rule.py b/copycat/rule.py index 4089039..90a72b9 100644 --- a/copycat/rule.py +++ b/copycat/rule.py @@ -2,7 +2,7 @@ import logging from workspaceStructure import WorkspaceStructure -from formulas import weightedAverage +import formulas class Rule(WorkspaceStructure): @@ -24,8 +24,7 @@ class Rule(WorkspaceStructure): self.externalStrength = self.internalStrength def updateInternalStrength(self): - from context import context as ctx - workspace = ctx.workspace + workspace = self.ctx.workspace if not (self.descriptor and self.relation): self.internalStrength = 0.0 return @@ -53,7 +52,7 @@ class Rule(WorkspaceStructure): weights = ((depthDifference, 12), (averageDepth, 18), (sharedDescriptorTerm, sharedDescriptorWeight)) - self.internalStrength = weightedAverage(weights) + self.internalStrength = formulas.weightedAverage(weights) if self.internalStrength > 100.0: self.internalStrength = 100.0 @@ -81,8 +80,7 @@ class Rule(WorkspaceStructure): self.descriptor.buffer = 100.0 def incompatibleRuleCorrespondence(self, correspondence): - from context import context as ctx - workspace = ctx.workspace + workspace = self.ctx.workspace if not correspondence: return False # find changed object @@ -97,8 +95,7 @@ class Rule(WorkspaceStructure): for m in correspondence.conceptMappings) def __changeString(self, string): - from context import context as ctx - slipnet = ctx.slipnet + slipnet = self.ctx.slipnet # applies the changes to self string ie. successor if self.facet == slipnet.length: if self.relation == slipnet.predecessor: @@ -119,8 +116,7 @@ class Rule(WorkspaceStructure): return self.relation.name.lower() def buildTranslatedRule(self): - from context import context as ctx - workspace = ctx.workspace + workspace = self.ctx.workspace slippages = workspace.slippages() self.category = self.category.applySlippages(slippages) self.facet = self.facet.applySlippages(slippages) diff --git a/copycat/workspaceObject.py b/copycat/workspaceObject.py index 688a93e..c5512b9 100644 --- a/copycat/workspaceObject.py +++ b/copycat/workspaceObject.py @@ -49,8 +49,7 @@ class WorkspaceObject(WorkspaceStructure): self.descriptions += [description] def addDescriptions(self, descriptions): - from context import context as ctx - workspace = ctx.workspace + workspace = self.ctx.workspace copy = descriptions[:] # in case we add to our own descriptions for description in copy: logging.info('might add: %s', description) @@ -124,8 +123,7 @@ class WorkspaceObject(WorkspaceStructure): def getPossibleDescriptions(self, descriptionType): from group import Group # gross, TODO FIXME - from context import context as ctx - slipnet = ctx.slipnet + slipnet = self.ctx.slipnet logging.info('getting possible descriptions for %s', self) descriptions = [] for link in descriptionType.instanceLinks: @@ -170,13 +168,11 @@ class WorkspaceObject(WorkspaceStructure): return objectOnMyRightIsRightmost and objectOnMyLeftIsLeftmost def distinguishingDescriptor(self, descriptor): - from context import context as ctx - slipnet = ctx.slipnet + slipnet = self.ctx.slipnet return slipnet.isDistinguishingDescriptor(descriptor) def relevantDistinguishingDescriptors(self): - from context import context as ctx - slipnet = ctx.slipnet + slipnet = self.ctx.slipnet return [d.descriptor for d in self.relevantDescriptions() if slipnet.isDistinguishingDescriptor(d.descriptor)] diff --git a/copycat/workspaceStructure.py b/copycat/workspaceStructure.py index 2970407..3a2e76f 100644 --- a/copycat/workspaceStructure.py +++ b/copycat/workspaceStructure.py @@ -8,6 +8,8 @@ def abstract_call(objekt, name): class WorkspaceStructure(object): def __init__(self): + from context import context as ctx + self.ctx = ctx self.string = None self.internalStrength = 0.0 self.externalStrength = 0.0