Skip to content
Browse files

Checkin. Partially working for calibaration 2

  • Loading branch information...
1 parent c15a874 commit 66b9ef555eeca299f168ea75aaa4867694b9670f Eric Garside committed Jun 22, 2012
Showing with 80 additions and 17 deletions.
  1. +1 −1 calibrations/001.txt
  2. +3 −2 definitions.py
  3. +4 −0 inferences.py
  4. +56 −13 interpreter.py
  5. +14 −0 quirks.py
  6. +2 −0 solver.py
  7. +0 −1 zoidberg.py
View
2 calibrations/001.txt
@@ -1 +1 @@
-1 + 1
+Jane has 14 balloons. She buys another 6 balloons. How many balloons does she have now?
View
5 definitions.py
@@ -10,7 +10,7 @@ class Operation:
class Defs:
ADDITION=["+", "buys", "another"]
- SUBTRACTION=["-"]
+ SUBTRACTION=["-", "ate", "gave", "sells"]
MULTIPLICATION=["*"]
DIVISION=["/"]
@@ -33,4 +33,5 @@ class Defs:
EQUIVALENCE=["="]
# Conceptual indicator lists
-ownership = ["has"]
+ownership = ["has", "bought", "his", "her"]
+variable = ["some"]
View
4 inferences.py
@@ -39,3 +39,7 @@ def ownership(s):
@staticmethod
def operation(s):
return s in defs.operation
+
+ @staticmethod
+ def variable(s):
+ return s in defs.variable
View
69 interpreter.py
@@ -11,6 +11,10 @@
from expression import Expression
from term import Term
from sympy import Symbol
+from quirks import quirk_fix
+from re import compile
+
+ALPHA = compile("[A-Za-z]")
class Interpretation(Thinker):
def __init__(self, problem):
@@ -20,11 +24,13 @@ def __init__(self, problem):
self.context_ownership = False
# Current state
+ self.last_tag = None
self.last_value = None
self.last_operation = None
self.last_relation = None
self.last_unit = None
self.last_context = None
+ self.primary_context = None
# Output
self.expression = None
@@ -71,7 +77,7 @@ def _analyze(self):
is_equivalence = self.statement.relation == Relation.EQUIVALENCE
if is_equivalence and len(self.statement.expressions) == 1:
if self.expression:
- self._think("Adding current expression to statement.")
+ self._think("Inserting current expression to statement.")
self._add("expression")
else:
self._create_expression("for hanging equality.")
@@ -103,6 +109,15 @@ def _create_context(self, context):
self._think("New context '{0}'", context)
self.contexts[context] = {}
+ def _rename_context(self, context, newname):
+ if context in self.contexts:
+ self._think("Context '{0}' becomes '{1}'", context, newname)
+ self.contexts[newname] = self.contexts[context]
+ del self.contexts[context]
+ if context == self.primary_context:
+ self._think("Renaming primary context to '{0}'", newname)
+ self.primary_context = newname
+
def _create_unit(self, unit):
if not unit in self.units:
self._think("New unit '{0}'", unit)
@@ -121,10 +136,8 @@ def _handle_unit_context(self):
"for {0} owned by {1}".format(
self.last_unit, self.last_context))
c[self.last_unit] = self.expression
- self._clear("last_unit")
else:
self._add("last_value")
- self._clear("last_unit")
def _handle_relation(self):
if self.last_relation:
@@ -134,10 +147,20 @@ def _handle_relation(self):
self._add("last_relation")
def _handle_tag(self, tag, word):
- if tag == "NNP": # noun, proper, singular
+ tag, word, thought = quirk_fix(tag, word)
+ if thought is not None:
+ self._think(thought)
+
+ if tag in ["NNP", "NN"]: # noun, proper, singular, common
self._think("I think '{0}' is a context", word)
- self._create_context(word)
- self._set_context(word)
+ if self.last_tag == tag:
+ context = "{0} {1}".format(self.last_context, word)
+ self._rename_context(self.last_context, context)
+ self.last_context = None
+ self._set_context(context)
+ else:
+ self._create_context(word)
+ self._set_context(word)
if tag == "NNS": #noun, common, plural
if self.last_value and not self.last_unit:
@@ -146,16 +169,16 @@ def _handle_tag(self, tag, word):
if word in self.units:
self._set_unit(word)
- if tag == "VBZ": # verb, present tense, 3rd person singular
+ if tag in ["VBZ", "VBD"]: # verb, present tense, past tense
if self.last_context:
if infer.conveys.ownership(word):
self._think("I think '{0}' conveys ownership", word)
self.context_ownership = True
elif infer.is_operation(word):
- self._think("I think '{0}' conveys operation", word)
+ self._think("I think '{0}' conveys an operation", word)
self._set_operation(word)
- if tag == "PRP": # pronoun, personal
+ if tag in ["PRP", "PRP$"]: # pronoun, personal
if self.last_context:
self._think("I think '{0}' refers to '{1}'", word,
self.last_context)
@@ -164,6 +187,15 @@ def _handle_tag(self, tag, word):
self._think("I think '{0}' is a determiner.", word)
if infer.is_operation(word):
self._think("I think '{0}' is an operation; skipping.", word)
+ if infer.conveys.variable(word):
+ self._set_variable(word)
+
+ if tag == ".":
+ if self.last_context != self.primary_context:
+ self._think("Resetting context to '{0}'", self.primary_context)
+ self.last_context = self.primary_context
+
+ self.last_tag = tag
# Expression building
def _add_term(self, term):
@@ -177,6 +209,9 @@ def _clear(self, v):
def _add(self, v):
val = getattr(self, v)
self._clear(v)
+ if val is None:
+ return
+
if v == "expression":
self.statement.expressions.append(val)
return
@@ -193,15 +228,23 @@ def _add(self, v):
t = Term(Term.VALUE, val)
if v == "last_operation":
t = Term(Term.OPERATION, val)
- self._think("Adding term '{0}' to current expression.", str(t))
+ self._think("Inserting term '{0}' to current expression.", str(t))
self._add_term(t)
# States
def _set_context(self, context):
+ if not self.primary_context:
+ self._think("Setting primary context to '{0}'", context)
+ self.primary_context = context
+
if not self.last_context:
self._think("Setting current context to '{0}'", context)
self.last_context = context
self._handle_unit_context()
+ else:
+ self._think("Switching context to '{0}'", context)
+ self._handle_unit_context()
+ self.last_context = context
def _set_unit(self, unit):
if not self.last_unit:
@@ -220,13 +263,13 @@ def _set_variable(self, name=None, context=None):
else:
v = self._create_variable()
self.last_value = v
- if self.build_expression:
+ if self.build_expression or self.expression:
self._add("last_value")
def _set_constant(self, v):
v = convert.to_number(v)
self.last_value = v
- if self.build_expression:
+ if self.build_expression or self.expression:
self._add("last_value")
def _set_relation(self, v):
@@ -254,7 +297,7 @@ def _set_operation(self, v):
# Support
def _interpret(self, word, tag):
- if word.isalpha():
+ if ALPHA.match(word) or tag == ".":
if self.build_expression:
self._set_variable(word)
else:
View
14 quirks.py
@@ -0,0 +1,14 @@
+#!/usr/bin/env python
+def quirk_fix(tag, word):
+ thought = None
+ if word == "ate":
+ if tag == "NN":
+ thought = "I think ate is a verb"
+ # Past tense of "eat" not the goddess Ate.
+ tag = "VBD"
+
+ if thought is not None:
+ thought = "Quirkfix: {0}".format(thought)
+
+ return tag, word, thought
+
View
2 solver.py
@@ -27,6 +27,8 @@ def _solve_equivalence_statement(self, s):
self._think("Solving equivalence statement")
a = self._solve_expression(expressions[0])
b = self._solve_expression(expressions[1])
+ self._think("I think a = {0}", a)
+ self._think("I think b = {0}", b)
soln = solve(Eq(a, b))
# solution returned as an array of answers or an object
View
1 zoidberg.py
@@ -22,7 +22,6 @@ class Operation:
"-"
]
-
class AnswerType:
# Answer is likely a number
NUMERIC="num"

0 comments on commit 66b9ef5

Please sign in to comment.
Something went wrong with that request. Please try again.