コード例 #1
0
 def interactive(self, model):
     with open(self.args.train_path, "rt") as fin:
         train_json = json.loads(fin.read())
         train_trio = build_equation_tree_examples_list(train_json,
                                                        numeric=self.args.numeric,
                                                        unify_one_zero=self.args.unify_one_zero)
         train = set(str(tree) for tree,_,_ in train_trio)
     gtrace = []
     exit = False
     while True:
         while True:
             try:
                 s = input("enter an equation:\n")
                 if s == "exit":
                     exit = True
                     break
                 equation = parse_equation(s)
                 break
             except Exception:
                 traceback.print_exc()
         if exit:
             break
         trio = load_single_equation_tree_example(equation.dump())
         tree = trio[0]
         tree_r = load_single_equation_tree_example(equation.dump())[0]
         tree_r.lchild, tree_r.rchild = tree_r.rchild, tree_r.lchild
         print((str(tree) in train) or (str(tree_r) in train))
         loader = sequential_sampler(trios=[trio],batch_size=1)
         trace = []
         record, loss = self.eval(model, loader, trace=trace)
         print(trace[0].probability)
         gtrace.append(trace[0])
     if self.args.trace_path:
         with open(self.args.trace_path, "wb") as f:
             f.write(pickle.dumps({"train":gtrace}))
コード例 #2
0
 def __init__(self, equation_str="", visual=False):
     self.pol = None
     equation = parse_equation(equation_str)
     if equation:
         self.pol = Polynomial(equation)
         print("Reduced form: ", self.pol)
         self.pol.solve()
         if visual:
             a, b, c, = self.pol.coefs()
             PolGraph(a, b, c).plot()
     else:
         print("Invalid equation: ", equation_str)
         usage()
コード例 #3
0
ファイル: views.py プロジェクト: mikeyrichardson/kybur
def edit_lesson(lesson_id):
    lesson = Lesson.query.get_or_404(lesson_id)
    form = AddProblemForm()
    if form.validate_on_submit():
        num_problems = Problem.query.filter_by(lesson_id=lesson.id).count()
        parsed_equation = parse.parse_equation(form.text.data)
        parsed_equation['number'] = num_problems + 1
        parsed_equation['lesson_id'] = lesson.id
        equation = Problem(**parsed_equation)
        db.session.add(equation)
        db.session.commit()
        return redirect(url_for('teacher.edit_lesson', lesson_id=lesson_id))
    else:
        for field, errors in form.errors.items():
            for error in errors:
                flash(error)
    return render_template('teacher/edit_lesson.html', lesson=lesson, form=form)
コード例 #4
0
ファイル: forms.py プロジェクト: mikeyrichardson/kybur
 def validate_text(self, field):
     try:
         parse.parse_equation(field.data)
     except parse.ParseError as e:
         raise ValidationError(e.message)