(grounded_graphs: List[SemanticGraph])
return grounded_graphs
This methods filters out second hop relations that are already present as first hop relations. Relation direction is
    respected.

    :param grounded_graphs: list of grounded graphs
    :return: filtered list of grounded graphs
    >>> g = graph.SemanticGraph(free_entities=[{"type":"NNP", "token_ids":[4], "linkings": [("Q158707", None)]}])
    >>> filter_second_hops([apply_grounding(s_g, p) for s_g in stages.add_entity_and_relation(g, leg_length=1) + stages.add_entity_and_relation(g, leg_length=2, fixed_relations=['P26']) for p in graph_queries.get_graph_groundings(s_g)])
This methods filters out second hop relations that are already present as first hop relations. Relation direction is
    respected.
This methods filters out second hop relations that are already present as first hop relations . Relation direction is respected .
def filter_second_hops(grounded_graphs: List[SemanticGraph]):
    """
    This methods filters out second hop relations that are already present as first hop relations. Relation direction is
    respected.

    :param grounded_graphs: list of grounded graphs
    :return: filtered list of grounded graphs
    >>> g = graph.SemanticGraph(free_entities=[{"type":"NNP", "token_ids":[4], "linkings": [("Q158707", None)]}])
    >>> filter_second_hops([apply_grounding(s_g, p) for s_g in stages.add_entity_and_relation(g, leg_length=1) + stages.add_entity_and_relation(g, leg_length=2, fixed_relations=['P26']) for p in graph_queries.get_graph_groundings(s_g)])
    """

    first_order_relations = {r for g in grounded_graphs for e in g.edges for r in {e.relationid, e.qualifierrelationid}
                             if any(n.startswith("Q") for n in e.nodes() if n) and graph_queries.QUESTION_VAR in e.nodes() and r}
    grounded_graphs = [g for g in grounded_graphs
                       if all((e.relationid not in first_order_relations and e.qualifierrelationid not in first_order_relations)
                              or any(n.startswith("Q") for n in e.nodes() if n) for e in g.edges)]
    return grounded_graphs
def filter_second_hops ( grounded_graphs : List [ SemanticGraph ] ) : first_order_relations = { r for g in grounded_graphs for e in g . edges for r in { e . relationid , e . qualifierrelationid } if any ( n . startswith ( "Q" ) for n in e . nodes ( ) if n ) and graph_queries . QUESTION_VAR in e . nodes ( ) and r } grounded_graphs = [ g for g in grounded_graphs if all ( ( e . relationid not in first_order_relations and e . qualifierrelationid not in first_order_relations ) or any ( n . startswith ( "Q" ) for n in e . nodes ( ) if n ) for e in g . edges ) ] return grounded_graphs