def buildStackByGlueRule(self, supportLattice1, supportLattice2): """ Build a pseudo lattice stack by given two support latice items. Output: [( "translation", [supportLattice1,supportLattice2], [COSTSUM - 10 * 6], score )] """ # Check availibility of support lattice. if supportLattice1 not in self.mapLattice or supportLattice2 not in self.mapLattice: return None ntSpan = [supportLattice1, supportLattice2] translation = "[X0] [X1]" # Check if one of the two support lattice is original site. glueRule = (translation, ntSpan, GLUECOST) simplePruner = SimpleCubePruner(self.model, [glueRule], ntSpan, self.mapLattice) stack = simplePruner.prune() # Prune with beam size. stack = stack[:self.beamSize] return stack
def buildSCFGStack(self, begin, width): """ Build stack of non lexical lattice items. """ stack = [] # Find all combinations of all support or inferred items. combs = self.findAllInferredCombinations(begin, width) for comb in combs: rulesFound, ntSpanList = self.fetchRulesForComb(comb) if not rulesFound: continue # Merge products. simplePruner = SimpleCubePruner(self.model, rulesFound, ntSpanList, self.mapLattice) hyps = simplePruner.prune() # Prune with beam size. hyps = hyps[:self.beamSize] stack.extend(hyps) # If not found, then return None if not stack: return None # Only leave highest score of same translation. mapTranslationBestScore = {} for hyp in stack: translation = hyp[0] score = hyp[-2] if (translation not in mapTranslationBestScore or score > mapTranslationBestScore[translation]): mapTranslationBestScore[translation] = score idx = 0 while idx < len(stack): hyp = stack[idx] if hyp[-2] < mapTranslationBestScore[hyp[0]]: stack.pop(idx) else: idx += 1 # Sort and prune. return self.sortStack(stack)