-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathappend_new_normal_form.py
75 lines (62 loc) · 2.73 KB
/
append_new_normal_form.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
import argparse
import os
import pandas as pd
import json
from pandas.core.frame import DataFrame
from fol.foq_v2 import (concate_n_chains, copy_query,
negation_sink,
binary_formula_iterator,
concate_iu_chains,
decompose_D,
parse_formula,
projection_sink, to_D,
union_bubble,
DeMorgan_replacement,
to_d,
transformation)
parser = argparse.ArgumentParser()
parser.add_argument("--benchmark_name", type=str, default="benchmark")
parser.add_argument("--input_formula_file", type=str, default="outputs/generated_formula_anchor_node=3.csv")
parser.add_argument("--knowledge_graph", action="append")
def convert_query(df,
old_form_name='DNF+MultiIUD',
new_form_name='DNF+MultiIUd',
convert_functional=decompose_D):
def convertor(f):
query_instance = parse_formula(f)
query_instance = convert_functional(query_instance)
return query_instance.formula
df[new_form_name] = df[old_form_name].map(convertor)
return df
def convert_grounded_query(df,
old_form_name='DNF+MultiIUD',
new_form_name='DNF+MultiIUd',
old_form_formula=None,
convert_functional=None):
assert old_form_formula is not None
assert convert_functional is not None
def grounded_convertor(f):
query_instance = parse_formula(old_form_formula)
query_instance.additive_ground(json.loads(f))
query_instance = convert_functional(query_instance)
return query_instance.dumps
df[new_form_name] = df[old_form_name].map(grounded_convertor)
return df
if __name__ == "__main__":
args = parser.parse_args()
target_folder = f"data/{args.benchmark_name}"
formula_file = args.input_formula_file
df = pd.read_csv(formula_file)
df = convert_query(df)
df.to_csv(formula_file, index=False)
for kg in args.knowledge_graph:
folder = os.path.join(target_folder, kg)
for i, row in df.iterrows():
print(row.formula_id)
data_file = f"data-{row.formula_id}.csv"
data_df = pd.read_csv(os.path.join(folder, data_file))
converted_data_df = convert_grounded_query(
data_df,
old_form_formula=row['DNF+MultiIUD'],
convert_functional=decompose_D)
converted_data_df.to_csv(os.path.join(folder, data_file), index=False)