Create template from equation
Project description
Template from Equation
This library provides functions to create template (make template) from its associated equation (e.g. 2+5 -> n0+n1).
The term "equation template" is first introduced in Kushman et al.(2014). It represents the abstraction of equation systems. The concept of it are then used in many following works. We found a tool to create such templates useful but not publicly available. So we tried to write one and share with peers in the area of automatic math word problem solving in NLP.
The template generated would contain "n0, n1, n2, ..." for numbers and "x0, x1, x2, ..." for variables. For example:
- 2+5 -> n0+n1
- 2+5*60/x = y -> n0 + n1 * n2 / x0 = x1
In some situations, we want to keep some constants in the equation unchanged. For example, to get the template of the equation "2+7*60 = x" for the problem "How many minutes is 2 hours and 7 minutes?", we want to keep the constant "60" unchanged. In this situation, our function allows passing a number slot which contains only the numbers we want to substitute for our template. (See the Example Usage below).
To make template from equation, we use the ast trees. When traversing an AST tree, it returns "n0, n1, n2, ..." for the leaf nodes of numbers, and "x0, x1, x2" for variables. Then, an equation of "2+5*x" can be turned into its template "n0+n1*x0". (Using AST tree can help us avoid some subtle string matching difficulties of regular expression.)
Installation
pip install template_from_equation
Usage
>>> from template_from_equation import Equation
>>> eq1 = Equation("2+5")
>>> eq1
2 + 5
>>> eq1.get_template()
'n0 + n1'
>>> eq2 = Equation("2+7*60 = x")
>>> eq2
2 + 7 * 60 = x
>>> eq2.get_template()
'n0 + n1 * n2 = x0'
>>> eq2.get_template(num_slot=[2,7])
'n0 + n1 * 60 = x0'
>>> eq3 = Equation("2*x+5*12 = y")
>>> eq3.get_template()
'n0 * x0 + n1 * n2 = x1'
>>> eq3.get_template(num_slot=[2,5])
'n0 * x0 + n1 * 12 = x1'
Todo
- An option to share tokens for repeated numbers, i.e. 2+5*2 -> n0 +n1 * n0
- To parse the operator "^"
References
- Kushman, Nate, et al. "Learning to automatically solve algebra word problems." Proceedings of the 52nd Annual Meeting of the Association for Computational Linguistics (Volume 1: Long Papers). Vol. 1. 2014.
- codegen: Some of the naming styles but not the code are adapted from codegen, which is a library to convert ast tree to python code
License
This project is licensed under the terms of the MIT license.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distributions
Built Distribution
File details
Details for the file template_from_equation-0.1.2-py3-none-any.whl
.
File metadata
- Download URL: template_from_equation-0.1.2-py3-none-any.whl
- Upload date:
- Size: 5.7 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.21.0 setuptools/41.0.1 requests-toolbelt/0.9.1 tqdm/4.31.1 CPython/3.7.3
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 1ae11d6297fe615cdec3afbe0c64ea0232152d546ddcaf619d306e00806cba5d |
|
MD5 | 06727ad68453ce74db89d1e182690ac7 |
|
BLAKE2b-256 | e1e286428d18cae0535f10a2fcd4ddf309f551c3299eba5686ed52aafdc34e04 |