def read_results(ground_truth_file, predicted_answers_file):
  """Read results and ground truth and return data structure with stats."""
  with codecs.getreader("utf-8")(tf.gfile.GFile(ground_truth_file,
                                                "r")) as read:
    data_ = {}
    for line in read:
      item = json.loads(line.strip())
      if isinstance(item["relation"], dict):
        relation = item["relation"]["wikidata_id"]
      elif isinstance(item["relation"], list):
        relation = (
            item["relation"][0]["wikidata_id"] + "_" +
            item["relation"][1]["wikidata_id"])
      data_[item["id"]] = [relation, item["subject"]["wikidata_id"]]
      if "is_impossible" in item and item["is_impossible"]:
        continue
      if item["object"] is None:
        continue
      if isinstance(item["object"]["mention"], dict):
        data_[item["id"]] += [item["object"]["mention"]["text"]]
      if "name" in item["object"]:
        data_[item["id"]] += [item["object"]["name"]]
      if "aliases" in item["object"]:
        data_[item["id"]] += item["object"]["aliases"].keys()
  with codecs.getreader("utf-8")(tf.gfile.GFile(predicted_answers_file,
                                                "r")) as fin:
    predictions = json.load(fin)

    telemetry, incorrect = [], []
    n = 0
    for key in data_:
      if key not in predictions:
        continue
      g = data_[key][2:]
      a = predictions[key]
      m = data_[key][:2]
      stats = score(g, a)
      telemetry.append([m[0], m[1], g, a, stats])
      if stats[0] == 0. and stats[3] > 0.:
        incorrect.append(key)
      n += 1
    return telemetry, incorrect
Read results and ground truth and return data structure with stats.
language/labs/drkit/evaluate.py