Skip to content
Snippets Groups Projects
Commit d84105f4 authored by chsieh16's avatar chsieh16
Browse files

Refactor file creation and feature names

parent c9830a80
No related branches found
No related tags found
No related merge requests found
......@@ -6,31 +6,32 @@ import csv
import grammar_generation_utils
import json
class DTreeLearner(LearnerBase):
def __init__(self, state_dim: int, perc_dim: int,
timeout: int = 10000) -> None:
super().__init__()
self._state_dim: int = state_dim
self._perc_dim: int = perc_dim
self._s2f_func_list: List = []
#check tempLocation dir exists, if not create it.
self.dir_name = "tempLocation"
check_temp_dir: bool = os.path.isdir(self.dir_name)
if not check_temp_dir:
# check directory name exists, if not create it.
self.dir_name = "out"
if not os.path.isdir(self.dir_name):
os.makedirs(self.dir_name)
# create empty files
self.data_file = self.dir_name+"/pre.data"
self.names_file = self.dir_name+"/pre.names"
open(self.data_file,'w').close()
open(self.names_file,'w').close()
path_prefix = self.dir_name+"/pre"
self.data_file = path_prefix + ".data"
self.names_file = path_prefix + ".names"
self.tree_out = path_prefix + ".json"
self.exec = 'c50exact/c5.0dbg -I 1 -m 1 -f tempLocation/pre'
# create empty files or truncate existing contents in files
open(self.data_file, 'w').close()
open(self.names_file, 'w').close()
self.tree_out = "tempLocation/pre.json"
self.exec = f'c50exact/c5.0dbg -I 1 -m 1 -f {path_prefix}'
@property
def state_dim(self) -> int:
......@@ -41,16 +42,13 @@ class DTreeLearner(LearnerBase):
return self._perc_dim
def set_grammar(self, grammar) -> None:
count = 1
numerical_vars = []
for a_mat, b_vec in grammar:
numerical_vars: List[str] = []
for count, (a_mat, b_vec) in enumerate(grammar):
self._s2f_func_list.append(
construct_sample_to_feature_func(a_mat, b_vec))
numerical_vars.extend(f"fvar{i}_A{count}B{count}"
for i in range(self.perc_dim))
numerical_vars.append("dbar"+f'A{count}B{count}')
numerical_vars.append("psibar"+f'A{count}B{count}')
count += 1
grammar_generation_utils.generateInputLanguageFile(self.names_file, numerical_vars, [])
def add_implication_examples(self, *args) -> None:
......@@ -65,7 +63,7 @@ class DTreeLearner(LearnerBase):
feature_vec_list.append(feature_vec)
print("Positive feature vectors (dbar, psibar):", feature_vec_list)
self.write_to_file(self.data_file,feature_vec_list, "true")
self.write_to_file(self.data_file, feature_vec_list, "true")
def add_negative_examples(self, *args) -> None:
feature_vec_list = []
......@@ -78,14 +76,13 @@ class DTreeLearner(LearnerBase):
print("Negative feature vectors (dbar, psibar):", feature_vec_list)
self.write_to_file(self.data_file, feature_vec_list, "false")
def write_to_file(self, file:str, feature_vec_list, label:str ):
def write_to_file(self, file: str, feature_vec_list, label: str):
with open(file, 'a') as d_file:
data_out = csv.writer(d_file)
for f in feature_vec_list:
print(f)
data_out.writerow(f+[label])
def learn(self) -> Tuple:
res = os.popen(self.exec).read()
print(res)
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment