def run_inference(source, rules, score_fn):
  """Determine one-best parse using score_fn.

  Args:
    source: Input string.
    rules: Set of QCFGRules.
    score_fn: Function with inputs (rule, span_begin, span_end) and returns
      float score for a given anchored rule application. Note that `span_begin`
      and `span_end` refer to token indexes, where span_end is exclusive, and
      `rule` is a QCFGRule.

  Returns:
    (target string, score) for highest scoring derivation, or (None, None)
    if there is no derivation for given source.
  """
  tokens = source.split(" ")
  node_fn = get_node_fn(score_fn)
  nodes = qcfg_parser.parse(
      tokens, rules, node_fn=node_fn, postprocess_cell_fn=postprocess_cell_fn)

  if not nodes:
    return None, None

  if len(nodes) > 1:
    raise ValueError("Multiple nodes returned for inference: %s" % nodes)

  return nodes[0].target_string, nodes[0].score
Determine one-best parse using score_fn.

  Args:
    source: Input string.
    rules: Set of QCFGRules.
    score_fn: Function with inputs (rule, span_begin, span_end) and returns
      float score for a given anchored rule application. Note that `span_begin`
      and `span_end` refer to token indexes, where span_end is exclusive, and
      `rule` is a QCFGRule.

  Returns:
    (target string, score) for highest scoring derivation, or (None, None)
    if there is no derivation for given source.
  
language/compgen/nqg/model/parser/inference/inference_parser.py