Created
May 3, 2021 10:46
-
-
Save daanklijn/47e3f930b7094875445272c575bb8f27 to your computer and use it in GitHub Desktop.
CONLL-U dataframe to dependency tree
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
df = pd.read_csv('conllu.csv') | |
words = [] | |
arcs = [] | |
for index, row in df.iterrows(): | |
words.append({ | |
'text': row['word'], | |
'tag': row['pos'] | |
}) | |
dep_head = row['dependency_head'] - 1 | |
dep_label = row['dependency_label'] | |
if dep_label not in ['root','_']: | |
arcs.append({ | |
'start': dep_head if index > dep_head else index, | |
'end': index if index > dep_head else dep_head, | |
'label': dep_label, | |
'dir': 'right' if index > dep_head else 'left' | |
}) | |
html = displacy.render([{'words': words, 'arcs': arcs}], | |
style="dep", manual=True) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Input:
Output:
