def rule_codelength(rule, config):
  """Computes the codelength for a given rule."""
  length = 0.0
  for token in rule.source + rule.target:
    if token in {qcfg_rule.NT_1, qcfg_rule.NT_2}:
      length += config.non_terminal_codelength
    else:
      length += config.terminal_codelength
  return length
Computes the codelength for a given rule.
language/compgen/nqg/model/induction/codelength_utils.py