コード例 #1
0
 def score_rule(self, a, r):
     funaligned = eunaligned = 0
     fweight = eweight = 1.0
     fratio = 0.0
     for i in range(len(r.f)):
         if not isvar(r.f[i]):
             if not a.faligned[r.fpos[i]]:
                 funaligned += 1
             if self.fweights is not None:
                 fweight *= self.fweights[r.fpos[i]]
             if self.fratios is not None:
                 fratio += self.fratios[r.fpos[i]]
     for i in range(len(r.e)):
         if not isvar(r.e[i]):
             if not a.ealigned[r.epos[i]]:
                 eunaligned += 1
             if self.eweights is not None:
                 eweight *= self.eweights[r.epos[i]]
     scores = []
     if self.fweights is not None:
         scores.append(fweight)
     if self.eweights is not None:
         scores.append(eweight)
     if self.fratios is not None:
         scores.append(fratio)
     return scores
コード例 #2
0
ファイル: grammar.py プロジェクト: fanglicheng/decoder
 def add(self, rule):
     node = self.root
     for word in rule.f:
         if FLAGS.nt_mismatch:
             if isvar(word):
                 word = nocat(word)
         node = node.setdefault(
             word, TrieNode(self.rule_bin_size, node.sym_list + (word, )))
     node.add(rule)
     self.added += 1
コード例 #3
0
 def weight(self, rule):
     num_terminals = 0
     for tok in rule.f:
         if isvar(tok):
             continue
         num_terminals += 1
     if num_terminals > 0:
         return num_terminals
     else: #Punish rules without any terminals
         return -3
コード例 #4
0
ファイル: rule_filter.py プロジェクト: fanglicheng/decoder
def filter(rule):
    #if rule.level > FLAGS.compose_level:
    #    return False
    if rule.arity > 2:
        return False
    if len(rule.f) > 5:
        return False
    # filter adjacent nonterminals
    nts = [isvar(sym) for sym in rule.f]
    for a, b in zip([False] + nts, nts + [False]):
        if a and b:
            return False
    return True
コード例 #5
0
 def weight(self, rule):
     for w in rule.f:
         if isvar(w) == False:
             return 0
     return 10
コード例 #6
0
 def weight(self, rule):
     count = 0
     for tok in rule.f:
         if isvar(tok):
             count += 1
     return count