-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathrels.py
executable file
·74 lines (62 loc) · 2.41 KB
/
rels.py
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#!/usr/bin/env python3
import re
from lxml import etree
filename = "tlg0012.tlg001.perseus-grc1.tb.xml"
prefix = "Il"
filename = "tlg0012.tlg002.perseus-grc1.tb.xml"
prefix = "Od"
formlist = {}
headlist = {}
haslemma = {}
hasrelation = {}
hascite = {}
hasform = {}
haspostag = {}
prev_ref = None
with open(filename) as f:
tree = etree.parse(f)
for sentence in tree.xpath("//sentence"):
sentence_id = sentence.attrib["id"]
subdoc = sentence.attrib["subdoc"]
for word in sentence.xpath("word"):
if "cite" in word.attrib:
word_id = word.attrib["id"]
form = word.attrib["form"]
lemma = word.attrib["lemma"]
postag = word.attrib["postag"]
head = word.attrib["head"]
relation = word.attrib["relation"]
cite = word.attrib["cite"]
ref = cite.split(":")[-1]
if ref:
fullid = sentence_id + ":" + word_id
headid = sentence_id + ":" + head
headlist[fullid] = headid
haslemma[fullid] = lemma
hasrelation[fullid] =relation
haspostag[fullid] =postag
hascite[fullid] =cite
hasform[fullid] =form
#print(word.attrib["form"]," ","fullid:",fullid)
formlist[fullid] = word
#print ("lemma:",formlist[fullid].attrib["lemma"])
#if ref:
#if ref != prev_ref:
# if prev_ref:
# print()
#print(f"{prefix}.{ref}", end=" ")
# prev_ref = ref
#print(lemma, end=" ")
for id in formlist:
if(formlist[id].attrib["postag"] and id in headlist):
#print("pos:",formlist[id].attrib["postag"])
wdhead = headlist[id]
if(re.search("_CO",hasrelation[id]) and wdhead in haslemma):
if( headlist[wdhead] in haslemma):
wdhead = headlist[wdhead]
# print ("CO head",headlist[id],hasform[wdhead])
if( wdhead in haslemma):
print(haslemma[id], hasform[id],hasrelation[id],haspostag[id],haslemma[wdhead],hasform[wdhead],haspostag[wdhead],hascite[id]),hascite[id]
#else:
#print("no lemma for:",headlist[id])
#print("id",id)