Exemplo n.º 1
0
 def decode_grammar(self, P):
     g=SubGrammar(self.G)
     vd = VaultDistribution()
     iterp = iter(P)
     stack = ['G']
     done = []
     while stack:
         head = stack.pop()
         assert head not in done
         done.append(head)
         p = iterp.next()
         n = vd.decode_vault_size(head, p)
         #print "RuleSizeDecoding:", head, n
         t_set = []
         for x in range(n):
             rhs = self.decode(head, iterp.next())
             #print "Decoding:", stack, head, '==>', rhs
             if rhs != '__totoal__':
                 r = filter(lambda x: x not in done+stack, 
                            self.G.get_actual_NonTlist(head, rhs))
                 if r:
                     for x in r:
                         if (x not in t_set):
                             t_set.append(x)
             g.add_rule(head, rhs)
         t_set.reverse()
         stack.extend(t_set)
     g.finalize() # fixes the freq and some other book keepings
     return g
Exemplo n.º 2
0
 def encode_grammar(self, G):
     # Encode sub-grammar
     vd = VaultDistribution()
     stack = ['G']
     code_g = []
     done = []
     while stack:
         head = stack.pop()
         assert head not in done
         done.append(head)
         rule_dict = G[head]
         t_set = []
         for rhs, f in rule_dict.items():
             if rhs != '__total__':
                 r = filter(lambda x: x not in done+stack, 
                            self.G.get_actual_NonTlist(head, rhs))
                 if r:
                     for x in r:
                         if (x not in t_set):
                             t_set.append(x)
         t_set.reverse()
         stack.extend(t_set)
         n = len(rule_dict.keys())-1
         code_g.append(vd.encode_vault_size(head, n))
         if n<0: 
             print "Sorry I cannot encode your password! Please choose"
             print "something different, password12"
             exit(0)
         assert n == vd.decode_vault_size(head, code_g[-1])
         code_g.extend([self.encode(head, r) 
                        for r in rule_dict.keys()
                        if r != '__total__'])
     extra = hny_config.HONEY_VAULT_GRAMMAR_SIZE - len(code_g);
     code_g.extend([convert2group(0,1) for x in range(extra)])
     return code_g