def clean_predicted_sequence(action_ids,
                             action_types,
                             scores,
                             vocab,
                             copy_strings,
                             segment_ids,
                             restore_preds_from_asql,
                             clean_vocab=None):
  """Cleans a set of predicted SQL queries."""
  copy_strings = [tok.decode('utf-8') for tok in copy_strings]
  action_id_to_table_name_map = None
  if restore_preds_from_asql:
    action_id_to_table_name_map = _action_id_to_table_name_map(
        segment_ids, copy_strings)
  string_seq = list()
  for action_type, action_id in zip(action_types, action_ids):
    if action_type == 1:
      # Generate symbol from output vocabulary.
      pred_idx = action_id
      if pred_idx == 1:
        # END symbol.
        break
      # Indices into vocab are offset by 3.
      symbol = vocab[pred_idx - 3]
      if clean_vocab:
        if symbol in clean_vocab:
          string_seq.append(symbol)
      else:
        string_seq.append(symbol)
    else:
      # Copy symbol from input.
      symbol = copy_strings[action_id]
      if restore_preds_from_asql and action_id in action_id_to_table_name_map:
        # For abstract SQL, need to fully qualify column names by prepending
        # the table name.
        table_name = action_id_to_table_name_map[action_id]
        symbol = '%s.%s' % (table_name, symbol)
      string_seq.append(symbol)

  sql = ''
  in_quote = False
  for i, token in enumerate(string_seq):
    if not in_quote and token not in QUOTES:
      sql += ' '
    if token in QUOTES:
      in_quote = not in_quote
    sql += token
    if not in_quote or token not in QUOTES and i < len(
        string_seq) - 1 and string_seq[i + 1] not in QUOTES:
      sql += ' '

  sql = sql.replace('  ', ' ')
  sql = sql.replace(' ##', '')
  sql = sqlparse.format(sql, reident=True, keyword_case='upper')
  sql = sql.replace('\n', ' ')
  sql = sql.replace('( ', '(')
  sql = sql.replace(' )', ')')
  sql = sql.replace(' . ', '.')
  sql = sql.replace(' %', '%')
  sql = sql.replace('% ', '%')

  for func in ('count', 'min', 'max', 'avg', 'sum'):
    sql = sql.replace('%s (' % func.upper(), '%s(' % func)

  for i in range(1, 11):
    sql = sql.replace('t%d' % i, 'T%d' % i)

  sql = sql.strip()

  return sql, float(scores)
Cleans a set of predicted SQL queries.
language/xsp/model/run_inference.py