/** Generate the number of words requested. */ @Override public String generateText(int numWords) { // TODO: Implement this method if (wordList.size() == 0) { return ""; } String text = ""; String current = starter; for (int i = 0; i < numWords; i++) { ListNode curnode = findNode(current); if (curnode == null) { curnode = findNode(starter); } String nexttext = curnode.getRandomNextWord(rnGenerator); text = text + nexttext + " "; current = nexttext; } return text; }
/* The goal of Markov Text Generation is to be able to generate text which resembles the source in a reasonable way. The method generateText returns such text as a String of words, the length of which is determined by numWords. */ @Override public String generateText(int numWords) { // TODO: Implement this method if (numWords < 0) { throw new IllegalArgumentException("Illegal Argument"); } if (numWords == 0) { return ""; } StringBuilder sb = new StringBuilder(); String crtWord = starter; sb.append(crtWord + " "); numWords--; // 先减去starter word占据的一个 while (numWords-- > 0) { ListNode node = isExist(crtWord, wordList); String next = node.getRandomNextWord(rnGenerator); sb.append(next + " "); crtWord = next; } return sb.toString(); }