(g: SemanticGraph, pass_exception=False, use_wikidata=True)
Convert the given graph to a WikiData query and retrieve the results. The results contain possible bindings
    for all free variables in the graph. If there are no free variables a single empty grounding is returned.

    :param g: graph as a dictionary
    :param pass_exception:
    :return: graph groundings encoded as a list of dictionaries
    >>> get_graph_groundings(SemanticGraph([Edge(leftentityid=QUESTION_VAR, rightentityid='Q571', qualifierentityid='MAX')]))
    [{'r0v': 'P31v'}, {'r0v': 'P800v'}]
    >>> get_graph_groundings(SemanticGraph([Edge(leftentityid='Q35637', relationid='P1346', rightentityid=QUESTION_VAR, qualifierentityid='2009'), Edge(leftentityid=QUESTION_VAR, relationid='iclass')]))
    [{'r1v': 'P31c', 'topic': 'human'}, {'r1v': 'P106c', 'topic': 'politician'}]
Convert the given graph to a WikiData query and retrieve the results. The results contain possible bindings
    for all free variables in the graph. If there are no free variables a single empty grounding is returned.
Convert the given graph to a WikiData query and retrieve the results . The results contain possible bindings for all free variables in the graph . If there are no free variables a single empty grounding is returned .
def get_graph_groundings(g: SemanticGraph, pass_exception=False, use_wikidata=True):
    """
    Convert the given graph to a WikiData query and retrieve the results. The results contain possible bindings
    for all free variables in the graph. If there are no free variables a single empty grounding is returned.

    :param g: graph as a dictionary
    :param pass_exception:
    :return: graph groundings encoded as a list of dictionaries
    >>> get_graph_groundings(SemanticGraph([Edge(leftentityid=QUESTION_VAR, rightentityid='Q571', qualifierentityid='MAX')]))
    [{'r0v': 'P31v'}, {'r0v': 'P800v'}]
    >>> get_graph_groundings(SemanticGraph([Edge(leftentityid='Q35637', relationid='P1346', rightentityid=QUESTION_VAR, qualifierentityid='2009'), Edge(leftentityid=QUESTION_VAR, relationid='iclass')]))
    [{'r1v': 'P31c', 'topic': 'human'}, {'r1v': 'P106c', 'topic': 'politician'}]
    """
    ungrouded_edges = g.get_ungrounded_edges()
    if ungrouded_edges:
        if len(ungrouded_edges) == 1 and ungrouded_edges[0].relationid == "iclass":
            if "zip" in g.tokens and any(e.relationid == "P281" for e in g.edges):
                return [{'r1v': 'P31c', 'topic': 'Q37447'}]
            elif any([scheme.property2label[e.relationid]["type"] == "time"
                      for e in g.edges if e.leftentityid != QUESTION_VAR]):
                return [{'r1v': 'P31c', 'topic': "Q577"}]
        if use_wikidata:
            groundings = endpoint_access.query_wikidata(graph_to_query(g, limit=500))
        else:
            groundings = get_all_groundings(g)
        if groundings is None:  # If there was an exception
            return None if pass_exception else []
        elif len(groundings) > 0:
            # keys = {b for r in groundings for b in r if b.startswith("r")}
            for e in ungrouded_edges:
                groundings = filter_relations(groundings, b=f"r{e.edgeid:d}v", freq_threshold=FREQ_THRESHOLD)
            if sentence.get_question_type(" ".join(g.tokens)) != 'temporal':
                groundings = [r for r in groundings if all([scheme.property2label[r[f"r{e.edgeid:d}v"][:-1]]["type"] != "time"
                                                           for e in ungrouded_edges if e.leftentityid != QUESTION_VAR])]
        groundings = sorted(groundings,
                            key=lambda r: sum([scheme.property2label[r[f"r{e.edgeid:d}v"][:-1]]['freq']
                                               for e in ungrouded_edges if f"r{e.edgeid:d}v" in r]), reverse=True)
        return groundings
    else:
        if verify_grounding(g) or not use_wikidata:
            return [{}]
        else:
            return []
def get_graph_groundings ( g : SemanticGraph , pass_exception = False , use_wikidata = True ) : ungrouded_edges = g . get_ungrounded_edges ( ) if ungrouded_edges : if len ( ungrouded_edges ) == 1 and ungrouded_edges [ 0 ] . relationid == "iclass" : if "zip" in g . tokens and any ( e . relationid == "P281" for e in g . edges ) : return [ { 'r1v' : 'P31c' , 'topic' : 'Q37447' } ] elif any ( [ scheme . property2label [ e . relationid ] [ "type" ] == "time" for e in g . edges if e . leftentityid != QUESTION_VAR ] ) : return [ { 'r1v' : 'P31c' , 'topic' : "Q577" } ] if use_wikidata : groundings = endpoint_access . query_wikidata ( graph_to_query ( g , limit = 500 ) ) else : groundings = get_all_groundings ( g ) if groundings is None : # If there was an exception return None if pass_exception else [ ] elif len ( groundings ) > 0 : # keys = {b for r in groundings for b in r if b.startswith("r")} for e in ungrouded_edges : groundings = filter_relations ( groundings , b = f"r{e.edgeid:d}v" , freq_threshold = FREQ_THRESHOLD ) if sentence . get_question_type ( " " . join ( g . tokens ) ) != 'temporal' : groundings = [ r for r in groundings if all ( [ scheme . property2label [ r [ f"r{e.edgeid:d}v" ] [ : - 1 ] ] [ "type" ] != "time" for e in ungrouded_edges if e . leftentityid != QUESTION_VAR ] ) ] groundings = sorted ( groundings , key = lambda r : sum ( [ scheme . property2label [ r [ f"r{e.edgeid:d}v" ] [ : - 1 ] ] [ 'freq' ] for e in ungrouded_edges if f"r{e.edgeid:d}v" in r ] ) , reverse = True ) return groundings else : if verify_grounding ( g ) or not use_wikidata : return [ { } ] else : return [ ]