def _conversion_decimal(context, is_train, is_extrapolation):
    """E.g., "How many grams are in 5kg?"."""
    dimension = random.choice(DIMENSIONS)
    while True:
        base_value, base_unit, target_value, target_unit = (
            _sample_conversion_decimal(dimension, is_extrapolation))
        if train_test_split.is_train(base_value) == is_train:
            break

    templates = [
        'Ada berapa {target_name} di {base_value} {base_name}?',
        'Apa {base_value} {base_name} dalam {target_name}?',
        'Ubah {base_value} {base_name} to {target_name}.',
    ]
    if base_unit.symbol is not None:
        templates += [
            'Ada berapa {target_name} di {base_value}{base_symbol}?',
            'Apa {base_value}{base_symbol} dalam {target_name}?',
            'Ubah {base_value}{base_symbol} ke {target_name}.',
        ]
    template = random.choice(templates)

    base_name = base_unit.name  # Indonesia no have plural stuff
    target_name = target_unit.name  # Indonesia no have plural stuff

    question = example.question(context,
                                template,
                                base_name=base_name,
                                base_symbol=base_unit.symbol,
                                base_value=base_value,
                                target_name=target_name)
    return example.Problem(question=question, answer=target_value)
def _conversion_decimal(context, is_train, is_extrapolation):
  """E.g., "How many grams are in 5kg?"."""
  dimension = random.choice(DIMENSIONS)
  while True:
    base_value, base_unit, target_value, target_unit = (
        _sample_conversion_decimal(dimension, is_extrapolation))
    if train_test_split.is_train(base_value) == is_train:
      break

  templates = [
      'How many {target_name} are there in {base_value} {base_name}?',
      'What is {base_value} {base_name} in {target_name}?',
      'Convert {base_value} {base_name} to {target_name}.',
  ]
  if base_unit.symbol is not None:
    templates += [
        'How many {target_name} are there in {base_value}{base_symbol}?',
        'What is {base_value}{base_symbol} in {target_name}?',
        'Convert {base_value}{base_symbol} to {target_name}.',
    ]
  template = random.choice(templates)

  base_name = pluralize(base_unit.name)
  target_name = pluralize(target_unit.name)

  question = example.question(
      context,
      template,
      base_name=base_name,
      base_symbol=base_unit.symbol,
      base_value=base_value,
      target_name=target_name)
  return example.Problem(question=question, answer=target_value)
def time(is_train):
    """Questions for calculating start, end, or time differences."""
    context = composition.Context()
    start_minutes = random.randint(1, 24 * 60 - 1)
    while True:
        duration_minutes = random.randint(1, 12 * 60 - 1)
        if train_test_split.is_train(duration_minutes) == is_train:
            break
    end_minutes = start_minutes + duration_minutes

    def format_12hr(minutes):
        """Format minutes from midnight in 12 hr format."""
        hours = (minutes // 60) % 24
        minutes %= 60
        am_pm = 'AM' if hours < 12 else 'PM'
        hours = (hours - 1) % 12 + 1
        return '{}:{:02} {}'.format(hours, minutes, am_pm)

    start = format_12hr(start_minutes)
    end = format_12hr(end_minutes)

    which_question = random.randint(0, 3)
    if which_question == 0:
        # Question: What is start = end - duration?
        template = random.choice([
            'Berapa {duration} menit sebelum {end}?',
        ])
        return example.Problem(question=example.question(
            context, template, duration=duration_minutes, end=end),
                               answer=start)
    elif which_question == 1:
        # Question: What is end = start + duration?
        template = random.choice([
            'Berapa {duration} menit setelah {start}?',
        ])
        return example.Problem(question=example.question(
            context, template, duration=duration_minutes, start=start),
                               answer=end)
    else:
        # Question: What is duration = end - start?
        template = random.choice([
            'Berapa menit antara {start} dan {end}?',
        ])
        return example.Problem(question=example.question(context,
                                                         template,
                                                         start=start,
                                                         end=end),
                               answer=duration_minutes)
Пример #4
0
def _sample_letter_bag(is_train, min_total):
    """Samples a "container of letters" and returns info on it."""
    while True:
        num_distinct_letters = random.randint(1, _MAX_DISTINCT_LETTERS)
        num_letters_total = random.randint(
            max(num_distinct_letters, min_total),
            min(_MAX_TOTAL_LETTERS, num_distinct_letters * _MAX_LETTER_REPEAT))
        letter_counts = combinatorics.uniform_positive_integers_with_sum(
            num_distinct_letters, num_letters_total)

        # Test/train split.
        if (is_train is None or train_test_split.is_train(
                sorted(letter_counts)) == is_train):
            break

    letters_distinct = random.sample(_LETTERS, num_distinct_letters)
    weights = {i: 1 for i in range(num_letters_total)}

    letters_with_repetition = []
    for letter, count in zip(letters_distinct, letter_counts):
        letters_with_repetition += [letter] * count
    random.shuffle(letters_with_repetition)

    random_variable = probability.DiscreteRandomVariable(
        {i: letter
         for i, letter in enumerate(letters_with_repetition)})

    if random.choice([False, True]):
        bag_contents = ''.join(letters_with_repetition)
    else:
        letters_and_counts = [
            '{}: {}'.format(letter, count)
            for letter, count in zip(letters_distinct, letter_counts)
        ]
        bag_contents = '{' + ', '.join(letters_and_counts) + '}'

    return LetterBag(weights=weights,
                     random_variable=random_variable,
                     letters_distinct=letters_distinct,
                     bag_contents=bag_contents)
def _conversion_fraction(context, is_train):
    """E.g., "How many grams are in three quarters of a kg?"."""
    dimension = random.choice(DIMENSIONS)

    # Limit probability of giving zero answer.
    allow_zero = random.random() < 0.2

    # Repeat until we find a pair with an integral answer. (Avoids ambiguity with
    # decimals.)
    while True:
        base_unit, target_unit = random.sample(list(dimension.keys()), 2)
        base_value = number.non_integer_rational(2, signed=False)
        if train_test_split.is_train(base_value) != is_train:
            continue
        answer = (base_value * sympy.Rational(dimension[base_unit]) /
                  sympy.Rational(dimension[target_unit]))
        if (abs(answer) <= 100000 and sympy.denom(answer) == 1
                and (allow_zero or answer != 0)):
            break

    template = random.choice([
        'Ada berapa {target_name} di {base_value} dari sebuah {base_name}?',
        'Berapa {base_value} dari {base_name} di {target_name}?',
    ])

    if sympy.denom(base_value) > 20 or random.choice([False, True]):
        base_value_string = base_value  # Will be represented as e.g., 2/3.
    else:
        base_value_string = display.StringNumber(
            base_value)  # e.g., two thirds

    question = example.question(context,
                                template,
                                base_name=base_unit.name,
                                base_value=base_value_string,
                                target_name=target_unit.name)
    return example.Problem(question=question, answer=answer)