From feae97a98855ac76b1ebf88aaa9d57f5e82350ea Mon Sep 17 00:00:00 2001 From: J Alan Brogan Date: Fri, 26 Oct 2012 18:20:26 +0100 Subject: [PATCH] Simpler returns --- README.md | 2 ++ copycat/conceptMapping.py | 26 ++++++-------------------- 2 files changed, 8 insertions(+), 20 deletions(-) diff --git a/README.md b/README.md index cf604a0..ae157be 100644 --- a/README.md +++ b/README.md @@ -6,3 +6,5 @@ An implementation of the [Douglas Hofstadter](http://prelectur.stanford.edu/lect This implementation is a copycat of Scott Boland's [Java implementation](http://itee.uq.edu.au/~scottb/_Copycat/), but re-written into Python. It's not a direct translation - but based on his code. I did not carry over the GUI, as this version can more usefully be run from command line, or imported for use by other Python scripts. In cases where I could not grok the Java implementation easily or directly I took ideas from the [LISP implementation](http://web.cecs.pdx.edu/~mm/how-to-get-copycat.html), or directly from [Melanie Mitchell](https://en.wikipedia.org/wiki/Melanie_Mitchell)'s "[Analogy-Making as Perception](http://www.amazon.com/Analogy-Making-Perception-Computer-Melanie-Mitchell/dp/0262132893/ref=tmm_hrd_title_0?ie=UTF8&qid=1351269085&sr=1-3)" + +I also tried to make the code more pythonic. diff --git a/copycat/conceptMapping.py b/copycat/conceptMapping.py index d3045a4..4f861e2 100644 --- a/copycat/conceptMapping.py +++ b/copycat/conceptMapping.py @@ -16,9 +16,7 @@ class ConceptMapping(object): return '' % (self.__str__(), self.initialDescriptor, self.targetDescriptor) def __str__(self): - if self.label: - return self.label.name - return 'anonymous' + return self.label and self.label.name or 'anonymous' def slipability(self): association = self.__degreeOfAssociation() @@ -51,9 +49,7 @@ class ConceptMapping(object): return False if not self.initialObject.distinguishingDescriptor(self.initialDescriptor): return False - if not self.targetObject.distinguishingDescriptor(self.targetDescriptor): - return False - return True + return self.targetObject.distinguishingDescriptor(self.targetDescriptor) def sameInitialType(self, other): return self.initialDescriptionType == other.initialDescriptionType @@ -80,16 +76,10 @@ class ConceptMapping(object): return self.sameTypes(other) and self.sameInitialDescriptor(other) def isContainedBy(self, mappings): - for mapping in mappings: - if self.sameKind(mapping): - return True - return False + return any([self.sameKind(mapping) for mapping in mappings]) def isNearlyContainedBy(self, mappings): - for mapping in mappings: - if self.nearlySameKind(mapping): - return True - return False + return any([self.nearlySameKind(mapping) for mapping in mappings]) def related(self, other): if self.initialDescriptor.related(other.initialDescriptor): @@ -108,9 +98,7 @@ class ConceptMapping(object): return False if not self.label or not other.label: return False - if self.label != other.label: - return True - return False + return self.label != other.label def supports(self, other): # Concept-mappings (a -> b) and (c -> d) support each other if a is related @@ -130,9 +118,7 @@ class ConceptMapping(object): return False if not self.label or not other.label: return False - if self.label == other.label: - return True - return False + return self.label == other.label def relevant(self): return self.initialDescriptionType.fully_active() and self.targetDescriptionType.fully_active()