def parse_shard(shard, current_index, max_seq_length):
  """Parse a single shard in input template to get shard string and quantity."""

  if "<opt>" in shard and "</opt>" in shard:
    # indication that these vectors need to be optimized but starting from the
    # provided input words.

    # Currently this does not support continuous space optimization starting
    # from randomly initialized word embeddings.
    shard = shard.replace("<opt>", "").replace("</opt>", "")
    opt_from_embedding = True
  else:
    opt_from_embedding = False

  if "<freq>" not in shard:
    # if <freq> is absent in shard, assume the frequency is 1
    # This is done for more convenient inputs
    shard_token = shard
    shard_quantity = "1"
  else:
    shard_token, shard_quantity = shard.split("<freq>")
  # shard_quantity is the number of tokens for current shard
  if shard_quantity == "*":
    # * tokens are only used at the end of a template
    shard_quantity = max(0, max_seq_length - current_index)
  else:
    shard_quantity = int(shard_quantity)

  return shard_token, shard_quantity, opt_from_embedding