private void SelectFile(object sender, RoutedEventArgs e) { mk = null; // ensure no old models are used with a new file OpenFileDialog openFileDailog = new OpenFileDialog(); openFileDailog.FileName = ""; // Default file name openFileDailog.DefaultExt = ".txt"; // Default file extension openFileDailog.Filter = "Text documents (.txt)|*.txt"; // Filter files by extension Nullable <bool> result = openFileDailog.ShowDialog(); if (result == true) { // Open document string filepath = openFileDailog.FileName; try { text = File.ReadAllText(filepath); filepath_textBox.Text = filepath; } catch (IOException ioEx) { MessageBox.Show("There was a problem reading the slected file. Error: " + ioEx.Message); text = null; filepath_textBox.Text = ""; } } } // end SelectFile()
} // end SelectFile() private void TrainModel(object sender, RoutedEventArgs e) { if (text == null) { MessageBox.Show("There is no text available for training."); return; } mk = new Markov(text); try { mk.Train(); MessageBox.Show("Successfully Trained Markov model. You can now generate sentences."); trainingStatus_label.Content = "Trained"; } catch (Exception ex) { MessageBox.Show(ex.Message); trainingStatus_label.Content = "Not Trained"; } } // end TrainModel()