/// <summary> /// Gets a network based on the given path /// </summary> /// <param name="path"></param> /// <returns></returns> public Bayesian LoadExistingNetwork(string path) { Bayesian bn = new Bayesian(); //gets the object in its text format from the file string bnJsonString = File.ReadAllText(path); //converts the text back to the object bn = JsonConvert.DeserializeObject <Bayesian>(bnJsonString); return(bn); }
/// <summary> /// saves the given network to a txt file /// </summary> /// <param name="bn">the network to be saved</param> /// <returns></returns> public bool SaveNetworkKnowledge(Bayesian bn) { string path; bool fileSaved = false; //checks if the file type is included if (!bn.FileName.EndsWith(".txt")) { path = _trainedFilePath + "/" + bn.FileName + ".txt"; } else { path = _trainedFilePath + "/" + bn.FileName; } try { if (!File.Exists(path)) { //converts the object to be placed into a file string bnJsonString = JsonConvert.SerializeObject(bn); //creates the file FileStream s = File.Create(path); s.Close(); //saves the bayesian network to the file StreamWriter sw = File.AppendText(path); sw.WriteLine(bnJsonString); sw.Close(); fileSaved = true; } } catch (Exception e) { Debug.WriteLine("Error : " + e); } return(fileSaved); }
/// <summary> /// The main menu for the program /// </summary> public void DisplayMenu() { bool displayMenu = true; string uInput; do { Console.WriteLine("Please Select an Option or press q to quit"); Console.WriteLine("1. Train"); Console.WriteLine("2. Generate A haiku"); Console.WriteLine("q. quit"); uInput = Console.ReadLine(); switch (uInput.ToLower()) { case "1": _bn.Train(_frw.LoadTrainingData()); if (_bn.Words == null || _bn.Words.Count == 0) { Console.Clear(); Console.WriteLine("\nERROR: No training Data Provided"); Console.ReadKey(); } SaveNetwork(); break; case "2": Console.Clear(); string[] availableNetworks = _frw.TrainedBayesianNetworkNames(); int networkNum = -100; if (availableNetworks.Count() != 0) { //list the already saved networks Console.WriteLine("Select the Network you'd like to use"); for (int i = 1; i <= availableNetworks.Count(); i++) { Console.WriteLine(i + ". " + availableNetworks[i - 1]); } bool outOfBounds = true; //validate the userinput do { uInput = Console.ReadLine(); int.TryParse(uInput, out networkNum); if ((networkNum > 0) && (networkNum <= availableNetworks.Count())) { outOfBounds = false; } else { Console.WriteLine("invalid input"); } } while(outOfBounds); Console.Clear(); Console.WriteLine("Press any key to exit. You're haiku is:"); //TEMP //for (int i = 0; i < 30; i++) //{ //setting the network _bn = _frw.LoadExistingNetwork(availableNetworks[networkNum - 1]); List <string[]> haiku = _bn.CreateHaiku(); Console.WriteLine(); foreach (string[] line in haiku) { Console.WriteLine(); foreach (string word in line) { Console.Write(word + " "); } } //} //TEMP Console.ReadKey(); } else { Console.WriteLine("ERROR: No trained networks available."); Console.ReadKey(); } break; case "q": displayMenu = false; break; default: Console.WriteLine("Invalid menu option"); break; } Console.Clear(); } while (displayMenu); }
/// <summary> /// Instanciates the member variables and allows /// </summary> public Menu() { _frw = new FileReadWrite(); _bn = new Bayesian(); }