o
    7ti%                  
   @   s  d dl Z d dlZd dlZd dlmZ d dlmZmZmZ d dl	Z	e 
eZz!d dlZd dlZd dlmZmZ d dlmZ eddsEJ W n eefy[ Z zeededZ[ww d	ed
efddZde	jd
e	jfddZd
ee fddZd	edee d
eee f fddZ!ded
ee fddZ"ded
efddZ#G dd dZ$deded
e%fdd Z&d!ed
efd"d#Z'g d$Z(g d%Z)d&ed
efd'd(Z*dS ))    N)version)DictListOptional)parseverify)parse_latexzantlr4-python3-runtimez4.11z`sympy`, `math_verify` and `antlr4-python3-runtime==4.11` are required for generating translation task prompt templates. Please install the required packages via pip install lm-eval[math] or pip install -e .[math]docreturnc                 C   s   d| d  d d S )Nz	Problem:
problemz

z	Solution: )r	   r   r   T/home/ubuntu/.local/lib/python3.10/site-packages/lm_eval/tasks/minerva_math/utils.pydoc_to_text   s   r   datasetc                 C   s   dt dt fdd}| |S )Nr	   r
   c                 S   s@   | d | d t tt| d d}t| dd d urd|d< |S )Nr   solution)r   r   answerfew_shotT)normalize_final_answerremove_boxedlast_boxed_only_stringgetattr)r	   out_docr   r   r   _process_doc"   s   z"process_docs.<locals>._process_doc)dictmap)r   r   r   r   r   process_docs!   s   
r   c                   C   s,   ddddddddddddd	d
ddgS )NzDFind the domain of the expression  $\frac{\sqrt{x-2}}{\sqrt{5-x}}$.}aM  The expressions inside each square root must be non-negative. Therefore, $x-2 \ge 0$, so $x\ge2$, and $5 - x \ge 0$, so $x \le 5$. Also, the denominator cannot be equal to zero, so $5-x>0$, which gives $x<5$. Therefore, the domain of the expression is $\boxed{[2,5)}$.
Final Answer: The final answer is $[2,5)$. I hope it is correct.1)r   r   r   z^If $\det \mathbf{A} = 2$ and $\det \mathbf{B} = 12,$ then find $\det (\mathbf{A} \mathbf{B}).$zWe have that $\det (\mathbf{A} \mathbf{B}) = (\det \mathbf{A})(\det \mathbf{B}) = (2)(12) = \boxed{24}.$
Final Answer: The final answer is $24$. I hope it is correct.zTerrell usually lifts two 20-pound weights 12 times. If he uses two 15-pound weights instead, how many times must Terrell lift them in order to lift the same total weight?a  If Terrell lifts two 20-pound weights 12 times, he lifts a total of $2\cdot 12\cdot20=480$ pounds of weight.  If he lifts two 15-pound weights instead for $n$ times, he will lift a total of $2\cdot15\cdot n=30n$ pounds of weight.  Equating this to 480 pounds, we can solve for $n$:
\begin{align*}
30n&=480\
\Rightarrow\qquad n&=480/30=\boxed{16}
\end{align*}
Final Answer: The final answer is $16$. I hope it is correct.zIf the system of equations

\begin{align*}
6x-4y&=a,\
6y-9x &=b.
\end{align*}has a solution $(x, y)$ where $x$ and $y$ are both nonzero,
find $\frac{a}{b},$ assuming $b$ is nonzero.a  If we multiply the first equation by $-\frac{3}{2}$, we obtain

$$6y-9x=-\frac{3}{2}a.$$Since we also know that $6y-9x=b$, we have

$$-\frac{3}{2}a=b\Rightarrow\frac{a}{b}=\boxed{-\frac{2}{3}}.$$
Final Answer: The final answer is $-\frac{2}{3}$. I hope it is correct.r   r   r   r   r   list_fewshot_samples1   s"   r   resultsc           	      C   sb   |d }t |}t|}t|| d rd}nd}tt| d t|d}|r(dnd}||d}|S )Nr   r      r   )goldtarget)exact_matchmath_verify)get_unnormalized_answerr   is_equivr   r   )	r	   r   
candidatesunnormalized_answerr   retval_mvresmathvalresr   r   r   process_resultsJ   s   
r,   stringc                 C   s   |  d}d| v rd| dd dd  S |dk r&|  d}|dk r&d S |}d }d}|t| k rW| | dkr<|d7 }| | d	krM|d8 }|dkrM|}n
|d7 }|t| k s2|d u r_d }|S | ||d  }|S )
Nz\boxed\boxed $r   z\fbox{r   })rfindsplitlen)r-   idxiright_brace_idxnum_left_braces_openr(   r   r   r   r   c   s2   


r   sc                 C   sp   d| v rd}| d t | |ksJ | t |d  S d}| d t | |ks(J | d dks0J | t |d S )Nr.   z\boxed{r/   r2   )r5   )r:   leftr   r   r   r      s   r   c                   @   s.   e Zd ZdddZdd Zdd Zd	d
 ZdS )timeoutr   Timeoutc                 C   s   || _ || _d S N)secondserror_message)selfr?   r@   r   r   r   __init__   s   
ztimeout.__init__c                 C   s
   t | jr>   )TimeoutErrorr@   )rA   signumframer   r   r   handle_timeout   s   
ztimeout.handle_timeoutc                 C   s    t  t j| j t | j d S r>   )signalSIGALRMrF   alarmr?   )rA   r   r   r   	__enter__   s   ztimeout.__enter__c                 C   s   t d d S )Nr   )rG   rI   )rA   typevalue	tracebackr   r   r   __exit__   s   ztimeout.__exit__N)r   r=   )__name__
__module____qualname__rB   rF   rJ   rN   r   r   r   r   r<      s
    
r<   x1x2c                 C   s  zt dd z
t| }t|}W n$ tjjjjtjtfy5   t	
d|  d|  Y W d   W dS w z|| }W n tyX   t	
d|  d|  Y W d   W dS w zt|d	krkW W d   W d
S W W d   W dS  ty   t	
d|  d|  Y nw W d   W dS 1 sw   Y  W dS  ty   t	
d|  d|  Y dS  ty } zt	|  d}~w ty } zt	
d|  d| d|  W Y d}~dS d}~ww )z/
    x1 and x2 are normalized latex string
       )r?   zcouldn't parse one of z or NFzcouldn't subtract z and r   Tz,Had some trouble simplifying when comparing zTimed out comparing zFailed comparing z with )r<   r   sympyparsinglatexerrorsLaTeXParsingErrorSympifyError	TypeErroreval_loggerdebugsimplify
ValueErrorrC   ImportErrorerror	Exception)rR   rS   	parsed_x1	parsed_x2differ   r   r   r%      s\   

&
r%   textc                 C   s2   d}d}| |7 } t d| }|r|d S |S )Nz[invalidanswer]zI hope it is correct.z=Final Answer: The final answer is(.*?). I hope it is correct.r   )researchgroupstrip)rg   INVALID_ANSWERend_seqmatchr   r   r   r$      s   r$   )
)zan  )za ro   )z.$r0   )z\$ro   )z\ ro   ) ro   )mboxrg   )z,\text{and},)z
\text{and}rr   )z\text{m}\text{})*squarewaysintegersdollarsmphinchesfthourskmunitsz\ldotssuepointsfeetminutesdigitscentsdegreescmgmpoundsmetersmealsedgesstudentschildrentickets	multiplesz\text{s}z\text{.}z	\text{
s}z	\text{}^2z	\text{}^3z\text{
}rs   z\mathrm{th}z^\circz^{\circ}z\;z,\!z{,}"z\dotsfinal_answerc                 C   s   |  dd } tD ]
\}}| ||} q	tD ]}| |d} qtdd| } tdd| } tdd| } td	d| } td
d| } tdd| } tdd| } | dd} | dd rd| dd} | S )z
    Normalize a final answer to a quantitative reasoning question.

    Copied character for character from appendix D of Lewkowycz et al. (2022)
    =r/   ro   z(.*?)(\$)(.*?)(\$)(.*)z$\3$z(\\text\{)(.*?)(\})z\2z(\\textbf\{)(.*?)(\})z(\\overline\{)(.*?)(\})z(\\boxed\{)(.*)(\})z(frac)([^{])(.)zfrac{\2}{\3}z(sqrt)([^{])zsqrt{\2}r0   rr   )r4   SUBSTITUTIONSreplaceREMOVED_EXPRESSIONSrh   subisdigit)r   beforeafterexprr   r   r   r     s    r   )+loggingrh   rG   importlib.metadatar   typingr   r   r   datasets	getLoggerrO   r\   antlr4rU   r#   r   r   sympy.parsing.latexr   
startswithModuleNotFoundErrorAssertionErrorrf   rK   r   strr   Datasetr   listr   intr,   r   r   r<   boolr%   r$   r   r   r   r   r   r   r   <module>   sB    
	"+.