-
Notifications
You must be signed in to change notification settings - Fork 61
/
Copy pathsynthesize_logic_corpus.py
185 lines (155 loc) · 6.36 KB
/
synthesize_logic_corpus.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
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
from z3 import *
import random
from random import shuffle
from itertools import combinations, product
from typing import List, Tuple
from functools import partial
from tqdm import tqdm
import os
solver = Solver()
vars_all_candidates = [chr(i) for i in list(range(97, 122))]
for symbol in vars_all_candidates:
# init all variables
exec("{0} = Bool('{0}')".format(symbol))
def sample_single_logic(var_inputs: Tuple):
var_1, var_2 = var_inputs
# given two vars, sample a logic to represent these
# decide order of two vars
logic_var_1 = var_1
logic_var_2 = var_2
if random.random() > 0.5:
var_1 = "not {}".format(var_1)
logic_var_1 = "Not({})".format(logic_var_1)
if random.random() > 0.5:
var_2 = "not {}".format(var_2)
logic_var_2 = "Not({})".format(logic_var_2)
# implies of two logic var
if random.random() > 0.5:
var_1, var_2 = var_2, var_1
logic_var_1, logic_var_2 = logic_var_2, logic_var_1
text = "( {} -> {} ) ;".format(var_1, var_2)
logic = "Implies({}, {})".format(logic_var_1, logic_var_2)
return logic, text
def sample_simple_hypo(var_candidates: List):
# sample 1 or 2
sample_num = 1 if random.random() < 0.75 else 2
var_combinations = list(combinations(var_candidates, 2))
shuffle(var_combinations)
if sample_num == 1 or len(var_combinations) == 1:
# select the first one as the var_candidates
var_1, var_2 = var_combinations[0]
return sample_single_logic((var_1, var_2))
sample_predicate = "And" if random.random() < 0.75 else "Or"
var_1, var_2 = var_combinations[0]
logic_1 = sample_single_logic((var_1, var_2))
var_1, var_2 = var_combinations[1]
logic_2 = sample_single_logic((var_1, var_2))
# build both
logic_part = sample_predicate + "({}, {})".format(logic_1[0], logic_2[0])
if sample_predicate == "And":
text_part = logic_1[1] + " " + logic_2[1]
else:
text_part = logic_1[1] + " or " + logic_2[1]
return logic_part, text_part
def validate_statements(fact: List, hypo_linear: str = None):
solver.reset()
# construct the overall statement
if hypo_linear is None:
fact_linear = ", ".join(fact)
logic_state = "And({})".format(fact_linear)
exec("solver.add(" + logic_state + ")")
result = solver.check()
if result.r == 1:
return True
else:
return False
else:
fact_linear = ", ".join(fact)
logic_state = "Not(Implies(And({0}), {1}))".format(fact_linear, hypo_linear)
exec("solver.add(" + logic_state + ")")
result = solver.check()
if result.r == -1:
return True
else:
return False
def sample_example():
vars_candidates = [chr(i) for i in list(range(97, 122))]
shuffle(vars_candidates)
# take the first 3-4 to construct the complete logic rules
# total_var_count = random.randint(4, 9)
total_var_count = 4
use_var_count = min(random.randint(2, total_var_count - 2), 4)
used_vars = vars_candidates[:use_var_count]
# take the 4-10 to construct negative examples
unused_vars = vars_candidates[use_var_count: total_var_count]
# for each pair of vars, firstly we construct var pairs
used_combines = list(combinations(used_vars, 2))
# we should verify that the logic is valid
context_logic_is_valid = False
logic_facts = []
text_facts = []
while not context_logic_is_valid:
shuffle(used_combines)
# take 3~5 from them
count_combines = random.randint(3, 6)
used_combines = used_combines[: count_combines]
logic_facts = list(map(sample_single_logic, used_combines))
# if the logic in context is valid, break
logic_facts, text_facts = zip(*logic_facts)
context_logic_is_valid = validate_statements(logic_facts)
logic_facts = list(logic_facts)
text_facts = list(text_facts)
# add some other facts
unused_combines = list(combinations(unused_vars, 2))
shuffle(unused_combines)
unused_text_facts = list(map(sample_single_logic, unused_combines[: random.randint(5, 15)]))
_, unused_text_facts = zip(*unused_text_facts)
unused_text_facts = list(unused_text_facts)
# add some used var and unused var facts which cannot affect the logic
add_negative_count = random.randint(1, 2)
all_combines = list(product(vars_candidates[:use_var_count], vars_candidates[use_var_count:total_var_count]))
shuffle(all_combines)
while add_negative_count > 0:
# try to add one
temp_logic_facts = logic_facts[:]
cur_logic, cur_text = sample_single_logic(all_combines[-add_negative_count])
temp_logic_facts.append(cur_logic)
if validate_statements(temp_logic_facts):
# update logic facts and textual facts
logic_facts.append(cur_logic)
text_facts.append(cur_text)
add_negative_count -= 1
# sample logic hypo
logic_hypo, text_hypo = None, ""
while logic_hypo is None:
logic_hypo, text_hypo = sample_simple_hypo(used_vars)
if logic_hypo in logic_facts:
# too trivial to verify
logic_hypo = None
# give an answer
text_facts = list(text_facts + unused_text_facts)
shuffle(text_facts)
text_final = text_hypo + " [SEP] " + " ".join(text_facts)
answer_final = "1" if validate_statements(logic_facts, logic_hypo) else "0"
return text_final, answer_final
def convert_logical_data(output_dir):
if not os.path.exists(output_dir):
os.makedirs(output_dir)
train_src_f = open(os.path.join(output_dir, "train.raw.input0"), "w", encoding="utf8")
train_tgt_f = open(os.path.join(output_dir, "train.label"), "w", encoding="utf8")
dev_src_f = open(os.path.join(output_dir, "dev.raw.input0"), "w", encoding="utf8")
dev_tgt_f = open(os.path.join(output_dir, "dev.label"), "w", encoding="utf8")
for _ in tqdm(range(100000)):
input_line, output_line = sample_example()
train_src_f.write(input_line + "\n")
train_tgt_f.write(output_line + "\n")
for _ in tqdm(range(2000)):
input_line, output_line = sample_example()
dev_src_f.write(input_line + "\n")
dev_tgt_f.write(output_line + "\n")
train_src_f.close()
train_tgt_f.close()
dev_src_f.close()
dev_tgt_f.close()
if __name__ == '__main__':
convert_logical_data("pretrain_logic")