Ejemplo n.º 1
0
 def show(ded, antvalues):
     if ded.rule:
         value = rule.subst(ded.rule.erhs, antvalues)
     else:
         value = antvalues[0]
     return ("[%.3f" %
             cost.prob(ded.dcost['posterior']), ) + value + ("]", )
Ejemplo n.º 2
0
 def transition(self, r, antstates, i, j, j1=None):
     enums = [self.map_word(e) for e in rule.subst(r.erhs, antstates)]
     score = 0.
     for i, j in self.xngrams(enums):
         score += self.ngram.lookup_ngrams(enums, i + self.order - 1, j)
     state = self.make_state(enums)
     return (state, self.unit * -score)
Ejemplo n.º 3
0
 def transition(self, r, antstates, i, j, j1=None):
     enums = [self.map_word(e) for e in rule.subst(r.erhs, antstates)]
     score = 0.
     for i, j in self.xngrams(enums):
         score += self.ngram.lookup_ngrams(enums, i+self.order-1, j)
     state = self.make_state(enums)
     return (state, self.unit * -score)
Ejemplo n.º 4
0
    def compute_nbest(self, item, n):
        """Assumes that the 1-best has already been found
        and stored in Deduction.viterbi"""

        if id(item) not in self.nbinfos:
            self.nbinfos[id(item)] = NBestInfo(item)
        nb = self.nbinfos[id(item)]

        while len(nb.nbest) < n and len(nb.cands) > 0:
            # Get the next best and add it to the list
            (cost, ded, ranks) = heapq.heappop(nb.cands)

            if self.ambiguity_limit:
                # compute English string
                antes = []
                for ant, rank in itertools.izip(ded.ants, ranks):
                    self.compute_nbest(ant, rank + 1)
                    antes.append(self.nbinfos[id(ant)].english[rank])
                if ded.rule is not None:
                    e = rule.subst(ded.rule.erhs, antes)
                elif len(antes) == 1:  # this is used by the Hiero goal item
                    e = antes[0]

                # don't want more than ambiguity_limit per english
                nb.ecount[tuple(e)] += 1
                if nb.ecount[tuple(e)] <= self.ambiguity_limit:
                    nb.nbest.append((cost, ded, ranks))
                    nb.english.append(e)
            else:
                nb.nbest.append((cost, ded, ranks))

            # Replenish the candidate pool
            for ant_i in xrange(len(ded.ants)):
                ant, rank = ded.ants[ant_i], ranks[ant_i]

                if self.compute_nbest(ant, rank + 2) >= rank + 2:
                    ant_nb = self.nbinfos[id(ant)]
                    nextranks = list(ranks)
                    nextranks[ant_i] += 1
                    nextranks = tuple(nextranks)
                    if (ded, nextranks) not in nb.index:
                        nextcost = cost - ant_nb.nbest[rank][0] + ant_nb.nbest[
                            rank + 1][0]
                        heapq.heappush(nb.cands, (nextcost, ded, nextranks))
                        nb.index.add((ded, nextranks))

        return len(nb.nbest)
Ejemplo n.º 5
0
    def compute_nbest(self, item, n):
        """Assumes that the 1-best has already been found
        and stored in Deduction.viterbi"""

        if id(item) not in self.nbinfos:
            self.nbinfos[id(item)] = NBestInfo(item)
        nb = self.nbinfos[id(item)]

        while len(nb.nbest) < n and len(nb.cands) > 0:
            # Get the next best and add it to the list
            (cost,ded,ranks) = heapq.heappop(nb.cands)

            if self.ambiguity_limit:
                # compute English string
                antes = []
                for ant, rank in itertools.izip (ded.ants, ranks):
                    self.compute_nbest(ant, rank+1)
                    antes.append(self.nbinfos[id(ant)].english[rank])
                if ded.rule is not None:
                    e = rule.subst(ded.rule.erhs, antes)
                elif len(antes) == 1: # this is used by the Hiero goal item
                    e = antes[0]

                # don't want more than ambiguity_limit per english
                nb.ecount[tuple(e)] += 1
                if nb.ecount[tuple(e)] <= self.ambiguity_limit:
                    nb.nbest.append((cost,ded,ranks))
                    nb.english.append(e)
            else:
                nb.nbest.append((cost,ded,ranks))

            # Replenish the candidate pool
            for ant_i in xrange(len(ded.ants)):
                ant, rank = ded.ants[ant_i], ranks[ant_i]

                if self.compute_nbest(ant, rank+2) >= rank+2:
                    ant_nb = self.nbinfos[id(ant)]
                    nextranks = list(ranks)
                    nextranks[ant_i] += 1
                    nextranks = tuple(nextranks)
                    if (ded, nextranks) not in nb.index:
                        nextcost = cost - ant_nb.nbest[rank][0] + ant_nb.nbest[rank+1][0]
                        heapq.heappush(nb.cands, (nextcost, ded, nextranks))
                        nb.index.add((ded,nextranks))

        return len(nb.nbest)
Ejemplo n.º 6
0
 def show(ded, antvalues):
     if ded.rule:
         value = rule.subst(ded.rule.erhs, antvalues)
     else:
         value = antvalues[0]
     return ("[%.3f" % cost.prob(ded.dcost["posterior"]),) + value + ("]",)
Ejemplo n.º 7
0
 def english(self):
     return self.value(lambda ded, antvalues: rule.subst(
         ded.rule.erhs, antvalues) if ded.rule else antvalues[0])
Ejemplo n.º 8
0
 def french(self):
     return self.value(lambda ded, antvalues: rule.subst(
         ded.rule.frhs, antvalues) if ded.rule else antvalues[0])
Ejemplo n.º 9
0
 def english(self):
     return self.value(lambda ded, antvalues: rule.subst(ded.rule.erhs, antvalues) if ded.rule else antvalues[0])
Ejemplo n.º 10
0
 def french(self):
     return self.value(lambda ded, antvalues: rule.subst(ded.rule.frhs, antvalues) if ded.rule else antvalues[0])