Exemple #1
0
    def printTrace(self, cell_type, src_sent):
        '''Prints the trace for top entries (as defined by settings.opts.trace_rules) in the cell (for debugging)'''

        traceFile = settings.opts.outFile + ".trace"
        tF = open(traceFile, 'a')

        nbest_cnt = 0
        hypTraceStack = []
        tgt_key = self.calcCandScore(cell_type)
        for entry in self.table[tgt_key][:]:

            tF.write("TRACE_BEGIN\n")
            hypTraceStack.append(entry)
            tF.write( "#Input  :: %s\n" % (src_sent) )
            tF.write( "#Output :: %s ||| %s\n" % (Hypothesis.getHypothesis(entry), Hypothesis.getFeatVec(entry)) )

            while ( hypTraceStack ):
                trace_entry = hypTraceStack.pop(0)
                for back_pointer in trace_entry.bp:
                    hypTraceStack.insert(0, back_pointer)
                inf_entry = trace_entry.inf_entry
                if inf_entry is not None:   # Non-leaf nodes in derivation
                    tF.write( "%s ||| %s ||| %s ||| %s\n" % ( inf_entry.src, Hypothesis.getHypothesis(inf_entry), Hypothesis.getFeatVec(inf_entry), trace_entry.inf_cell ) )
                else:                       # Leaf nodes in derivation
                    tF.write( "%s ||| %s ||| %s ||| %s\n" % ( trace_entry.src, Hypothesis.getHypothesis(trace_entry), Hypothesis.getFeatVec(trace_entry), trace_entry.inf_cell ) )

            tF.write("TRACE_END\n")
            nbest_cnt += 1
            del hypTraceStack[:]
            if nbest_cnt == settings.opts.trace_rules: break
        tF.close()
Exemple #2
0
    def printTrace(self, cell_type, src_sent):
        '''Prints the trace for top entries (as defined by settings.opts.trace_rules) in the cell (for debugging)'''

        traceFile = settings.opts.outFile + ".trace"
        tF = open(traceFile, 'a')

        nbest_cnt = 0
        hypTraceStack = []
        tgt_key = self.calcCandScore(cell_type)
        for entry in self.table[tgt_key][:]:

            tF.write("TRACE_BEGIN\n")
            hypTraceStack.append(entry)
            tF.write("#Input  :: %s\n" % (src_sent))
            tF.write("#Output :: %s ||| %s\n" %
                     (Hypothesis.getHypothesis(entry),
                      Hypothesis.getFeatVec(entry)))

            while (hypTraceStack):
                trace_entry = hypTraceStack.pop(0)
                for back_pointer in trace_entry.bp:
                    hypTraceStack.insert(0, back_pointer)
                inf_rule = trace_entry.inf_rule
                if inf_rule is not None:  # Non-leaf nodes in derivation
                    tF.write("%s ||| %s ||| %s ||| %s\n" %
                             (inf_rule.src, inf_rule.tgt,
                              Hypothesis.getFeatVec(trace_entry),
                              trace_entry.inf_cell))
                else:  # Leaf nodes in derivation
                    tF.write("%s ||| %s ||| %s |||| %s\n" %
                             (trace_entry.src,
                              Hypothesis.getHypothesis(trace_entry),
                              Hypothesis.getFeatVec(trace_entry),
                              trace_entry.inf_cell))

            tF.write("TRACE_END\n")
            nbest_cnt += 1
            del hypTraceStack[:]
            if nbest_cnt == settings.opts.trace_rules: break
        tF.close()
Exemple #3
0
    def forceDecodePrune(self, refsLst, last_cell=False):
        '''Prune the top-level cells for force decoding'''

        left_side = 'S'             # forceDecodePrune() can only be used in 'S' cells for 'S' derivations
        for key in self.table.iterkeys():
            if key[0] != left_side: continue
            cand_indx = 0
            for cand in self.table[key][:]:
                matches_ref = False
                cand_tgt = Hypothesis.getHypothesis(cand)
                for ref_i in refsLst:
                    if (not last_cell and ref_i.startswith(cand_tgt)) or (last_cell and ref_i == cand_tgt):
                        matches_ref = True
                        break
                if not matches_ref: del self.table[key][cand_indx]
                else: cand_indx += 1

            # If all the entries are deleted, then there will be no S derivations in the cell; set has_S_tree as False
            if not self.table[key]: self.has_S_tree = False
        return self.has_S_tree
Exemple #4
0
    def forceDecodePrune(self, refsLst, last_cell=False):
        '''Prune the top-level cells for force decoding'''

        left_side = 'S'  # forceDecodePrune() can only be used in 'S' cells for 'S' derivations
        for key in self.table.iterkeys():
            if key[0] != left_side: continue
            cand_indx = 0
            for cand in self.table[key][:]:
                matches_ref = False
                cand_tgt = Hypothesis.getHypothesis(cand)
                for ref_i in refsLst:
                    if (not last_cell and ref_i.startswith(cand_tgt)) or (
                            last_cell and ref_i == cand_tgt):
                        matches_ref = True
                        break
                if not matches_ref: del self.table[key][cand_indx]
                else: cand_indx += 1

            # If all the entries are deleted, then there will be no S derivations in the cell; set has_S_tree as False
            if not self.table[key]: self.has_S_tree = False
        return self.has_S_tree