-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathMAGIC.py
More file actions
150 lines (129 loc) · 6.57 KB
/
MAGIC.py
File metadata and controls
150 lines (129 loc) · 6.57 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
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
from __future__ import division
from ink.base.structure import InkExtractor
import pandas as pd
import json
import numpy as np
import operator
from tqdm import tqdm
global g
import re
from functools import lru_cache
class Magic(object):
def __init__(self, connector, structured_file, header, index_col, main_col, property_prefix, cta_filter=None, skiplist=[]):
self.connector=connector
self.file = structured_file
self.name = structured_file.split('/')[-1].split('.')[0]
self.maincol = main_col
self.property_prefix=property_prefix
self.skiplist=skiplist
self.cea = {}
self.cpa = {}
self.cta = set()
self.cta_filter=cta_filter
self.header = header
self.index_col = index_col
@lru_cache(maxsize=128)
def generate_embedding(self, data, depth, jobs, filter=None):
extractor = InkExtractor(self.connector, verbose=False)
X_train, _ = extractor.create_dataset(depth, set(data), set(), self.skiplist, jobs)
extracted_data = extractor.fit_transform(X_train, counts=False, levels=False, float_rpr=False)
df_data = pd.DataFrame.sparse.from_spmatrix(extracted_data[0])
df_data.index = [x for x in extracted_data[1]]
df_data.columns = extracted_data[2]
if filter is not None:
cols = [col for col in df_data.columns if filter in col]
df_data = df_data[cols]
return df_data
def search_entity_api(self, entity):
raise NotImplementedError("Please Implement this method")
def annotate(self):
df = pd.read_csv(self.file, header=self.header, index_col=self.index_col)
data = {}
for k, row in df.iterrows():
try:
data[(row[self.maincol],k)] = self.search_entity_api(re.sub("[\(\[].*?[\)\]]", "", row[self.maincol]))
except:
data[(row[self.maincol],k)] = []
all_entities=set()
for x in data:
all_entities.update(set(data[x]))
if len(all_entities)>0:
full_emb = self.generate_embedding(tuple(all_entities),2,30)
emb_label = full_emb[[c for c in full_emb.columns if self.property_prefix in c]]
cols = [c for c in emb_label.columns if c.count("http") < 2 or 'http://www.w3.org/2000/01/rdf-schema#label§' in c]
emb_label = emb_label[cols]
for k, row in df.iterrows():
if len(data[(row[self.maincol],k)]) > 0:
#emb = full_emb#self.generate_embedding(tuple(data),2,min([1,len(data)]))
#emb_label = emb[[c for c in emb.columns if self.property_prefix in c]]
cols = [c for c in emb_label.columns for r in range(len(row)) if
row[r] is not np.nan and r != self.maincol and '§' + str(row[r]) in c]
#cols = [c for c in cols if c.count("http") < 2 or 'http://www.w3.org/2000/01/rdf-schema#label§' in c]
major_ind = emb_label[cols].loc[data[(row[self.maincol],k)],:].sum(axis=1).idxmax()
self.cea[(k, self.maincol)] = major_ind
major_ind_emb = emb_label.loc[major_ind, cols]
major_ind_emb = major_ind_emb[(major_ind_emb != 0)]
major_ind_emb_cols = major_ind_emb.index
for r in range(len(row)):
if r != self.maincol and row[r] is not np.nan:
pos_cols = [c for c in cols if str(row[r]) in c]
if len(pos_cols) > 0:
pos_cols = max(pos_cols, key=len)
cpa_relation = pos_cols.split('.http')[0]
if '§' in cpa_relation:
cpa_relation = cpa_relation.split('§')[0]
if (self.maincol, r) not in self.cpa:
self.cpa[(self.maincol, r)] = {}
if cpa_relation not in self.cpa[(self.maincol, r)]:
self.cpa[(self.maincol, r)][cpa_relation] = 0
self.cpa[(self.maincol, r)][cpa_relation] += 1
res = self.connector.query_relation(major_ind, cpa_relation)
for i in res:
if i['l']['value'] == row[r]:
self.cea[(k, r)] = i['o']['value']
break
for k, row in df.iterrows():
for c in self.cpa:
try:
best_col = max(self.cpa[c].items(), key=operator.itemgetter(1))[0]
res = self.connector.query_relation(self.cea[(k, c[0])], best_col)
except:
res = []
for i in res:
if row[c[1]] is not np.nan and i['l']['value'] == row[c[1]]:
self.cea[(k, c[1])] = i['o']['value']
break
column_entity = {}
for c in self.cea:
if c[1] not in column_entity:
column_entity[c[1]] = set()
column_entity[c[1]].add(self.cea[c])
for col in column_entity:
try:
self.cta.add((self.name, col, self.generate_embedding(tuple(column_entity[col]), 1, 50, self.cta_filter).sum().idxmax().split('§')[-1]))
except:
None
print(self.name)
print(self.maincol)
def _export_cea(self, prefix):
with open(prefix+"_cea.txt", "a") as myfile:
for c in self.cea:
myfile.write('"' + self.name + '","' + str(c[0]) + '","' + str(c[1]) + '","')
myfile.write(self.cea[c])
myfile.write('"\n')
def _export_cpa(self, prefix):
with open(prefix + "_cpa.txt", "a") as myfile:
for c in self.cpa:
res = max(self.cpa[c].items(), key=operator.itemgetter(1))[0]
myfile.write('"' + self.name + '","' + str(c[0]) + '","' + str(c[1]) + '","')
myfile.write(res)
myfile.write('"\n')
def _export_cta(self, prefix):
with open(prefix + "_cta.txt", "a") as myfile:
for c in self.cta:
myfile.write('"' + str(c[0]) + '","' + str(c[1]) + '","' + str(c[2]))
myfile.write('"\n')
def export_files(self, prefix):
self._export_cea(prefix)
self._export_cta(prefix)
self._export_cpa(prefix)