/** * Make a new {@link StringArrayMap} resulting from training the model on all of the files stored * in the {@link #trainedTexts} array, but with the specified number of words in a prefix * * @param prefixLength Number defining the count of words in a prefix * @return The new {@link StringArrayMap} resulting from retraining */ public static StringArrayMap retrain(int prefixLength) { Prefix.numPref = prefixLength; Prefix.initializeSentenceStartArray(); StringArrayMap m = new StringArrayMap(); for (int i = 0; i < numTextsTrained; i++) { PrefixGenerator.trainPrefixMap(m, trainedTexts[i]); } return m; }
/** The main method used to run the engine */ public static void main(String[] args) { // scanner opened here, closed later when the program is selected to end Scanner in = new Scanner(System.in); while (true) { int decision = -1; // Prompt user input/action do { promptUser(); try { decision = in.nextInt(); } catch (Exception e) { decision = -1; } in.nextLine(); } while (decision < 0); switch (decision) { case 0: System.out.printf("\nProgram Ending\n"); in.close(); return; case 1: if (numTextsTrained < 1) { System.out.printf("Program has not been trained yet\n\n"); break; } System.out.printf("\nDynamically Generated Text\n\n"); for (int i = 0; i < numSentences; ++i) { String sentence = generateSentence(map); System.out.printf("%s\n\n", sentence); } break; case 2: String filename = null; do { System.out.print("Enter file name ('0' for menu): "); filename = in.next(); in.nextLine(); // Allow user to return to main menu if (filename.equals("0")) break; // Check if program has already been trained on this text if (haveTrainedText(filename)) { System.out.printf("Program has already been trained on this text\n\n"); filename = null; continue; } // Check that the file is valid File check = new File(filename); if (!check.isFile()) { System.out.printf("Invalid file name\n\n"); filename = null; continue; } PrefixGenerator.trainPrefixMap(map, filename); } while (filename == null); addTrainedTexts(filename); System.out.println(); break; case 3: int length = -1; do { System.out.print("Number of Words in Prefix: "); length = in.nextInt(); if (length <= 0) System.out.println("Invalid input"); in.nextLine(); } while (length <= 0); map = retrain(length); System.out.println("All texts re-trained\n"); break; case 4: int num = -1; do { System.out.print("Num. of Sentences: "); num = in.nextInt(); if (num < 0) System.out.println("Invalid input"); } while (num < 0); numSentences = num; System.out.println(); break; default: System.out.printf("Invalid program action\n\n"); } } }