/** Train the generator by adding the sourceText */ @Override public void train(String sourceText) { // TODO: Implement this method String splitOn = "[\\s.]+"; String[] words = sourceText.split(splitOn); String prev = starter; for (String word : words) { ListNode tomodify = null; ListIterator<ListNode> iter = wordList.listIterator(); while (iter.hasNext()) { ListNode node = iter.next(); if (node.getWord().equals(prev)) { tomodify = node; break; } } if (tomodify == null) { tomodify = new ListNode(prev); wordList.add(tomodify); } // add w to the targeted ListNode tomodify.addNextWord(word); prev = word; } }
private ListNode findNode(String key) { for (ListNode n : wordList) { if (n.getWord().equals(key)) { return n; } } return null; }
// helper function private ListNode isExist(String crtWord, List<ListNode> wordList) { int size = wordList.size(); for (int i = 0; i < size; i++) { ListNode node = wordList.get(i); if (node.getWord().equals(crtWord)) { return node; } } return null; }