(graph_with_scores, gold_answers)
return return_graphs
Generate all possible groundings that produce positive f-score starting with the given ungrounded graph and
    using expand and restrict operations on its denotation.

    :param graph_with_scores: a tuple of the starting graph that should contain a list of tokens and a list of entities and a zero score
    :param gold_answers: list of gold answers for the encoded question
    :return: a list of generated grounded graphs
Generate all possible groundings that produce positive f-score starting with the given ungrounded graph and
    using expand and restrict operations on its denotation.
Generate all possible groundings that produce positive f - score starting with the given ungrounded graph and using expand and restrict operations on its denotation .
def generate_with_gold(graph_with_scores, gold_answers):
    """
    Generate all possible groundings that produce positive f-score starting with the given ungrounded graph and
    using expand and restrict operations on its denotation.

    :param graph_with_scores: a tuple of the starting graph that should contain a list of tokens and a list of entities and a zero score
    :param gold_answers: list of gold answers for the encoded question
    :return: a list of generated grounded graphs
    """
    pool = [graph_with_scores]  # pool of possible parses
    if len(gold_answers) == 0 or not any(gold_answers):
        return pool
    positive_graphs, negative_graphs = [], []
    iterations = 0
    while pool \
            and (max(g.scores[2] for g in positive_graphs) if len(positive_graphs) > 0 else 0.0) < MIN_F_SCORE_TO_STOP \
            and iterations < MAX_ITERATIONS:
        g = pool.pop(0)
        logger.debug("Pool length: {}, Graph: {}".format(len(pool), g))
        master_g_fscore = g.scores[2]
        if master_g_fscore < MIN_F_SCORE_TO_STOP:
            chosen_graphs = []
            f_i = 0
            while f_i < len(stages.ACTIONS) and not chosen_graphs:
                suggested_graphs = stages.ACTIONS[f_i](g[0])
                logger.debug("Suggested graphs: {}".format(suggested_graphs))
                for s_g in suggested_graphs:
                    iterations += 1
                    temp_chosen_graphs, not_chosen_graphs = ground_one_with_gold(s_g, gold_answers, master_g_fscore)
                    negative_graphs += not_chosen_graphs
                    chosen_graphs += temp_chosen_graphs
                f_i += 1
            positive_graphs += chosen_graphs
            logger.debug("Chosen graphs length: {}".format(len(chosen_graphs)))

            if len(chosen_graphs) > 0:
                logger.debug("Extending the pool.")
                pool.extend(chosen_graphs)
                pool = sorted(pool, key=lambda x: (len(x.graph.edges), 1-x.scores[2]), reverse=False)

    negative_graphs = sorted(negative_graphs, key=lambda x: (len(x.graph.edges), -len(x.graph.denotations)), reverse=True)
    positive_graphs = sorted(positive_graphs, key=lambda x: x.scores[2], reverse=True)
    return_graphs = positive_graphs + negative_graphs[:100]
    for g in return_graphs:
        g.graph.denotation_classes = graph_queries.get_graph_groundings(
            stages.with_denotation_class_edge(g.graph))
    logger.debug(f"Iterations {iterations}")
    logger.debug(f"Negative {len(negative_graphs)}")
    if iterations >= MAX_ITERATIONS:
        logger.error(f"Max iterations reached: {iterations}")
    return return_graphs
def generate_with_gold ( graph_with_scores , gold_answers ) : pool = [ graph_with_scores ] # pool of possible parses if len ( gold_answers ) == 0 or not any ( gold_answers ) : return pool positive_graphs , negative_graphs = [ ] , [ ] iterations = 0 while pool and ( max ( g . scores [ 2 ] for g in positive_graphs ) if len ( positive_graphs ) > 0 else 0.0 ) < MIN_F_SCORE_TO_STOP and iterations < MAX_ITERATIONS : g = pool . pop ( 0 ) logger . debug ( "Pool length: {}, Graph: {}" . format ( len ( pool ) , g ) ) master_g_fscore = g . scores [ 2 ] if master_g_fscore < MIN_F_SCORE_TO_STOP : chosen_graphs = [ ] f_i = 0 while f_i < len ( stages . ACTIONS ) and not chosen_graphs : suggested_graphs = stages . ACTIONS [ f_i ] ( g [ 0 ] ) logger . debug ( "Suggested graphs: {}" . format ( suggested_graphs ) ) for s_g in suggested_graphs : iterations += 1 temp_chosen_graphs , not_chosen_graphs = ground_one_with_gold ( s_g , gold_answers , master_g_fscore ) negative_graphs += not_chosen_graphs chosen_graphs += temp_chosen_graphs f_i += 1 positive_graphs += chosen_graphs logger . debug ( "Chosen graphs length: {}" . format ( len ( chosen_graphs ) ) ) if len ( chosen_graphs ) > 0 : logger . debug ( "Extending the pool." ) pool . extend ( chosen_graphs ) pool = sorted ( pool , key = lambda x : ( len ( x . graph . edges ) , 1 - x . scores [ 2 ] ) , reverse = False ) negative_graphs = sorted ( negative_graphs , key = lambda x : ( len ( x . graph . edges ) , - len ( x . graph . denotations ) ) , reverse = True ) positive_graphs = sorted ( positive_graphs , key = lambda x : x . scores [ 2 ] , reverse = True ) return_graphs = positive_graphs + negative_graphs [ : 100 ] for g in return_graphs : g . graph . denotation_classes = graph_queries . get_graph_groundings ( stages . with_denotation_class_edge ( g . graph ) ) logger . debug ( f"Iterations {iterations}" ) logger . debug ( f"Negative {len(negative_graphs)}" ) if iterations >= MAX_ITERATIONS : logger . error ( f"Max iterations reached: {iterations}" ) return return_graphs