o
    6ti'                     @   sj  d dl Z d dlmZmZ d dlZzd dlZd dlZd dlZd dlm	Z	m
Z
mZ d dlmZ W n ey9   edw dZ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defddZded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)DictList)LatexExtractionConfigparseverify)parse_latexz`math-verify`, `sympy>=1.12`, and antlr4-python3-runtime==4.11 is required for generating translation task prompt templates. please install via pip install lm-eval[math] or pip install -e .[math]z[invalidanswer]docreturnc                 C   s   d| d  d d S )Nz	Problem:
problemz

z	Solution: )r   r   r   X/home/ubuntu/.local/lib/python3.10/site-packages/lm_eval/tasks/leaderboard/math/utils.pydoc_to_text   s   r   datasetc                 C   s&   dt dt fdd}| dd |S )Nr   r	   c                 S   s<   | d | d t t| d d}t| dd d urd|d< |S )Nr
   solution)r
   r   answerfew_shotT)remove_boxedlast_boxed_only_stringgetattr)r   out_docr   r   r   _process_doc   s   z"process_docs.<locals>._process_docc                 S   s   | d dkS )NlevelLevel 5r   )xr   r   r   <lambda>)   s    zprocess_docs.<locals>.<lambda>)dictfiltermap)r   r   r   r   r   process_docs   s   
r   c                   C   s4   dddd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.1r   )r
   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_samples,   s*   r    resultsc                 C   sb   |d }t |}t | d t gd}t||rd}nd}zt| |}W n   d}Y ||d}|S )Nr   r   )extraction_config   )exact_matchexact_match_original)r   r   r   process_result_v1)r   r!   
candidatesparsed_candidateparsed_answerretvaloriginaloutputr   r   r   process_resultsI   s   
r-   r'   c                 C   sR   t |}t|}t| d }|tkrdS | | ks!t||r%d}|S d}|S )Nr   r   r#   )get_unnormalized_answernormalize_final_answerINVALID_ANSWERstripis_equiv)r   r'   unnormalized_answerr   normalized_goldr*   r   r   r   r&   ^   s   r&   stringc                 C   s   |  d}d| v rd| dd dd  S |dk r&|  d}|dk r&t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_t}|S | ||d  }|S )
Nz\boxed\boxed $r   z\fbox{r#   })rfindsplitr0   len)r5   idxiright_brace_idxnum_left_braces_openr*   r   r   r   r   l   s2   


r   sc                 C   s   z:d| v rd}| d t | |ksJ | t |d  W S d}| d t | |ks*J | d dks2J | t |d W S  tyE   t Y S w )Nr6   z\boxed{r7   r:   )r=   AssertionErrorr0   )rB   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)selfrH   rI   r   r   r   __init__   s   
ztimeout.__init__c                 C   s
   t | jrG   )TimeoutErrorrI   )rJ   signumframer   r   r   handle_timeout   s   
ztimeout.handle_timeoutc                 C   s    t  t j| j t | j d S rG   )signalSIGALRMrO   alarmrH   )rJ   r   r   r   	__enter__   s   ztimeout.__enter__c                 C   s   t d d S )Nr   )rP   rR   )rJ   typevalue	tracebackr   r   r   __exit__   s   ztimeout.__exit__N)r#   rF   )__name__
__module____qualname__rK   rO   rS   rW   r   r   r   r   rE      s
    
rE   x1x2c                 C   s  t t}ztdd z
t| }t|}W n$ tjjjj	tj
tfy:   |d|  d|  Y W d   W dS w z|| }W n ty]   |d|  d|  Y W d   W dS w zt|d	krpW W d   W d
S W W d   W dS  ty   |d|  d|  Y nw W d   W dS 1 sw   Y  W dS  ty   |d|  d|  Y dS  ty } z||  d}~w ty } z|d|  d| d|  W Y d}~dS d}~ww )z/
    x1 and x2 are normalized latex string
    r#   )rH   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 )logging	getLoggerrX   rE   r   sympyparsinglatexerrorsLaTeXParsingErrorSympifyError	TypeErrordebugsimplify
ValueErrorrL   ImportErrorerror	Exception)r[   r\   eval_logger	parsed_x1	parsed_x2differ   r   r   r2      s^   


&
r2   textc                 C   s.   d}| |7 } t d| }|r|d S tS )NzI hope it is correct.z=Final Answer: The final answer is(.*?). I hope it is correct.r#   )researchgroupr1   r0   )rq   end_seqmatchr   r   r   r.      s   r.   )
)zan  )za rw   )z.$r8   )z\$rw   )z\ rw   ) rw   )mboxrq   )z,\text{and},)z
\text{and}rz   )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{
}r{   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)
    =r7   rw   z(.*?)(\$)(.*?)(\$)(.*)z$\3$z(\\text\{)(.*?)(\})z\2z(\\textbf\{)(.*?)(\})z(\\overline\{)(.*?)(\})z(\\boxed\{)(.*)(\})z(frac)([^{])(.)zfrac{\2}{\3}z(sqrt)([^{])zsqrt{\2}r8   rz   )r<   SUBSTITUTIONSreplaceREMOVED_EXPRESSIONSrr   subisdigit)r   beforeafterexprr   r   r   r/     s    r/   )#r]   typingr   r   datasetsrr   rP   r_   math_verifyr   r   r   sympy.parsing.latexr   ModuleNotFoundErrorr0   r   strr   Datasetr   listr    intr-   r&   r   r   rE   boolr2   r.   r   r   r/   r   r   r   r   <module>   s8    ",.