def build_index_and_mapping(triples):
ents = bidict()<EOL>rels = bidict()<EOL>ent_id = <NUM_LIT:0><EOL>rel_id = <NUM_LIT:0><EOL>collected = []<EOL>for t in triples:<EOL><INDENT>for e in (t.head, t.tail):<EOL><INDENT>if e not in ents:<EOL><INDENT>ents[e] = ent_id<EOL>ent_id += <NUM_LIT:1><EOL><DEDENT><DEDENT>if t.relation not in rels:<EOL><INDENT>rels[t.relation] = rel_id<EOL>rel_id += <NUM_LIT:1><EOL><DEDENT>collected.append(kgedata.TripleIndex(ents[t.head], rels[t.relation], ents[t.tail]))<EOL><DEDENT>return collected, ents, rels<EOL>
index all triples into indexes and return their mappings