def _json_entry_to_example(self, entry, candidate_index, example_index):
    """Converts FewRel entry (candidate or query) into RelationInputExample."""
    text = " ".join(entry["tokens"])
    e1_tokens = [entry["h"][2][0][0], entry["h"][2][0][-1]]
    e2_tokens = [entry["t"][2][0][0], entry["t"][2][0][-1]]
    e1 = [0, 0]
    e2 = [0, 0]
    if self._add_entity_markers:
      wordpieces = _tokenize_with_entity_markers(
          entry["tokens"], self._tokenizer, e1_tokens, e2_tokens)
    else:
      wordpieces = self._tokenizer.tokenize(text)

    if len(wordpieces) > self._max_seq_length - 2:
      tf.logging.info("[_create_entrys] Truncating sentence [%s] %s",
                      len(wordpieces), " ".join(wordpieces).encode("utf-8"))
      wordpieces = wordpieces[0:(self._max_seq_length - 2)]
    wordpieces = ["[CLS]"] + wordpieces + ["[SEP]"]

    token_index = 0
    cur_token_length = 0
    for i, wordpiece in enumerate(wordpieces):
      if wordpiece in reserved_wordpieces():
        continue

      # For the start of entity, index should be first wordpiece.
      if e1[0] == 0 and e1_tokens[0] == token_index:
        e1[0] = i
      # For the end of entity, index should be last wordpiece.
      if e1_tokens[1] == token_index:
        e1[1] = i
      if e2[0] == 0 and e2_tokens[0] == token_index:
        e2[0] = i
      if e2_tokens[1] == token_index:
        e2[1] = i

      cur_token_length += len(wordpiece) - 2 * int(wordpiece.startswith("##"))
      if cur_token_length == len(entry["tokens"][token_index]):
        token_index += 1
        cur_token_length = 0

    return RelationInputExample(
        guid="%s:%s" % (candidate_index, example_index),
        wordpieces=wordpieces,
        label=candidate_index,
        e1=e1,
        e2=e2)
Converts FewRel entry (candidate or query) into RelationInputExample.
language/relation_learning/data/fewrel.py