def decode_vault_size(self, lhs, cf): assert not lhs.startswith('L') cf %= self.G[lhs]['__total__'] if cf == 0 and lhs == 'G': print_err("Grammar of size 0!!!!", lhs, cf) #return cf i = getIndex(cf, self.G[lhs].values()) return i + 1
def getGenerationAtRule( lhs, prob, grammar): """ given a left hand side of a rule and the grammar it finds the exact rhs which has CP(cumulative Probability) in the ruleset just more than the given `prob` :returns `('IloveYou',0,420)` """ # REMEMBER: every array in grammar rule should have start with a # dummy entry('',0,0) and prob zero!! d = [0] d.extend([x[2] for x in grammar[lhs]]) for i in xrange(1, len(d)): d[i] += d[i-1]; prob = prob % d[-1] t = getIndex ( d, 0, len(d)-1, prob ) - 1; return grammar[lhs][t]
def freq2key(f, T, A): i = getIndex(f, A) w = T.restore_key(i) return w, A[i]