@@ -31,17 +31,23 @@ class _MockLM:
3131 model = "mock/local"
3232 def __call__ (self , * , prompt : str , ** kwargs ):
3333 import re , json as _json
34- qmatch = re .search (r"Question:\s*(.*)" , prompt , re .S )
35- question = qmatch .group (1 ).strip () if qmatch else prompt
34+ qmatch = re .search (r"\nQuestion:\s*(.*?)\n\nState:" , prompt , re .S )
35+ if qmatch :
36+ question = qmatch .group (1 ).strip ()
37+ else :
38+ qs = re .findall (r"\bQuestion:\s*(.*)" , prompt )
39+ question = qs [- 1 ].strip () if qs else prompt
3640 qn = (question
3741 .replace ("\u00d7 " , "x" )
3842 .replace ("\u00f7 " , "/" )
3943 .replace ("\u2212 " , "-" )
4044 .replace ("\u2013 " , "-" )
4145 .replace ("\u2014 " , "-" ))
46+ qn_math = re .sub (r"\b\d{4}[-/]\d{1,2}[-/]\d{1,2}\b" , " DATE " , qn )
47+ qn_math = re .sub (r"\b\d{1,2}[-/]\d{1,2}[-/]\d{2,4}\b" , " DATE " , qn_math )
4248 ql = qn .lower ()
4349 # heuristic: suggest calculator/now/final
44- if (re .search (r"[0-9].*[+\-*/%]" , qn ) or
50+ if (re .search (r"[0-9].*[+\-*/%]" , qn_math ) or
4551 re .search (r"\b\d+(?:\.\d+)?\s*(?:x|times|multiplied by|plus|minus|add|added to|subtract|subtracted by|divide|divided by|over)\s*\d+(?:\.\d+)?\b" , ql ) or
4652 (re .search (r"\d" , ql ) and any (w in ql for w in [
4753 "add" ,"sum" ,"plus" ,"minus" ,"subtract" ,"multiply" ,"divide" ,"total" ,"power" ,"factorial" ,"compute" ,"calculate" ,"!" ,"**" ,"^"
@@ -62,9 +68,13 @@ def __call__(self, *, prompt: str, **kwargs):
6268 m = re .search (r"\b(\d+(?:\.\d+)?)\s*(?:divide|divided by|over)\s*(\d+(?:\.\d+)?)\b" , ql )
6369 if m :
6470 expr = f"{ m .group (1 )} /{ m .group (2 )} "
71+ if expr is None and ("add" in ql or "sum" in ql ):
72+ nums = re .findall (r"\b\d+(?:\.\d+)?\b" , qn_math )
73+ if len (nums ) >= 2 :
74+ expr = "+" .join (nums )
6575 # crude expression extraction fallback
6676 if expr is None :
67- cands = re .findall (r"[0-9\+\-\*/%\(\)\.!\^\s]+" , qn )
77+ cands = re .findall (r"[0-9\+\-\*/%\(\)\.!\^\s]+" , qn_math )
6878 cands = [c .strip () for c in cands if c .strip ()]
6979 expr = max (cands , key = len ) if cands else "2+2"
7080 return _json .dumps ({"tool" : {"name" : "calculator" , "args" : {"expression" : expr }}})
0 commit comments