def serialize_fact(self, document, tok_to_char_offset, ctx_span, subj_id,
                     subj_mentions, property_id, tail_id, tail_mention,
                     ctx_type):
    """Create JSON object with distantly supervised fact."""
    context = " ".join(
        [tt.word for tt in document.tokens[ctx_span[0]:ctx_span[1]]])
    my_offsets = {}
    begin = tok_to_char_offset[ctx_span[0]]
    for ix in range(ctx_span[0], ctx_span[1]):
      my_offsets[ix] = tok_to_char_offset[ix] - begin
    all_mentions = []
    for m in subj_mentions:
      all_mentions.append({
          "start": my_offsets[m.begin],
          "text": " ".join(tt.word for tt in document.tokens[m.begin:m.end])
      })
    subject = {
        "wikidata_id": subj_id,
        "aliases": self.get_aliases(subj_id),
        "mentions": all_mentions,
    }
    if property_id not in self.property_names:
      print("did not find", property_id, "in names")
    relation = {
        "wikidata_id": property_id,
        "text": self.property_names.get(property_id, property_id)
    }
    if tail_id is None:
      obj = None
      id_ = subj_id + "_" + property_id + "_None"
    else:
      obj = {
          "wikidata_id": tail_id,
          "aliases": self.get_aliases(tail_id),
          "mention": {
              "start":
                  my_offsets[tail_mention.begin],
              "text":
                  " ".join(tt.word for tt in
                           document.tokens[tail_mention.begin:tail_mention.end])
          }
      }
      id_ = subj_id + "_" + property_id + "_" + str(tail_id)
    serialized = json.dumps({
        "id": id_,
        "context": context,
        "subject": subject,
        "relation": relation,
        "object": obj,
        "context_type": ctx_type,
    })
    return serialized
Create JSON object with distantly supervised fact.
language/labs/drkit/wikidata/preprocessing/distantly_supervise.py