예제 #1
0
        private static void CreateTreeAndHandleUserOperation(DataTable data)
        {
            var decisionTree = new DecisionTreeId3(data);

            var returnToMainMenu = false;

            Console.ForegroundColor = ConsoleColor.Green;
            Console.WriteLine("\nDecision tree created");
            Console.ResetColor();

            do
            {
                var valuesForQuery = new Dictionary <string, string>();

                // loop for data input for the query and some special commands
                for (var i = 0; i < data.Columns.Count - 1; i++)
                {
                    Console.ForegroundColor = ConsoleColor.Cyan;
                    Console.WriteLine(
                        $"\nEnter your value for {data.Columns[i]} or help for a list of the additional instructions");
                    Console.ResetColor();
                    var input = ReadLineTrimmed();

                    if (input.ToUpper().Equals("ENDPROGRAM"))
                    {
                        EndProgram();
                    }
                    else if (input.ToUpper().Equals("PRINT"))
                    {
                        Console.WriteLine();
                        DecisionTreeId3.PrintTree(decisionTree.Root);
                        i--;
                    }
                    else if (input.ToUpper().Equals("MAINMENU"))
                    {
                        returnToMainMenu = true;
                        Console.WriteLine();

                        break;
                    }
                    else if (input.ToUpper().Equals("HELP"))
                    {
                        Console.ForegroundColor = ConsoleColor.Yellow;
                        Console.WriteLine("\nExportData to export your tree");
                        Console.ForegroundColor = ConsoleColor.Magenta;
                        Console.WriteLine("Print to print the tree");
                        Console.ForegroundColor = ConsoleColor.Yellow;
                        Console.WriteLine("EndProgram to end the program");
                        Console.ForegroundColor = ConsoleColor.Magenta;
                        Console.WriteLine("MainMenu to return to the main menu");
                        Console.ForegroundColor = ConsoleColor.Gray;

                        i--;
                    }
                    else if (input.ToUpper().Equals("EXPORTDATA"))
                    {
                        Console.ForegroundColor = ConsoleColor.Cyan;
                        Console.WriteLine("\nEnter path for your csv file");
                        Console.ResetColor();
                        input = ReadLineTrimmed();

                        var endOfDirectoryPath = input.LastIndexOf("\\");

                        if (endOfDirectoryPath > 0 && Directory.Exists(input.Substring(0, endOfDirectoryPath)))
                        {
                            try
                            {
                                CsvFileHandler.ExportToCsvFile(data, input);
                            }
                            catch (Exception ex)
                            {
                                DisplayErrorMessage(
                                    $"An error during the export occured. \nError message: {ex.Message}");
                            }
                        }
                        else
                        {
                            DisplayErrorMessage("Directory not found");
                        }

                        i--;
                    }
                    else if (string.IsNullOrEmpty(input) || string.IsNullOrWhiteSpace(input))
                    {
                        DisplayErrorMessage("The attribute can't be empty or a white space");
                        i--;
                    }
                    else
                    {
                        valuesForQuery.Add(data.Columns[i].ToString(), input);
                    }
                }

                // if input was not to return to the main menu, the query will be processed
                if (!returnToMainMenu)
                {
                    var answer = decisionTree.GetAnswer(valuesForQuery);
                    Console.ForegroundColor = ConsoleColor.Green;
                    Console.WriteLine("Predict is " + answer.Reason + " -> " + answer.Percent + "%");
                    Console.ResetColor();
                }
            } while (!returnToMainMenu);
        }
예제 #2
0
        public static void Main(string[] args)
        {
            Console.WindowWidth     = Console.LargestWindowWidth - 10;
            Console.ForegroundColor = ConsoleColor.Yellow;
            Console.WriteLine("Welcome to the decision tree calculator");
            Console.WriteLine("---------------------------------------");
            Console.ResetColor();

            do
            {
                var data = new DataTable();

                Console.ForegroundColor = ConsoleColor.Cyan;
                Console.WriteLine("1 - Import data from csv file");
                Console.ForegroundColor = ConsoleColor.Yellow;
                Console.WriteLine("2 - Enter data manually");
                Console.ForegroundColor = ConsoleColor.Cyan;
                Console.WriteLine("3 - End program");
                Console.ResetColor();
                var input = ReadLineTrimmed();

                switch (input)
                {
                // data will be imported from csv file
                case "1":
                    Console.ForegroundColor = ConsoleColor.Cyan;
                    Console.WriteLine("\nEnter the path to the .csv file which you want to import");
                    Console.ResetColor();
                    input = ReadLineTrimmed();

                    data = CsvFileHandler.ImportFromCsvFile(input);

                    if (data == null)
                    {
                        DisplayErrorMessage(
                            "An error occured while importing the data from the .csv file. Press any key to close the program.");
                        Console.ReadKey();
                        EndProgram();
                    }
                    else
                    {
                        CreateTreeAndHandleUserOperation(data);
                    }

                    break;

                // user enters data by hand
                case "2":
                    do
                    {
                        Console.ForegroundColor = ConsoleColor.Yellow;
                        Console.WriteLine("\n1 - Enter data");
                        Console.ForegroundColor = ConsoleColor.Cyan;
                        Console.WriteLine("2 - Create decision tree");
                        Console.ForegroundColor = ConsoleColor.Cyan;
                        Console.WriteLine("3 - Export to csv file");
                        Console.ForegroundColor = ConsoleColor.Yellow;
                        Console.WriteLine("4 - End program");
                        Console.ResetColor();
                        input = ReadLineTrimmed();

                        switch (input)
                        {
                        // user enters data by hand
                        case "1":
                            Console.ForegroundColor = ConsoleColor.Cyan;
                            Console.WriteLine("\nHow many columns do you want to enter?");
                            Console.ResetColor();
                            input = ReadLineTrimmed();

                            int amountOfColumns;
                            var rightInput = int.TryParse(input, out amountOfColumns);

                            if (rightInput && amountOfColumns > 1)
                            {
                                data = EnterColumnTitle(amountOfColumns);
                                data = EnterRowValues(data);

                                CreateTreeAndHandleUserOperation(data);
                            }
                            else
                            {
                                DisplayErrorMessage(
                                    "Wrong input. Amount of columns must be an integer greater than 1");
                            }

                            break;

                        // after data input decision tree can be created
                        case "2":
                            if (data?.Rows.Count > 0)
                            {
                                CreateTreeAndHandleUserOperation(data);
                            }
                            else
                            {
                                DisplayErrorMessage("You don't have data entered yet");
                            }

                            break;

                        // after data input the data can be exported into a csv file
                        case "3":
                            Console.ForegroundColor = ConsoleColor.Cyan;
                            Console.WriteLine("\nEnter path for your csv file");
                            Console.ResetColor();
                            input = ReadLineTrimmed();

                            var endOfDirectoryPath = input.LastIndexOf("\\");

                            if (endOfDirectoryPath > 0 &&
                                Directory.Exists(input.Substring(0, endOfDirectoryPath)))
                            {
                                try
                                {
                                    CsvFileHandler.ExportToCsvFile(data, input);
                                }
                                catch (Exception ex)
                                {
                                    DisplayErrorMessage(
                                        $"An error during the export occured. Error message: {ex.Message}");
                                }
                            }
                            else
                            {
                                DisplayErrorMessage("Directory not found");
                            }

                            break;

                        case "4":
                            EndProgram();
                            break;

                        default:
                            DisplayErrorMessage("Wrong input");
                            break;
                        }
                    } while (true);

                case "3":
                    EndProgram();
                    break;

                default:
                    DisplayErrorMessage("Wrong input");
                    break;
                }
            } while (true);
        }
예제 #3
0
        private static void CreateTreeAndHandleUserOperation(DataTable data)
        {
            var decisionTree = new Tree();

            decisionTree.Root = Tree.Learn(data, "");
            var returnToMainMenu = false;

            Console.ForegroundColor = ConsoleColor.Green;
            Console.WriteLine("\nDecision tree created");
            Console.ResetColor();

            do
            {
                var valuesForQuery = new Dictionary <string, string>();

                // loop for data input for the query and some special commands
                for (var i = 0; i < data.Columns.Count - 1; i++)
                {
                    Console.ForegroundColor = ConsoleColor.Cyan;
                    Console.WriteLine($"\nEnter your value for {data.Columns[i]} or help for a list of the additional instructions");
                    Console.ResetColor();
                    var input = ReadLineTrimmed();

                    if (input.ToUpper().Equals("ENDPROGRAM"))
                    {
                        EndProgram();
                    }
                    else if (input.ToUpper().Equals("PRINT"))
                    {
                        Console.WriteLine();
                        Tree.Print(decisionTree.Root, decisionTree.Root.Name.ToUpper());
                        Tree.PrintLegend("Due to the limitation of the console the tree is displayed as a list of every possible route. The colors indicate the following values:");

                        i--;
                    }
                    else if (input.ToUpper().Equals("MAINMENU"))
                    {
                        returnToMainMenu = true;
                        Console.WriteLine();

                        break;
                    }
                    else if (input.ToUpper().Equals("HELP"))
                    {
                        Console.ForegroundColor = ConsoleColor.Yellow;
                        Console.WriteLine("\nExportData to export your tree");
                        Console.ForegroundColor = ConsoleColor.Magenta;
                        Console.WriteLine("Print to print the tree");
                        Console.ForegroundColor = ConsoleColor.Yellow;
                        Console.WriteLine("EndProgram to end the program");
                        Console.ForegroundColor = ConsoleColor.Magenta;
                        Console.WriteLine("MainMenu to return to the main menu");
                        Console.ForegroundColor = ConsoleColor.Gray;

                        i--;
                    }
                    else if (input.ToUpper().Equals("EXPORTDATA"))
                    {
                        Console.ForegroundColor = ConsoleColor.Cyan;
                        Console.WriteLine("\nEnter path for your csv file");
                        Console.ResetColor();
                        input = ReadLineTrimmed();

                        var endOfDirectoryPath = input.LastIndexOf("\\");

                        if (endOfDirectoryPath > 0 && Directory.Exists(input.Substring(0, endOfDirectoryPath)))
                        {
                            try
                            {
                                CsvFileHandler.ExportToCsvFile(data, input);
                            }
                            catch (Exception ex)
                            {
                                DisplayErrorMessage($"An error during the export occured. \nError message: {ex.Message}");
                            }
                        }
                        else
                        {
                            DisplayErrorMessage("Directory not found");
                        }

                        i--;
                    }
                    else if (string.IsNullOrEmpty(input) || string.IsNullOrWhiteSpace(input))
                    {
                        DisplayErrorMessage("The attribute can't be empty or a white space");
                        i--;
                    }
                    else
                    {
                        valuesForQuery.Add(data.Columns[i].ToString(), input);
                    }
                }

                // if input was not to return to the main menu, the query will be processed
                if (!returnToMainMenu)
                {
                    var result = Tree.CalculateResult(decisionTree.Root, valuesForQuery, "");

                    Console.WriteLine();

                    if (result.Contains("Attribute not found"))
                    {
                        DisplayErrorMessage("Can't caluclate outcome. Na valid route through the tree was found");
                    }
                    else
                    {
                        Tree.Print(null, result);
                        Tree.PrintLegend("The colors indicate the following values:");
                    }
                }
            } while (!returnToMainMenu);
        }