-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtree.py
More file actions
52 lines (43 loc) · 1.24 KB
/
tree.py
File metadata and controls
52 lines (43 loc) · 1.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
class Word:
def __init__(self,ind, spel, pos, kar):
self.ind = ind
self.spel = spel
self.pos = pos
self.kar = kar
def __str__(self):
return str(self.spel)
def __repr__(self):
return str(self.spel)
def makeword(listitem):
word = Word(listitem[0], listitem[1], listitem[3], listitem[7])
return word
def subtree(word, pars):
sub = [makeword(word)]
sub += [subtree(child,pars) for child in pars if child[6] == word[0]]
return sub
def maketree(pars):
"""
Converts dependency parser output to tree representation.
"""
root = [word for word in pars if int(word[6]) == 0] # { word | word \in pars, word[6] == 0}
tree = [subtree(r,pars) for r in root]
return tree
def getkar(const):
return const[0].kar
def makesent(tree):
"""
Converts tree representation to list of words.
"""
def getsent(tree, sent):
sent[int(tree[0].ind)] = tree[0].spel
for dep in tree[1:]:
getsent(dep, sent)
return sent
sent = {}
for t in tree:
sent.update(getsent(t, {}))
sentence = []
for i in range(len(sent)):
if sent[i+1] != '':
sentence.append(sent[i+1])
return sentence