From 8e9d0c1267a53dcb908aded87bb49711db0f7b25 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Czeka=C5=84ski?= Date: Sun, 24 May 2020 17:07:34 +0200 Subject: [PATCH] Add method appending examples to file --- src/AI/DecisionTrees/ExamplesManager.py | 28 ++++++++++++++++++++++++- 1 file changed, 27 insertions(+), 1 deletion(-) diff --git a/src/AI/DecisionTrees/ExamplesManager.py b/src/AI/DecisionTrees/ExamplesManager.py index c74a5e5..36dd08e 100644 --- a/src/AI/DecisionTrees/ExamplesManager.py +++ b/src/AI/DecisionTrees/ExamplesManager.py @@ -1,4 +1,4 @@ -from typing import Any, Union +from typing import Any, Union, List from src.AI.DecisionTrees.projectSpecificClasses.DistFromObject import DistFromObject from src.AI.DecisionTrees.projectSpecificClasses.PlayerStatsValue import PlayerStatsValue @@ -12,6 +12,11 @@ class ExamplesManager: self.examplesFilePath = examplesFilePath def readExamples(self): + """ + This method reads examples for decision tree learning from file. + + :return: List of read examples. + """ examples = [] file = open(self.examplesFilePath, "r") @@ -113,4 +118,25 @@ class ExamplesManager: examples.append(example) + file.close() + return examples + + def addExamplesToFile(self, examplesToAdd: List[SurvivalDTExample]): + """ + Appends given examples to examples file. + + :param examplesToAdd: + """ + file = open(self.examplesFilePath, "a+") + + example: SurvivalDTExample + for example in examplesToAdd: + strToWrite = "{}|{}|{}|{}|{}|{}|{}".format(example.classification.name, + example.hungerVal.name, + example.thirstVal.name, + example.staminaVal.name, + example.distFromFood.name, + example.distFromWater.name, + example.distFromRestPlace.name) + file.close()