(  # pylint: disable=invalid-name
    dataset_path=None,
    train=True,
    test=False,
    challenge=False,
    tolerance=0.01,
    cumulative=True,
    python_code=False,
    full_dict=False,
    partial_results=True,
    nlp_rationale=False,
    correct_answer=False,
    answer_in_mathqa_format=True,
    correct_answer_given_reasoning=False,
    category=False,
    order_prediction=False,
    reduced_operation_name=True,
    qed=False)
return mathqa_yield_examples
Prepares MathQA inputs.

  The generation procedure leaves a lot parameters to be set by the user.
  Currently we support only correct examples in the following sense:
  python execution agrees with the declared answer up to 1%.

  According to this criterion wrong examples such as
  problem: calculate 85184 ÷ ? = 352
  operations ['multiply(n0,n1)']
  are ignored (this should be divide(n0,n1) in this case).

  Args:
    dataset_path: a path with the MathQA dataset.
    train: if True, then generate training examples; if train, test and
      challenge are set to False generate validation examples.
    test: if train is set to False and test is set to True,
      then generate test examples.
    challenge: if train and test are set to False and challenge is set to True,
      then generate challenge examples.
    tolerance: if for a given example relative difference between Python result
      and the result declared in the dataset exceeds the level, then the example
      is dropped; tolerances ranging from 0.1 to 0.001 yield from 18K to 21K
      examples.
    cumulative: if set to True, then generate examples in the format input -
      problem + numbers + op1 + op2 + op3 target - op4 If set to False, then
      examples are in the format input - problem + numbers target - all
      operations.
    python_code: if set to True, then generates python code instead of
      MathQA commands.
    full_dict: if set to True, then Python examples are returned together with
      the DSL code and the NLP rationale.
    partial_results: if set to True, then partial results will be reported as
      part of the input, e.g. input - problem + numbers + op1 + #1 + op2 + #2 +
      op3 + #3, target - op4, where #k is the partial results from operation
      opk. Activated only in cumulative set to True.
    nlp_rationale: if set to True, then input is the problem and the target is
      the nlp rationale.
    correct_answer: if set to True, then input is the problem plus all possible
      answers and the target is the correct answer.
    answer_in_mathqa_format: if set to True, then convert numerical answer to
      the MathQA format and wrap it in the subtract operation.
      E.g. "3.13" is converted to "subtract(const_3_13,const_0)".
    correct_answer_given_reasoning: if set to True, then input is the problem
      plus linear formula plus all possible answers and the target is the
      correct answer.
    category: if set to True, then input is the problem and the target is its
      category.
    order_prediction: if set to True, then input is the problem and a list of
      all operations; with probability 0.5 two operations are swapped; the task
      consists in detecting whether the operations were swapped. See the
      order prediction task in CreateAquaInputs in this file.
    reduced_operation_name: If set to True, then in order prediction consider
      only the operation token without parameterers.
    qed: if set to True, then the reasoning is finished with an additional
      operation qed.

  Returns:
    mathqa_yield_examples: a generator of MathQA examples; the generator yields
    non-tokenized examples - they can be further processed using for example
    the tokenize function from this module
Prepares MathQA inputs.
Prepares MathQA inputs .
def CreateMathQAInputs(  # pylint: disable=invalid-name
    dataset_path=None,
    train=True,
    test=False,
    challenge=False,
    tolerance=0.01,
    cumulative=True,
    python_code=False,
    full_dict=False,
    partial_results=True,
    nlp_rationale=False,
    correct_answer=False,
    answer_in_mathqa_format=True,
    correct_answer_given_reasoning=False,
    category=False,
    order_prediction=False,
    reduced_operation_name=True,
    qed=False):
  """Prepares MathQA inputs.

  The generation procedure leaves a lot parameters to be set by the user.
  Currently we support only correct examples in the following sense:
  python execution agrees with the declared answer up to 1%.

  According to this criterion wrong examples such as
  problem: calculate 85184 ÷ ? = 352
  operations ['multiply(n0,n1)']
  are ignored (this should be divide(n0,n1) in this case).

  Args:
    dataset_path: a path with the MathQA dataset.
    train: if True, then generate training examples; if train, test and
      challenge are set to False generate validation examples.
    test: if train is set to False and test is set to True,
      then generate test examples.
    challenge: if train and test are set to False and challenge is set to True,
      then generate challenge examples.
    tolerance: if for a given example relative difference between Python result
      and the result declared in the dataset exceeds the level, then the example
      is dropped; tolerances ranging from 0.1 to 0.001 yield from 18K to 21K
      examples.
    cumulative: if set to True, then generate examples in the format input -
      problem + numbers + op1 + op2 + op3 target - op4 If set to False, then
      examples are in the format input - problem + numbers target - all
      operations.
    python_code: if set to True, then generates python code instead of
      MathQA commands.
    full_dict: if set to True, then Python examples are returned together with
      the DSL code and the NLP rationale.
    partial_results: if set to True, then partial results will be reported as
      part of the input, e.g. input - problem + numbers + op1 + #1 + op2 + #2 +
      op3 + #3, target - op4, where #k is the partial results from operation
      opk. Activated only in cumulative set to True.
    nlp_rationale: if set to True, then input is the problem and the target is
      the nlp rationale.
    correct_answer: if set to True, then input is the problem plus all possible
      answers and the target is the correct answer.
    answer_in_mathqa_format: if set to True, then convert numerical answer to
      the MathQA format and wrap it in the subtract operation.
      E.g. "3.13" is converted to "subtract(const_3_13,const_0)".
    correct_answer_given_reasoning: if set to True, then input is the problem
      plus linear formula plus all possible answers and the target is the
      correct answer.
    category: if set to True, then input is the problem and the target is its
      category.
    order_prediction: if set to True, then input is the problem and a list of
      all operations; with probability 0.5 two operations are swapped; the task
      consists in detecting whether the operations were swapped. See the
      order prediction task in CreateAquaInputs in this file.
    reduced_operation_name: If set to True, then in order prediction consider
      only the operation token without parameterers.
    qed: if set to True, then the reasoning is finished with an additional
      operation qed.

  Returns:
    mathqa_yield_examples: a generator of MathQA examples; the generator yields
    non-tokenized examples - they can be further processed using for example
    the tokenize function from this module
  """
  if train:
    dataset_path = os.path.join(dataset_path, 'train.json')
  elif test:
    dataset_path = os.path.join(dataset_path, 'test.json')
  elif challenge:
    dataset_path = os.path.join(dataset_path, 'challenge_test.json')
  else:
    dataset_path = os.path.join(dataset_path, 'dev.json')
  # Opening with GFile allows to use remotely stored files, e.g.
  # in a gs bucket.
  dataset_handle = tf.io.gfile.GFile(dataset_path, 'r')
  dataset = json.load(dataset_handle)

  def mathqa_yield_examples(generator=None):
    del generator
    while True:
      for example in itertools.cycle(dataset):
        result = process_single_mathqa_example(example)
        # TODO(henrykm): Remove the first two ifs.
        if not result:
          continue
        answer_num, python_result, python_program, list_op, list_num = result
        if not answer_num or not python_result[-1]:
          continue
        if qed:
          list_op.append('qed')
        if math.isclose(answer_num, python_result[-1], rel_tol=tolerance):
          input_prefix = example['Problem']
          for i in range(len(list_num)):
            input_prefix += ' n{} = {}'.format(i, list_num[i])
          if cumulative:
            for i in range(len(list_op)):
              input_values = input_prefix
              target_values = list_op[i]
              input_prefix += ' ' + list_op[i]
              if partial_results:
                input_prefix += ' #{} = {}'.format(i, answer_num)
              yield input_values, target_values, np.array([1] *
                                                          len(target_values))
          elif python_code:
            input_values = '# ' + input_prefix
            target_values = ''
            for command in python_program:
              if 'math' in command:
                target_values += 'import math\n'
                break
            for command in python_program:
              if 'scipy' in command:
                target_values += 'import scipy\n'
                break
            for i in range(len(list_num)):
              target_values += 'n{} = {}\n'.format(i, list_num[i])
            target_values += '\n'.join(python_program[:-1])
            final_line = python_program[-1].split('=')[1]
            target_values += '\nanswer ={}'.format(final_line)
            var_dict = {}
            # We generate a python code and want to check whether the answer
            # is coorect.
            exec(target_values, globals(), var_dict)  # pylint: disable=exec-used
            if math.isclose(answer_num, var_dict['answer'], rel_tol=tolerance):
              if full_dict:
                yield input_values, target_values, example[
                    'linear_formula'], example['Rationale']
              else:
                yield input_values, target_values, np.array([1] *
                                                            len(target_values))
          elif nlp_rationale:
            input_values = 'infer full rationale: ' + input_prefix
            target_values = example['Rationale']
            yield input_values, target_values, np.array([1] *
                                                        len(target_values))
          elif correct_answer:
            input_values = 'infer correct answer: ' + input_prefix
            input_values += ' ' + example['options']
            if answer_in_mathqa_format:
              target_values = str(answer_num)
              target_values = convert_to_subtract(
                  convert_float_to_mathqa(target_values))
            else:
              target_values = example['correct']
            yield input_values, target_values, np.array([1] *
                                                        len(target_values))
          elif correct_answer_given_reasoning:
            input_values = 'infer correct answer given reasoning: ' + input_prefix
            input_values += ' ' + ' '.join(list_op) + ' ' + example['options']
            target_values = example['correct']
            yield input_values, target_values, np.array([1] *
                                                        len(target_values))
          elif category:
            input_values = 'infer category: ' + input_prefix
            target_values = example['category']
            yield input_values, target_values, np.array([1] *
                                                        len(target_values))
          elif order_prediction:
            if np.random.uniform() < 0.5 and len(list_op) >= 2:
              idx = range(len(list_op))
              i1, i2 = random.sample(idx, 2)
              list_op[i1], list_op[i2] = list_op[i2], list_op[i1]
              target_values = 'not_ordered'
            else:
              target_values = 'ordered'
            if reduced_operation_name:
              list_op = [op.split('(')[0] for op in list_op]
            input_values = 'order prediction: ' + input_prefix + ' ' + ' '.join(
                list_op)
            yield input_values, target_values, np.array([1] *
                                                        len(target_values))
          else:
            input_values = 'infer full calculation: ' + input_prefix
            target_values = example['linear_formula']
            yield input_values, target_values, np.array([1] *
                                                        len(target_values))

  return mathqa_yield_examples
def CreateMathQAInputs ( # pylint: disable=invalid-name dataset_path = None , train = True , test = False , challenge = False , tolerance = 0.01 , cumulative = True , python_code = False , full_dict = False , partial_results = True , nlp_rationale = False , correct_answer = False , answer_in_mathqa_format = True , correct_answer_given_reasoning = False , category = False , order_prediction = False , reduced_operation_name = True , qed = False ) : if train : dataset_path = os . path . join ( dataset_path , 'train.json' ) elif test : dataset_path = os . path . join ( dataset_path , 'test.json' ) elif challenge : dataset_path = os . path . join ( dataset_path , 'challenge_test.json' ) else : dataset_path = os . path . join ( dataset_path , 'dev.json' ) # Opening with GFile allows to use remotely stored files, e.g. # in a gs bucket. dataset_handle = tf . io . gfile . GFile ( dataset_path , 'r' ) dataset = json . load ( dataset_handle ) def mathqa_yield_examples ( generator = None ) : del generator while True : for example in itertools . cycle ( dataset ) : result = process_single_mathqa_example ( example ) # TODO(henrykm): Remove the first two ifs. if not result : continue answer_num , python_result , python_program , list_op , list_num = result if not answer_num or not python_result [ - 1 ] : continue if qed : list_op . append ( 'qed' ) if math . isclose ( answer_num , python_result [ - 1 ] , rel_tol = tolerance ) : input_prefix = example [ 'Problem' ] for i in range ( len ( list_num ) ) : input_prefix += ' n{} = {}' . format ( i , list_num [ i ] ) if cumulative : for i in range ( len ( list_op ) ) : input_values = input_prefix target_values = list_op [ i ] input_prefix += ' ' + list_op [ i ] if partial_results : input_prefix += ' #{} = {}' . format ( i , answer_num ) yield input_values , target_values , np . array ( [ 1 ] * len ( target_values ) ) elif python_code : input_values = '# ' + input_prefix target_values = '' for command in python_program : if 'math' in command : target_values += 'import math\n' break for command in python_program : if 'scipy' in command : target_values += 'import scipy\n' break for i in range ( len ( list_num ) ) : target_values += 'n{} = {}\n' . format ( i , list_num [ i ] ) target_values += '\n' . join ( python_program [ : - 1 ] ) final_line = python_program [ - 1 ] . split ( '=' ) [ 1 ] target_values += '\nanswer ={}' . format ( final_line ) var_dict = { } # We generate a python code and want to check whether the answer # is coorect. exec ( target_values , globals ( ) , var_dict ) # pylint: disable=exec-used if math . isclose ( answer_num , var_dict [ 'answer' ] , rel_tol = tolerance ) : if full_dict : yield input_values , target_values , example [ 'linear_formula' ] , example [ 'Rationale' ] else : yield input_values , target_values , np . array ( [ 1 ] * len ( target_values ) ) elif nlp_rationale : input_values = 'infer full rationale: ' + input_prefix target_values = example [ 'Rationale' ] yield input_values , target_values , np . array ( [ 1 ] * len ( target_values ) ) elif correct_answer : input_values = 'infer correct answer: ' + input_prefix input_values += ' ' + example [ 'options' ] if answer_in_mathqa_format : target_values = str ( answer_num ) target_values = convert_to_subtract ( convert_float_to_mathqa ( target_values ) ) else : target_values = example [ 'correct' ] yield input_values , target_values , np . array ( [ 1 ] * len ( target_values ) ) elif correct_answer_given_reasoning : input_values = 'infer correct answer given reasoning: ' + input_prefix input_values += ' ' + ' ' . join ( list_op ) + ' ' + example [ 'options' ] target_values = example [ 'correct' ] yield input_values , target_values , np . array ( [ 1 ] * len ( target_values ) ) elif category : input_values = 'infer category: ' + input_prefix target_values = example [ 'category' ] yield input_values , target_values , np . array ( [ 1 ] * len ( target_values ) ) elif order_prediction : if np . random . uniform ( ) < 0.5 and len ( list_op ) >= 2 : idx = range ( len ( list_op ) ) i1 , i2 = random . sample ( idx , 2 ) list_op [ i1 ] , list_op [ i2 ] = list_op [ i2 ] , list_op [ i1 ] target_values = 'not_ordered' else : target_values = 'ordered' if reduced_operation_name : list_op = [ op . split ( '(' ) [ 0 ] for op in list_op ] input_values = 'order prediction: ' + input_prefix + ' ' + ' ' . join ( list_op ) yield input_values , target_values , np . array ( [ 1 ] * len ( target_values ) ) else : input_values = 'infer full calculation: ' + input_prefix target_values = example [ 'linear_formula' ] yield input_values , target_values , np . array ( [ 1 ] * len ( target_values ) ) return mathqa_yield_examples