-
Notifications
You must be signed in to change notification settings - Fork 8
/
load.py
90 lines (72 loc) · 2.87 KB
/
load.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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
__author__='thiagocastroferreira'
def source(tripleset, entitymap, entities={}):
src, delexsrc = [], []
for triple in tripleset:
subject = '_'.join(triple.subject.split())
predicate = '_'.join(triple.predicate.split())
patient = '_'.join(triple.object.split())
src.append('<TRIPLE>')
src.append(subject)
src.append(predicate)
src.append(patient)
src.append('</TRIPLE>')
delexsrc.append('<TRIPLE>')
# SUBJECT
if entitymap[triple.subject] not in entities:
entity = 'ENTITY-' + str(len(list(entities.keys())) + 1)
entities[entitymap[triple.subject]] = entity
else:
entity = entities[entitymap[triple.subject]]
delexsrc.append(entity)
# PREDICATE
delexsrc.append(predicate)
# OBJECT
if entitymap[triple.object] not in entities:
entity = 'ENTITY-' + str(len(list(entities.keys())) + 1)
entities[entitymap[triple.object]] = entity
else:
entity = entities[entitymap[triple.object]]
delexsrc.append(entity)
delexsrc.append('</TRIPLE>')
# src.append('eos')
# delexsrc.append('eos')
return src, delexsrc, entities
def snt_source(tripleset, entitymap, entities):
aggregation = []
delex_aggregation = []
for sentence in tripleset:
snt, delex_snt = ['<SNT>'], ['<SNT>']
for striple in sentence:
subject = '_'.join(striple.subject.split())
predicate = '_'.join(striple.predicate.split())
patient = '_'.join(striple.object.split())
snt.append('<TRIPLE>')
snt.append(subject)
snt.append(predicate)
snt.append(patient)
snt.append('</TRIPLE>')
delex_snt.append('<TRIPLE>')
# SUBJECT
if entitymap[striple.subject] not in entities:
entity = 'ENTITY-' + str(len(list(entities.keys())) + 1)
entities[entitymap[striple.subject]] = entity
else:
entity = entities[entitymap[striple.subject]]
delex_snt.append(entity)
# PREDICATE
delex_snt.append(predicate)
# OBJECT
if entitymap[striple.object] not in entities:
entity = 'ENTITY-' + str(len(list(entities.keys())) + 1)
entities[entitymap[striple.object]] = entity
else:
entity = entities[entitymap[striple.object]]
delex_snt.append(entity)
delex_snt.append('</TRIPLE>')
snt.append('</SNT>')
aggregation.extend(snt)
delex_snt.append('</SNT>')
delex_aggregation.extend(delex_snt)
# aggregation = ['bos'] + aggregation + ['eos']
# delex_aggregation = ['bos'] + delex_aggregation + ['eos']
return aggregation, delex_aggregation, entities