def prune(self): """ Function actually do the pruning process return a sorted hypothesis list of this fragment @rtype: list of NovelDepStrHypothesis """ hypBest = self.getHypothesisByPosition(self.bestPosition) if hypBest.failed: log_error("[AllInOneCubePruner] failed to build 0,0,0 hyp !!!") stack_output = [] self.lastBestScore = 0 else: stack_output = [hypBest] self.lastBestScore = hypBest.getScore() while len(stack_output) < self.size: # expand currently best lattice self.expandEdges() if self.needStop : break # pop the best hyp of edges to output hypBest = self.popBestHypothesis() self.lastBestScore = hypBest.getScore() stack_output.append( hypBest ) stack_output.sort( key=lambda x:x.getScore() , reverse=True ) return stack_output
def expandEdges(self): """ expand edge lattices of cube save new hypothesises to self.heapEdges ------------- Expand could be done by add each dimension by 1 , of course , if it can be """ # first get all positions to expand # [pos,...] newedges = [] for idx_dimension in range(self.dimensionSize): cur_index = self.bestPosition[idx_dimension] if idx_dimension == 0: # rule dimension if cur_index+1 < len(self.stackRules): newedge = self.bestPosition[:] newedge[0] = cur_index+1 newedges.append( newedge ) else: # hyp dimensions if cur_index+1 < len(self.stacksHyps[idx_dimension-1]): newedge = self.bestPosition[:] newedge[idx_dimension] = cur_index+1 newedges.append( newedge ) # for each new edge lattice , build hypothesis for position in newedges: pastedpos = self.pastePosition(position) if pastedpos in self.positionExpanded : continue #print position hyp = self.getHypothesisByPosition(position) if hyp.failed: log_error("[AllInOneCubePruner] failed to build a hyp.") else: # !!! to prevent one direction expanding if hyp.getScore() == self.lastBestScore : continue self.mapPositionHyp[pastedpos] = hyp self.heapEdges.append( (0-hyp.getScore(),pastedpos) ) self.positionExpanded.append(pastedpos) if len(self.heapEdges) == 0: # no new hyp could be found self.needStop = True