/// <summary> /// retrieves the networks from the folder. /// </summary> /// <returns>returns the bayesingNetwork array</returns> public BayesingNetwork[] GetSavedBayesingNetworks() { //list of networks List <BayesingNetwork> bayesingNetworks = new List <BayesingNetwork>(); try { //d is the directory which holds the networks infomation foreach (string d in Directory.GetDirectories(_BayesingNetworkFolder)) { //contains the categories for the networks List <CategoryObj> cat = new List <CategoryObj>(); //file is the networks categories foreach (string file in Directory.EnumerateFiles(d, "*.txt")) { CategoryObj c = new CategoryObj(GetStopWords(), GetSuffixes()) { Name = Path.GetFileName(file) }; //collects the dictionary information for the categories string[] information = File.ReadAllLines(file); c.DocumentsUsed = int.Parse(information[0]); Dictionary <string, int> kvp = new Dictionary <string, int>(); for (int i = 1; i < information.Length; i++) { //splits the key from the value it holds string[] WordAndCountSplit = information[i].Split('+'); int amountOfWords = int.Parse(WordAndCountSplit[1]); kvp.Add(WordAndCountSplit[0], amountOfWords); } //new dictionary entry to be added to the category c.WordInformation = kvp; cat.Add(c); } BayesingNetwork bn = new BayesingNetwork(cat) { Name = d }; bayesingNetworks.Add(bn); } } catch (Exception) { Console.WriteLine("File error, please check the files are input correctly"); } return(bayesingNetworks.ToArray()); }
/// <summary> /// displays a list of posible networks for the user to choose from /// </summary> /// <param name="bayesingNetworks">A list of avalible networks</param> /// <returns>whether the menu needs to be reloaded</returns> bool ChooseABaysingNetwork(BayesingNetwork[] bayesingNetworks) { Console.Clear(); bool reloadMenu = true; //used to determine if the network needs reloaded string userInput = ""; //basic userinput int menuOption = 0; //keeps track of the amount of options //checks if it can display any networks if (bayesingNetworks.Count() > 0) { //lists all the available networks Console.WriteLine("Please select an AI to use"); foreach (BayesingNetwork bn in bayesingNetworks) { menuOption++; Console.WriteLine(menuOption + ". " + bn.Name); } userInput = Console.ReadLine(); //checks if the user selected a valid network if (int.TryParse(userInput, out int result) && result <= bayesingNetworks.Count()) { _bn = bayesingNetworks[result - 1]; reloadMenu = false; } else { Console.Clear(); Console.WriteLine("Invalid option"); Console.WriteLine("Returning to Main Menu"); Console.ReadKey(); } } else { Console.WriteLine("There are no trained AI available"); Console.WriteLine("Returning to Main Menu"); Console.ReadKey(); } return(reloadMenu); }
/// <summary> /// Loads the users main menu /// </summary> public void StartUp() { string menuOption; bool menu = true; bool failed = false; //Ensures the users keeps returning to the menu until they wish to quit. do { Console.Clear(); Console.WriteLine("Select an Option"); Console.WriteLine("1. Train"); Console.WriteLine("2. Classify text "); Console.WriteLine("Q to quit"); menuOption = Console.ReadLine().ToLower(); switch (menuOption) { case "1": //creates a new network _bn = new BayesingNetwork(); try { _bn.Train(); } catch (Exception) { failed = true; } if (failed) { Console.WriteLine("File error, unable to read training data"); } else { SaveBayesingNetwork(); } menu = true; break; case "2": //gets an already existing network menu = ChooseABaysingNetwork(frw.GetSavedBayesingNetworks()); if (!menu) { SelectText(); if (_bn != null) { //gets the result of the analysed text string[] result = _bn.GetAnalysedResult().ToArray(); Console.Clear(); if (result.Count() != 0) { //displays the result to the user Console.WriteLine("The results of the analysed text are"); int i = 1; foreach (string s in result) { Console.WriteLine(i + ". " + s); i++; } } else { Console.WriteLine("No results avalible."); } } } Console.ReadKey(); menu = true; break; case "q": //allows the user to escape the program Console.Clear(); Console.WriteLine("Exiting Program"); menu = false; break; default: Console.Clear(); Console.WriteLine("Invalid option. Please enter 1, 2 or q."); menu = true; break; } //Console.ReadKey(); } while (menu); }