def _link_entities(line, overall_regex, pattern_to_entities):
  """Return list of matching entities in text."""
  matches = []

  def _add_entities(match, text, entities):
    for entity in entities:
      m_st = match.start()
      m_en = match.start() + len(match.group(1))
      matches.append({
          "name": entity[0],
          "kb_id": entity[1],
          "text": text[m_st:m_en],
          "start": m_st,
      })

  for match in re.finditer(overall_regex, line):
    ent = match.group(1).lower()
    if ent not in pattern_to_entities:
      print("Did not find %s" % ent)
      continue
    _add_entities(match, line, pattern_to_entities[ent])
  return matches
Return list of matching entities in text.
language/labs/drkit/metaqa/preprocessing/process_wiki.py