def _add_value_literal(item_str, example, anonymize):
  """Adds a value action to the output."""

  if anonymize:
    example.gold_sql_query.actions.append(SQLAction(symbol='"'))
    example.gold_sql_query.actions.append(SQLAction(symbol='value'))
    example.gold_sql_query.actions.append(SQLAction(symbol='"'))
    return True

  # Add quotes if [1] there aren't quotes and [2] it's not numeric
  if not item_str.replace(
      '.', '',
      1).isdigit() and item_str.count('"') < 2 and item_str.count('\'') < 2:
    item_str = "'" + item_str + "'"

  def find_and_add_copy_from_text(substr):
    """Finds a substring in the utterance and adds a copying action."""
    # It's a substring of the original utterance, but not so sure it could be
    # composed of wordpieces.
    found = False
    start_wordpiece = -1
    end_wordpiece = -1
    for i in range(len(example.model_input.utterance_wordpieces)):
      for j in range(i + 1, len(example.model_input.utterance_wordpieces) + 1):
        # Compose a string. If it has ##, that means it's a wordpiece, so should
        # not have a space in front.
        composed_pieces = ' '.join([
            wordpiece.wordpiece
            for wordpiece in example.model_input.utterance_wordpieces[i:j]
        ]).replace(' ##', '')
        if substr.lower() == composed_pieces:
          start_wordpiece = i
          end_wordpiece = j

          found = True
          break
      if found:
        break

    if start_wordpiece >= 0 and end_wordpiece >= 0:
      # Found wordpiece(s, when put together) comprising this item
      for i in range(start_wordpiece, end_wordpiece):
        action = SQLAction(
            utterance_copy=example.model_input.utterance_wordpieces[i])
        example.gold_sql_query.actions.append(action)
      return True
    return False

  # First, check if this string could be copied from the wordpiece-tokenized
  # inputs.
  quote_type = ''
  if item_str.lower() in example.model_input.original_utterance.lower():
    success = find_and_add_copy_from_text(item_str)

    if not success or item_str in VALID_GENERATED_TOKENS:
      example.gold_sql_query.actions.append(SQLAction(symbol=item_str))

    return success or item_str in VALID_GENERATED_TOKENS

  elif item_str.startswith('\'') and item_str.endswith('\''):
    quote_type = '\''
  elif item_str.startswith('"') and item_str.endswith('"'):
    quote_type = '"'

  if quote_type:
    if item_str[1:-1].lower() in example.model_input.original_utterance.lower():
      example.gold_sql_query.actions.append(SQLAction(symbol=quote_type))

      success = find_and_add_copy_from_text(item_str[1:-1])
      if not success or item_str in VALID_GENERATED_TOKENS:
        example.gold_sql_query.actions.append(SQLAction(symbol=item_str))

      example.gold_sql_query.actions.append(SQLAction(symbol=quote_type))

      return success or item_str in VALID_GENERATED_TOKENS
    elif item_str[1] == '%' and item_str[-2] == '%' and item_str[2:-2].lower(
    ) in example.model_input.original_utterance.lower():
      example.gold_sql_query.actions.append(SQLAction(symbol=quote_type))
      example.gold_sql_query.actions.append(SQLAction(symbol='%'))

      success = find_and_add_copy_from_text(item_str[2:-2])
      if not success or item_str in VALID_GENERATED_TOKENS:
        example.gold_sql_query.actions.append(SQLAction(symbol=item_str[2:-2]))

      example.gold_sql_query.actions.append(SQLAction(symbol='%'))
      example.gold_sql_query.actions.append(SQLAction(symbol=quote_type))

      return success or item_str in VALID_GENERATED_TOKENS

  # Just add it as choice from the output vocabulary
  if u'u s a' in item_str:
    raise ValueError('WHAT????????')
  example.gold_sql_query.actions.append(SQLAction(symbol=item_str))

  return item_str in VALID_GENERATED_TOKENS
Adds a value action to the output.
language/xsp/data_preprocessing/abstract_sql_converters.py