File size: 2,829 Bytes
373bd8e 8225751 373bd8e 8225751 373bd8e 8225751 373bd8e 8225751 373bd8e 2bad6a1 8e24b9b |
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 |
import json
import pandas as pd
# load manual check sheet
df_predicate = pd.read_csv('predicate_manual_check.csv')
df_predicate = df_predicate[df_predicate['remove (noisy)'] != 'x']
df_predicate = df_predicate[df_predicate['remove (too vague)'] != 'x']
predicate_main = df_predicate[df_predicate['ok'] == 'x']['unique predicates'].tolist()
df_sub = df_predicate[df_predicate['ok'] != 'x']
df_sub_same = df_sub[['unique predicates', 'same as']].dropna()
df_sub_same = df_sub_same[[i in predicate_main for i in df_sub_same['same as']]]
df_sub_same.index = df_sub_same.pop('unique predicates')
sub_same = df_sub_same['same as'].to_dict()
df_sub_rev = df_sub[['unique predicates', 'reverse of']].dropna()
df_sub_rev = df_sub_rev[[i in predicate_main for i in df_sub_rev['reverse of']]]
df_sub_rev.index = df_sub_rev.pop('unique predicates')
sub_rev = df_sub_rev['reverse of'].to_dict()
# load data and filter based on manual predicate check sheet
with open(f"data/t_rex.filter.jsonl") as f:
data = pd.DataFrame([json.loads(i) for i in f.read().split('\n') if len(i) > 0])
data['predicate'] = [sub_same[i] if i in sub_same else i for i in data['predicate']]
data['reverse'] = [i in sub_rev for i in data['predicate']]
data['predicate'] = [sub_rev[i] if i in sub_rev else i for i in data['predicate']]
data_remain = data[[i not in predicate_main for i in data['predicate']]]
data_remain = data_remain[[i.startswith("P") for i in data_remain['predicate']]]
data = data[[i in predicate_main for i in data['predicate']]]
data_rev = data[data['reverse']].copy()
o = data_rev.pop("object")
s = data_rev.pop("subject")
data_rev["subject"] = o
data_rev["object"] = s
data[data['reverse']] = data_rev[data.columns]
data.pop("reverse")
df_main = df_predicate[df_predicate['ok'] == 'x'][['unique predicates', 'pretty relation name', 'pretty relation name is reverse']]
df_main['reverse'] = [i == 'x' for i in df_main.pop('pretty relation name is reverse')]
df_main['predicate'] = df_main.pop('unique predicates')
data_filter_join = data.merge(df_main, how='inner', on='predicate')
data_filter_join_rev = data_filter_join[data_filter_join['reverse']].copy()
o = data_filter_join_rev.pop("object")
s = data_filter_join_rev.pop("subject")
data_filter_join_rev["subject"] = o
data_filter_join_rev["object"] = s
data_filter_join[data_filter_join['reverse']] = data_filter_join_rev[data_filter_join.columns]
data_filter_join.pop("reverse")
data_filter_join.pop("predicate")
data_filter_join['predicate'] = data_filter_join.pop("pretty relation name")
new = pd.concat([data_filter_join, data_remain[data_filter_join.columns]])
print(f"{len(new)} triples, {len(new['predicate'].unique())} predicates")
with open(f"data/t_rex.filter_unified.jsonl", 'w') as f:
for _, i in new.iterrows():
f.write(json.dumps(i.to_dict()) + '\n')
|