import math


def entity_count_penalty(source_entities_count, candidate_entities_count):
  """Calculates the entity count penalty (ECP) introduced in the paper.

  ECP is inspired by BLEU’s brevity penalty. ECP penalizes systems producing c
  entities if c is more than twice the number of entities in the source.

  Args:
    source_entities_count: Count of source entities.
    candidate_entities_count: Count of candidate entities.

  Returns:
    ECP value.
  """
  s, c = float(source_entities_count), float(candidate_entities_count)
  return 1.0 if c < 2 * s else math.exp(1 - c / (2 * s))