Semantic triples- how to make a function that finds dependensies for a given entity in a list?

Viewed 8
def get_entity(sen, ind):
    entity_str = sen[ind][1]
    
    # TODO: Add following entity to entity-items
    fo_i = ind + 1
    while fo_i < len(sen) and ____:
        entity_str += " " + ____
        fo_i += 1
    
    # TODO: Add acendent to entity-iems
    pr_i = ind - 1
    while pr_i > -1 and ____:
        entity_str = ____ + " " + entity_str
        pr_i -= 1
    
    return entity_str


doc = nlp(sentences[13])
sent = [(token.i, token.text, token.pos_, token.dep_.lower(), \
        token.head.i, token.head.text, token.head.pos_, \
        token.ent_iob_) for token in doc]

print("Mening:")
for w in sent:
    print(w)

print("Word on index=24:", sent[24])

complete_entity = get_entity(sent, 24)
print("Complete entity:", complete_entity)

I'm just supposed to add code to the empty lines in the code above. This is the list I'm working from:

(0, 'French', 'ADJ', 'amod', 1, 'musician', 'NOUN', 'B') (1, 'musician', 'NOUN', 'compound', 5, 'Jarre', 'PROPN', 'O') (2, 'Jean', 'PROPN', 'compound', 4, 'Michel', 'PROPN', 'B') (3, '-', 'PUNCT', 'punct', 4, 'Michel', 'PROPN', 'I') (4, 'Michel', 'PROPN', 'compound', 5, 'Jarre', 'PROPN', 'I') (5, 'Jarre', 'PROPN', 'nsubj', 6, 'is', 'AUX', 'I') (6, 'is', 'AUX', 'root', 6, 'is', 'AUX', 'O') (7, 'to', 'PART', 'aux', 8, 'perform', 'VERB', 'O') (8, 'perform', 'VERB', 'xcomp', 6, 'is', 'AUX', 'O') (9, 'at', 'ADP', 'prep', 8, 'perform', 'VERB', 'O') (10, 'a', 'DET', 'det', 11, 'concert', 'NOUN', 'O') (11, 'concert', 'NOUN', 'pobj', 9, 'at', 'ADP', 'O') (12, 'in', 'ADP', 'prep', 11, 'concert', 'NOUN', 'O') (13, 'Copenhagen', 'PROPN', 'pobj', 12, 'in', 'ADP', 'B') (14, 'to', 'PART', 'aux', 15, 'mark', 'VERB', 'O') (15, 'mark', 'VERB', 'advcl', 8, 'perform', 'VERB', 'O') (16, 'the', 'DET', 'det', 17, 'bicentennial', 'NOUN', 'O') (17, 'bicentennial', 'NOUN', 'dobj', 15, 'mark', 'VERB', 'O') (18, 'of', 'ADP', 'prep', 17, 'bicentennial', 'NOUN', 'O') (19, 'the', 'DET', 'det', 20, 'birth', 'NOUN', 'O') (20, 'birth', 'NOUN', 'pobj', 18, 'of', 'ADP', 'O') (21, 'of', 'ADP', 'prep', 20, 'birth', 'NOUN', 'O') (22, 'writer', 'NOUN', 'compound', 25, 'Andersen', 'PROPN', 'O') (23, 'Hans', 'PROPN', 'compound', 25, 'Andersen', 'PROPN', 'B') (24, 'Christian', 'PROPN', 'compound', 25, 'Andersen', 'PROPN', 'I') (25, 'Andersen', 'PROPN', 'pobj', 21, 'of', 'ADP', 'I') (26, '.', 'PUNCT', 'punct', 6, 'is', 'AUX', 'O')

My problem is that I only get one ancestor when writing this:

pr_i = ind - 1
    while pr_i > -1 and sen[pr_i][7] == 'B':
        entity_str = sen[pr_i][1] + " " + entity_str
        pr_i -= 1
0 Answers
Related