class DecisionTree:
    def __init__(self):
        self.log = LoggerUtil(self.__class__.__name__).get()
        self.config = ConfigUtil.get_config_instance()
        self.helper = Helper()
        self.metrics = Metrics()

    @staticmethod
    def train_model(x_train, y_train):
        model = DecisionTreeClassifier()
        model.fit(x_train, y_train)
        return model

    @staticmethod
    def test_model(model, x_test):
        return model.predict(x_test)

    def main(self, x_train, x_test, y_train, y_test):
        image_path = self.config["image_path"]
        model = self.train_model(x_train, y_train)

        self.log.info("{} Model performance on test data".format(self.__class__.__name__))
        y_pred = self.test_model(model, x_test)
        acc_score, cr_report, cnf_matrix = self.metrics.metrics(y_true=y_test, y_predicted=y_pred)
        self.helper.plot_save_cnf_matrix(cnf_matrix, flag="test", model_name=self.__class__.__name__,
                                         image_path=image_path)
        return {
            "model": model,
            "metrics": {
                "accuracy": acc_score,
                "classification_report": cr_report,
                "confusion_matrix": cnf_matrix
            }
        }
class TestModel:
    def __init__(self):
        self.log = LoggerUtil(self.__class__.__name__).get()
        self.config = ConfigUtil.get_config_instance()
        self.model = LinguisticModel()
        self.read_data = ReadData()
        self.metrics = Metrics()
        self.helper = Helper()
        self.logic = Logic()

    def check_if_trained(self):
        models_path = self.config["models_path"]
        model = joblib.load(models_path + "/" + "mlp.mdl")
        vectorizer = joblib.load(models_path + "/" + "vectorizer.mdl")
        return vectorizer, model

    def main(self, test=False):
        if test:
            nlp = load("en_core_web_sm")
            vectorizer, model = self.check_if_trained()
            self.log.info("Please enter the sentence")
            sentence = str(input())
            tokens = self.read_data.transform_sentence(sentence)
            features = vectorizer.transform(tokens)
            predictions = model.predict(features)
            ling_pred = self.logic.apply_rules(text_tokens=tokens, nlp=nlp)
            self.log.info("Given sentence : {}".format(sentence))
            self.log.info(
                "Prediction of Linguistic Model : {}".format(ling_pred))
            self.log.info("Prediction of ML Model : {}".format(
                any(predictions)))
            self.log.info("Final Prediction : {}".format(ling_pred
                                                         or any(predictions)))
        else:
            model, vectorizer = self.model.main()
            tagged_data_df = self.read_data.prepare_tagged_data()
            features = vectorizer.transform(tagged_data_df["data"])
            labels = tagged_data_df["labels"]
            predictions = model.predict(features)

            acc_score, cr_report, cnf_matrix = self.metrics.metrics(
                y_true=labels, y_predicted=predictions)
            self.helper.plot_save_cnf_matrix(
                cnf_matrix=cnf_matrix,
                model_name="satwik",
                flag="test",
                image_path=
                "/home/satwik/Documents/Hiring/huddl_assignment/Images/")