o
    Ni                      @   s   d Z ddlmZ ddlmZ ddlmZ ddlZddlmZ ddlm	  m
Z ddlmZ dZdZd	Zg d
ZdgZg dZdZdZdZdd ZG dd dejjZdS )zMathematics database.    )absolute_import)division)print_functionN)loggingz
@article{2019arXiv,
  author = {Saxton, Grefenstette, Hill, Kohli},
  title = {Analysing Mathematical Reasoning Abilities of Neural Models},
  year = {2019},
  journal = {arXiv:1904.01557}
}
a  
Mathematics database.

This dataset code generates mathematical question and answer pairs,
from a range of question types at roughly school-level difficulty.
This is designed to test the mathematical learning and algebraic
reasoning skills of learning models.

Original paper: Analysing Mathematical Reasoning Abilities of Neural Models
(Saxton, Grefenstette, Hill, Kohli).

Example usage:

```
train_examples, val_examples = tfds.load(
    'math_dataset/arithmetic__mul',
    split=['train', 'test'],
    as_supervised=True)
```
zRhttps://storage.googleapis.com/mathematics-dataset/mathematics_dataset-v1.0.tar.gz)z
train-easyztrain-mediumz
train-hardinterpolate)qmeasurement__conversionalgebra__linear_1dalgebra__linear_1d_composedalgebra__linear_2dalgebra__linear_2d_composedalgebra__polynomial_roots"algebra__polynomial_roots_composedalgebra__sequence_next_termalgebra__sequence_nth_termarithmetic__add_or_subarithmetic__add_or_sub_in_basearithmetic__add_sub_multiplearithmetic__divarithmetic__mixedarithmetic__mularithmetic__mul_div_multiple arithmetic__nearest_integer_rootarithmetic__simplify_surdcalculus__differentiate calculus__differentiate_composedcomparison__closestcomparison__closest_composedcomparison__kth_biggest comparison__kth_biggest_composedcomparison__paircomparison__pair_composedcomparison__sortcomparison__sort_composedr   measurement__timenumbers__base_conversionnumbers__div_remaindernumbers__div_remainder_composednumbers__gcdnumbers__gcd_composednumbers__is_factornumbers__is_factor_composednumbers__is_primenumbers__is_prime_composednumbers__lcmnumbers__lcm_composednumbers__list_prime_factors$numbers__list_prime_factors_composednumbers__place_valuenumbers__place_value_composednumbers__round_numbernumbers__round_number_composedpolynomials__addpolynomials__coefficient_namedpolynomials__collectpolynomials__composepolynomials__evaluatepolynomials__evaluate_composedpolynomials__expandpolynomials__simplify_powerprobability__swr_p_level_setprobability__swr_p_sequencer   r	   r
   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r    r!   r"   r   r#   r$   r%   r&   r'   r(   r)   r*   r+   r,   r-   r.   r/   r0   r1   r2   r3   r4   r5   r6   r7   r8   r9   r:   r;   r<   r=   r>   questionanswerzmathematics_dataset-v1.0c                  C   s:   g } t ttD ]}| tjj|tjdtd q| S )z?Generate configs with different subsets of mathematics dataset.z1.0.0)nameversiondescription)	sortedset_MODULESappendtfdscoreBuilderConfigVersion_DESCRIPTION)configsmodule rO   Y/home/ubuntu/.local/lib/python3.10/site-packages/tensorflow_datasets/text/math_dataset.py_generate_builder_configs   s   
rQ   c                   @   s6   e Zd ZdZe Zdd Zdd Zdd Zdd	 Z	d
S )MathDatasetzMath Dataset.c              
   C   s8   t jj| tt jtt j tt j ittfdt	dS )Nz/https://github.com/deepmind/mathematics_dataset)builderrC   featuressupervised_keyshomepagecitation)
rH   rI   DatasetInforL   rT   FeaturesDict	_QUESTIONText_ANSWER	_CITATION)selfrO   rO   rP   _info   s   

zMathDataset._infoc           
   	   C   s   g }|D ]F}t j|t||}tjj|rJtjj|%}|	 
d}|d d d D ]	}	|	s5||	 q,|| W d    n1 sEw   Y  q|S )N
)ospathjoin_DATASET_VERSIONtfiogfileexistsGFilereadsplitremoveextend)
r^   	directoryconfig
categorieslinescategory	data_fileflslrO   rO   rP   _read_data_from_all_categories   s   
	z*MathDataset._read_data_from_all_categoriesc                 C   sN   | t}| jjd }tjjtjj||t	ddtjjtjj
||tddgS )zReturns SplitGenerators.z.txt)ro   rp   rq   )rA   
gen_kwargs)download_and_extract	_DATA_URLbuilder_configrA   rH   rI   SplitGeneratorSplitTRAIN_TRAIN_CATEGORYTEST_INTERPOLATE_CATEGORY)r^   
dl_managerro   rp   rO   rO   rP   _split_generators   s"   
zMathDataset._split_generatorsc                 c   s    |  |||}td||t| |ddd }|ddd }t|t|ks4J dt|t|f tt||D ]\}\}}	t|t|	i}
t|
	 rR||
fV  q;dS )z1Yields examples based on directory, module file..z%s: %s contains total: %dN      z&answers: %d do not match questions: %d)
rx   r   infolen	enumerateziprZ   r\   allvalues)r^   ro   rp   rq   rr   	questionsanswersidxqaresultrO   rO   rP   _generate_examples  s$   
zMathDataset._generate_examplesN)
__name__
__module____qualname____doc__rQ   BUILDER_CONFIGSr_   rx   r   r   rO   rO   rO   rP   rR      s    rR   )r   
__future__r   r   r   rb   abslr   tensorflow.compat.v2compatv2rf   tensorflow_datasets.public_api
public_apirH   r]   rL   r{   r   r   rF   rZ   r\   re   rQ   rI   GeneratorBasedBuilderrR   rO   rO   rO   rP   <module>   s(   	y