def main(unused_argv):
  """Induce and write set of rules."""
  examples = tsv_utils.read_tsv(FLAGS.input)
  config = json_utils.json_file_to_dict(FLAGS.config)
  if not config.get("allow_duplicate_examples", True):
    examples = set([tuple(ex) for ex in examples])
  examples = sorted(examples, key=lambda e: (len(e[0]), e))

  seed_rules = set()
  # Add mannual seed rules.
  if FLAGS.seed_rules_file:
    for seed_rules_file in FLAGS.seed_rules_file:
      seed_rules |= set(qcfg_file.read_rules(seed_rules_file))

  target_grammar_rules = (
      target_grammar.load_rules_from_file(FLAGS.target_grammar)
      if FLAGS.target_grammar else None)

  num_partitions = config.get("num_partitions", 1)
  partition_to_examples = np.array_split(examples, num_partitions)
  induction_state = induction_utils.InductionState(FLAGS.output, config)
  if FLAGS.restore_partition or FLAGS.restore_step:
    # Restore from an existing induction state.
    induction_state.restore_state(FLAGS.restore_partition, FLAGS.restore_step)
  else:
    # Initialize the induction state with manual seed rules.
    induction_state.current_rules = seed_rules.copy()

  while induction_state.current_partition < num_partitions:
    current_examples = induction_utils.get_examples_up_to_partition(
        partition_to_examples, induction_state.current_partition)
    logging.info("Partition: %s, number of examples: %s.",
                 induction_state.current_partition, len(current_examples))
    # At the first step of each partition, we add a rule corresponding to
    # each example in the partition.
    if induction_state.current_step_in_partition == 0:
      induction_state.current_rules |= induction_utils.get_example_rules(
          partition_to_examples[induction_state.current_partition])
    policy = greedy_policy.GreedyPolicy(
        config,
        current_examples,
        seed_rules,
        target_grammar_rules,
        verbose=FLAGS.verbose)
    induce_rules_for_partition(policy, induction_state)
    induction_state.current_partition += 1
    induction_state.current_step_in_partition = 0
  qcfg_file.write_rules(induction_state.current_rules, FLAGS.output)
Induce and write set of rules.
language/compgen/csl/induction/search_main.py