-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathStreamlitMAGIC.py
More file actions
198 lines (169 loc) · 8.34 KB
/
StreamlitMAGIC.py
File metadata and controls
198 lines (169 loc) · 8.34 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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
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
import streamlit as st
class tqdm:
def __init__(self, iterable, length=None, title=None):
if title:
st.write(title)
self.prog_bar = st.progress(0)
self.iterable = iterable
if length:
self.length = length
else:
self.length = len(iterable)
self.i = 0
def __iter__(self):
for obj in self.iterable:
yield obj
self.i += 1
current_prog = self.i / self.length
self.prog_bar.progress(current_prog)
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
self.embeddings = {}
@lru_cache(maxsize=128)
def generate_embedding(self, data, depth, jobs, filter=None):
extractor = InkExtractor(self.connector, verbose=True)
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)
print(df)
data = {}
for k, row in tqdm(df.iterrows(), length=len(df), title='Get Candidates'):
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:
st.write('Generate embeddings for '+str(len(all_entities))+' candidates')
full_emb = self.generate_embedding(tuple(all_entities),2,3)
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 tqdm(df.iterrows(), length=len(df), title='Get cell annotations'):
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 tqdm(df.iterrows(), length=len(df), title='Get relationships annotations'):
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
self.column_entity = {}
all_entities = set()
for c in self.cea:
if c[1] not in self.column_entity:
self.column_entity[c[1]] = set()
self.column_entity[c[1]].add(self.cea[c])
all_entities.add(self.cea[c])
self.skiplist.extend(['http://dbpedia.org/property/wikiPageUsesTemplate','http://dbpedia.org/ontology/wikiPageWikiLink'])
print(self.skiplist)
self.embeddings = self.generate_embedding(tuple(all_entities), 2, 3, 'http://dbpedia.org/property')
for col in tqdm(self.column_entity, length=len(self.column_entity), title='Get column annotations'):
try:
self.cta.add((self.name, col, self.generate_embedding(tuple(self.column_entity[col]), 1, 3, self.cta_filter).sum().idxmax().split('§')[-1]))
except:
None
print(self.name)
print(self.maincol)
def augement(self, base_column):
df = self.embeddings.loc[self.column_entity[base_column]]
dct = {}
tf_cols = df.loc[:, (df.sum(axis=0) == len(self.column_entity[base_column]))].columns
for x in tf_cols:
try:
print(x)
df2 = df.filter(regex=x + '§')
pdf2 = df2.idxmax(1).str.split('§').str[1].to_frame()
pdf2.columns = [x]
dct[x] = pdf2
except:
None
return dct
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)