def build_connections(movies, participants, role, threshold):
  """
  Build connections between movies and participants (cast and directors).
  The `role` filed in the `attributes` will be constructed with corresponding
  pointers, and the nodes that are not connected to any movie will be removed.

  Parameters
  ----------
  movies : dict
    A dict of movies, with movie id as keys.
  participants : dict
    A dict of participants, with subject id as keys.
  role : str
    New key in the attributes to save the links.
  threshold : int
    Only the participants linked with more than `threshold` number of movies
    will be kept.

  Returns
  -------
  dict
    Updated `movies`.
  dict
    Updated `participants`.
  """
  linked = set()
  for m in movies.values():
    m.attributes[role] = {}
  for p in participants.values():
    movie_ids = [int(m.strip()) for m in p.attributes['movie_id'].split(',')]
    p.attributes.pop('movie_id')
    if len(movie_ids) <= threshold:
      continue
    for mid in movie_ids:
      if mid in movies.keys():
        movies[mid].attributes[role][p.id] = p
        p.movies[mid] = movies[mid]
        linked.add(p.id)
  to_pop = []
  for p in participants.keys():
    if p not in linked:
      to_pop.append(p)
  for p in to_pop:
    participants.pop(p)
  return movies, participants