def reducedConcatPrimitives(): #uses strConcat!! #[Primitive("empty_string", arrow(tpregex, tpregex), PRC(pregex.String(""))) #] + [ return [ Primitive("string_" + i, arrow(tpregex, tpregex), PRC( pregex.String(i))) for i in printable[:-4] if i not in disallowed_list ] + [ Primitive("string_" + name, arrow(tpregex, tpregex), PRC(pregex.String(char))) for char, name in disallowed ] + [ Primitive("r_dot", arrow(tpregex, tpregex), PRC(pregex.dot)), Primitive("r_d", arrow(tpregex, tpregex), PRC(pregex.d)), Primitive("r_s", arrow(tpregex, tpregex), PRC(pregex.s)), #Primitive("r_w", arrow(tpregex, tpregex), PRC(pregex.w)), Primitive("r_l", arrow(tpregex, tpregex), PRC(pregex.l)), Primitive("r_u", arrow(tpregex, tpregex), PRC(pregex.u)), #todo Primitive("r_kleene", arrow(arrow(tpregex, tpregex), arrow(tpregex, tpregex)), PRC(pregex.KleeneStar, 1)), #Primitive("r_plus", arrow(arrow(tpregex, tpregex), arrow(tpregex,tpregex)), PRC(pregex.Plus,1)), #Primitive("r_maybe", arrow(arrow(tpregex, tpregex), arrow(tpregex,tpregex)), PRC(pregex.Maybe,1)), Primitive( "r_alt", arrow(arrow(tpregex, tpregex), arrow(tpregex, tpregex), arrow(tpregex, tpregex)), PRC(pregex.Alt, 2)), ] + [Primitive("r_const", arrow(tpregex, tpregex), None)]
def concatPrimitives(): return [ Primitive("string_" + i, arrow(tpregex, tpregex), PRC( pregex.String(i))) for i in printable[:-4] if i not in disallowed_list ] + [ Primitive("string_" + name, arrow(tpregex, tpregex), PRC(pregex.String(char))) for char, name in disallowed ] + [ Primitive("r_dot", arrow(tpregex, tpregex), PRC(pregex.dot)), Primitive("r_d", arrow(tpregex, tpregex), PRC(pregex.d)), Primitive("r_s", arrow(tpregex, tpregex), PRC(pregex.s)), Primitive("r_w", arrow(tpregex, tpregex), PRC(pregex.w)), Primitive("r_l", arrow(tpregex, tpregex), PRC(pregex.l)), Primitive("r_u", arrow(tpregex, tpregex), PRC(pregex.u)), #todo Primitive("r_kleene", arrow(arrow(tpregex, tpregex), arrow(tpregex, tpregex)), PRC(pregex.KleeneStar, 1)), Primitive("r_plus", arrow(arrow(tpregex, tpregex), arrow(tpregex, tpregex)), PRC(pregex.Plus, 1)), Primitive("r_maybe", arrow(arrow(tpregex, tpregex), arrow(tpregex, tpregex)), PRC(pregex.Maybe, 1)), Primitive( "r_alt", arrow(arrow(tpregex, tpregex), arrow(tpregex, tpregex), arrow(tpregex, tpregex)), PRC(pregex.Alt, 2)), ]
def __call__(self, pre): if self.arity == len(self.args): if self.arity == 0: return pregex.Concat([self.f, pre]) elif self.arity == 1: return pregex.Concat([self.f(*self.args), pre]) else: return pregex.Concat([self.f(self.args), pre]) #this line is bad, need brackets around input to f if f is Alt else: return PRC(self.f, self.arity, args=self.args+[pre(pregex.String(""))])
def basePrimitives(): return [Primitive("string_" + i, tpregex, pregex.String(i)) for i in printable[:-4] if i not in disallowed_list ] + [ Primitive("string_" + name, tpregex, pregex.String(char)) for char, name in disallowed ] + [ Primitive("r_dot", tpregex, pregex.dot), Primitive("r_d", tpregex, pregex.d), Primitive("r_s", tpregex, pregex.s), Primitive("r_w", tpregex, pregex.w), Primitive("r_l", tpregex, pregex.l), Primitive("r_u", tpregex, pregex.u), Primitive("r_kleene", arrow(tpregex, tpregex), _kleene), Primitive("r_plus", arrow(tpregex, tpregex), _plus), Primitive("r_maybe", arrow(tpregex, tpregex), _maybe), Primitive("r_alt", arrow(tpregex, tpregex, tpregex), _alt), Primitive("r_concat", arrow(tpregex, tpregex, tpregex), _concat), ]
def matchEmpericalNoLetterPrimitives(corpus): return lambda: [ Primitive("empty_string", tpregex, pregex.String("")) ] + [ Primitive("string_" + i, tpregex, pregex.String(i)) for i in printable[:-4] if i not in disallowed_list + list(printable[10:62]) ] + [ Primitive("string_" + name, tpregex, pregex.String(char)) for char, name in disallowed ] + [ Primitive("r_dot", tpregex, emp_dot_no_letter(corpus) ), Primitive("r_d", tpregex, emp_d(corpus) ), Primitive("r_s", tpregex, pregex.s), Primitive("r_kleene", arrow(tpregex, tpregex), _kleene), #Primitive("r_plus", arrow(tpregex, tpregex), _plus), #Primitive("r_maybe", arrow(tpregex, tpregex), _maybe), Primitive("r_alt", arrow(tpregex, tpregex, tpregex), _alt), Primitive("r_concat", arrow(tpregex, tpregex, tpregex), _concat), ]
def easyWordsPrimitives(): return [ Primitive("string_" + i, tpregex, pregex.String(i)) for i in printable[10:62] if i not in disallowed_list ] + [ Primitive("r_d", tpregex, pregex.d), Primitive("r_s", tpregex, pregex.s), #Primitive("r_w", tpregex, pregex.w), Primitive("r_l", tpregex, pregex.l), Primitive("r_u", tpregex, pregex.u), Primitive("r_kleene", arrow(tpregex, tpregex), _kleene), Primitive("r_plus", arrow(tpregex, tpregex), _plus), Primitive("r_maybe", arrow(tpregex, tpregex), _maybe), Primitive("r_alt", arrow(tpregex, tpregex, tpregex), _alt), Primitive("r_concat", arrow(tpregex, tpregex, tpregex), _concat), ]
for char, name in disallowed ] + [ Primitive("r_dot", tpregex, emp_dot_no_letter(corpus)), Primitive("r_d", tpregex, emp_d(corpus)), Primitive("r_s", tpregex, pregex.s), Primitive("r_kleene", arrow(tpregex, tpregex), _kleene), #Primitive("r_plus", arrow(tpregex, tpregex), _plus), #Primitive("r_maybe", arrow(tpregex, tpregex), _maybe), Primitive("r_alt", arrow(tpregex, tpregex, tpregex), _alt), Primitive("r_concat", arrow(tpregex, tpregex, tpregex), _concat), ] if __name__ == '__main__': concatPrimitives() from dreamcoder.program import Program p = Program.parse( "(lambda (r_kleene (lambda (r_maybe (lambda (string_x $0)) $0)) $0))") print(p) print(p.runWithArguments([pregex.String("")])) prims = concatPrimitives() g = Grammar.uniform(prims) for i in range(100): prog = g.sample(arrow(tpregex, tpregex)) preg = prog.runWithArguments([pregex.String("")]) print("preg:", preg.__repr__()) print("sample:", preg.sample())