(sentence_tokens, graph_set)
return sentence_tokens
Replace entities in a sentences given a list of graphs.

    :param sentence_tokens: list of tokens
    :param graph_set: list of graphs
    :return: list of tokens with replaced entities
    >>> replace_entities_in_instance(['This', 'can', 'include', 'erotic', 'and', 'nude', 'modeling', ',', 'pornography', \
    ',', 'escorting', ',', 'and', 'in', 'some', 'cases', 'prostitution', '.'], [{'entities': [(['prostitution'], 'NNP'), (['escorting'], 'NNP')], 'edgeSet': [{'type': 'direct', 'kbID': 'P264v'}]}])
    ['This', 'can', 'include', 'erotic', 'and', 'nude', 'modeling', ',', 'pornography', ',', '<e>', ',', 'and', 'in', 'some', 'cases', '<e>', '.']
    >>> replace_entities_in_instance(['what', 'is', 'the', 'president', 'of', 'brazil', '?'],\
     [{'edgeSet': [{'canonical_right': 'Brazil', 'kbID': 'P35v','right': ['Brazil'],'rightkbID': 'Q155','type': 'reverse'}, \
     {'canonical_right': 'president','hopUp': 'P279v','kbID': 'P1308v','right': ['president'],'rightkbID': 'Q30461','type': 'reverse'},\
     {'canonical_right': ['sociologist','diplomat','economist','politician','trade unionist','writer'],'kbID': 'P106v','type': 'iclass'},\
     {'canonical_right': ['human'], 'kbID': 'P31v', 'type': 'iclass'}], 'entities': []}])
    ['what', 'is', 'the', 'president', 'of', '<e>', '?']
    >>> replace_entities_in_instance(['what', 'is', 'the', 'president', 'of', 'brazil', '?'],\
     [{'edgeSet': [{'kbID': 'P35v', 'type': 'reverse'}, \
     {'hopUp': 'P279v','kbID': 'P1308v','type': 'reverse'}]}])
    ['what', 'is', 'the', 'president', 'of', 'brazil', '?']
Replace entities in a sentences given a list of graphs.
Replace entities in a sentences given a list of graphs .
def replace_entities_in_instance(sentence_tokens, graph_set):
    """
    Replace entities in a sentences given a list of graphs.

    :param sentence_tokens: list of tokens
    :param graph_set: list of graphs
    :return: list of tokens with replaced entities
    >>> replace_entities_in_instance(['This', 'can', 'include', 'erotic', 'and', 'nude', 'modeling', ',', 'pornography', \
    ',', 'escorting', ',', 'and', 'in', 'some', 'cases', 'prostitution', '.'], [{'entities': [(['prostitution'], 'NNP'), (['escorting'], 'NNP')], 'edgeSet': [{'type': 'direct', 'kbID': 'P264v'}]}])
    ['This', 'can', 'include', 'erotic', 'and', 'nude', 'modeling', ',', 'pornography', ',', '<e>', ',', 'and', 'in', 'some', 'cases', '<e>', '.']
    >>> replace_entities_in_instance(['what', 'is', 'the', 'president', 'of', 'brazil', '?'],\
     [{'edgeSet': [{'canonical_right': 'Brazil', 'kbID': 'P35v','right': ['Brazil'],'rightkbID': 'Q155','type': 'reverse'}, \
     {'canonical_right': 'president','hopUp': 'P279v','kbID': 'P1308v','right': ['president'],'rightkbID': 'Q30461','type': 'reverse'},\
     {'canonical_right': ['sociologist','diplomat','economist','politician','trade unionist','writer'],'kbID': 'P106v','type': 'iclass'},\
     {'canonical_right': ['human'], 'kbID': 'P31v', 'type': 'iclass'}], 'entities': []}])
    ['what', 'is', 'the', 'president', 'of', '<e>', '?']
    >>> replace_entities_in_instance(['what', 'is', 'the', 'president', 'of', 'brazil', '?'],\
     [{'edgeSet': [{'kbID': 'P35v', 'type': 'reverse'}, \
     {'hopUp': 'P279v','kbID': 'P1308v','type': 'reverse'}]}])
    ['what', 'is', 'the', 'president', 'of', 'brazil', '?']
    """
    graph_entities = {}
    for g in graph_set:
        entities = [entity if type(entity) == dict else {"type": entity[1], "tokens": entity[0]} for entity in
                    g.get('entities', [])]
        # First add entities that are not features in edges yet
        graph_entities.update({" ".join(entity.get("tokens", [])): entity for entity in entities})
        # Add entities from the edges
        for e in g.get("edgeSet", []):
            if e.get("type") != "iclass" and e.get('right'):
                e_type = "NNP"
                e_relations = {e.get("kbID"), e.get("hopUp"), e.get("hopDown")}
                if e.get("type") in {"class", "iclass", "time"} or e_relations & {"P31v", "P106v", "P279v"}:
                    e_type = "NN"
                graph_entities[" ".join(e.get('right', []))] = {"tokens": e.get('right', []), "type": e_type}
    sentence_tokens = replace_entities(sentence_tokens, graph_entities.values())
    return sentence_tokens
def replace_entities_in_instance ( sentence_tokens , graph_set ) : graph_entities = { } for g in graph_set : entities = [ entity if type ( entity ) == dict else { "type" : entity [ 1 ] , "tokens" : entity [ 0 ] } for entity in g . get ( 'entities' , [ ] ) ] # First add entities that are not features in edges yet graph_entities . update ( { " " . join ( entity . get ( "tokens" , [ ] ) ) : entity for entity in entities } ) # Add entities from the edges for e in g . get ( "edgeSet" , [ ] ) : if e . get ( "type" ) != "iclass" and e . get ( 'right' ) : e_type = "NNP" e_relations = { e . get ( "kbID" ) , e . get ( "hopUp" ) , e . get ( "hopDown" ) } if e . get ( "type" ) in { "class" , "iclass" , "time" } or e_relations & { "P31v" , "P106v" , "P279v" } : e_type = "NN" graph_entities [ " " . join ( e . get ( 'right' , [ ] ) ) ] = { "tokens" : e . get ( 'right' , [ ] ) , "type" : e_type } sentence_tokens = replace_entities ( sentence_tokens , graph_entities . values ( ) ) return sentence_tokens