Skip to content

Instantly share code, notes, and snippets.

@OhadRubin
Created July 4, 2023 11:41
Show Gist options
  • Save OhadRubin/5d6048c2335c57744ba6dbc0024b2f35 to your computer and use it in GitHub Desktop.
Save OhadRubin/5d6048c2335c57744ba6dbc0024b2f35 to your computer and use it in GitHub Desktop.
injecting numeracy
import random
from sympy import Eq, Symbol, solve
from transitions import Machine
class WordProblemStateMachine:
def __init__(self):
self.global_state = {}
def initialize_container(self, container, entity, quantity, attribute):
if container not in self.global_state:
self.global_state[container] = {}
self.global_state[container][entity] = quantity
def positive_transfer(self, from_container, to_container, entity, quantity, attribute):
self.transfer(from_container, to_container, entity, quantity)
def negative_transfer(self, from_container, to_container, entity, quantity, attribute):
self.transfer(to_container, from_container, entity, quantity)
def transfer(self, from_container, to_container, entity, quantity):
self.global_state[from_container][entity] -= quantity
if to_container not in self.global_state:
self.global_state[to_container] = {}
if entity in self.global_state[to_container]:
self.global_state[to_container][entity] += quantity
else:
self.global_state[to_container][entity] = quantity
def get_container_quantity(self, container, entity):
return self.global_state.get(container, {}).get(entity, None)
class WordProblemGenerator:
def __init__(self):
self.templates = [
("{container1} has {quantity1} {attribute1} {entity1}",
"state_machine.initialize_container('{container1}', '{entity1}', {quantity1}, '{attribute1}')"),
("{container1} gives {quantity2} {attribute1} {entity1} to {container2}",
"state_machine.positive_transfer('{container1}', '{container2}', '{entity1}', {quantity2}, '{attribute1}')"),
("{container1} takes {quantity2} {attribute1} {entity1} from {container2}",
"state_machine.negative_transfer('{container1}', '{container2}', '{entity1}', {quantity2}, '{attribute1}')")
]
self.container_names = ["Alice", "Bob", "Charlie"]
self.entities = ["apple", "ball", "car"]
self.attributes = {
"apple": ["red", "green", "yellow"],
"ball": ["large", "small", "medium"],
"car": ["blue", "black", "white"]
}
def generate_problem(self, actions):
problem_text = []
problem_code = []
for action in actions:
template_idx = action['template_idx']
template_args = action['template_args']
current_template = self.templates[template_idx]
current_problem_text = current_template[0].format(**template_args)
current_problem_code = current_template[1].format(**template_args)
problem_text.append(current_problem_text)
problem_code.append(current_problem_code)
return "\n".join(problem_text), "\n".join(problem_code), None # The final answer calculation can be implemented later
# Example usage
problem_generator = WordProblemGenerator()
actions = [
{'template_idx': 0, 'template_args': {'container1': 'Alice', 'quantity1': 10, 'entity1': 'balls', 'attribute1': 'big'}},
{'template_idx': 0, 'template_args': {'container1': 'Bob', 'quantity1': 5, 'entity1': 'balls', 'attribute1': 'big'}},
{'template_idx': 0, 'template_args': {'container1': 'Charlie', 'quantity1': 11, 'entity1': 'balls', 'attribute1': 'big'}},
{'template_idx': 2, 'template_args': {'container1': 'Bob', 'container2': 'Charlie', 'quantity2': 6, 'entity1': 'balls', 'attribute1': 'big'}},
{'template_idx': 2, 'template_args': {'container1': 'Alice', 'container2': 'Bob', 'quantity2': 7, 'entity1': 'balls', 'attribute1': 'big'}}
]
problem_text, problem_code, final_answer = problem_generator.generate_problem(actions)
print("Problem Text:", problem_text)
print("Problem Code:", problem_code)
state_machine = WordProblemStateMachine()
# print(exec(problem_code))
# Calculate Bob's final quantity of balls
final_answer = state_machine.get_container_quantity('Bob', 'balls')
print("Final Answer:", final_answer)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment