@Override public Writable getHeuristicNext(Map<String, Writable> writables) { if (getNextWritables().isEmpty()) { return null; } HashSet<Writable> temp = new HashSet<Writable>(); for (String s : getNextWritables()) temp.add(writables.get(s)); TreeSet<Writable> ordered = new TreeSet<Writable>( new Comparator<Writable>() { public int compare(Writable one, Writable two) { return Integer.compare(one.getUseCount(), two.getUseCount()); } }); ordered.addAll(temp); ArrayList<Writable> heuristicPick = new ArrayList<Writable>(); int count = 0; Word last = null; for (Writable w : ordered) { if (last == null || !(last.getUseCount() == w.getUseCount())) count++; for (int k = 0; k < count; k++) { heuristicPick.add(w); } } return heuristicPick.get((int) (Math.random() * heuristicPick.size())); }
/* * Shrinks end punctuation, and checks for interior punctuation If the word * passes the above it is created and its end punctuation is processed and * added to its previous word if there is one, and checks the previous word * to see if it can be capitalizable * * Returns null if the word is not valid */ public static Word createWord(String word, boolean capitalizable) { // check if word is valid word = shrinkEndPunct(word); if (word == null) return null; if (!checkInteriorPunct(word)) return null; // create word, process end punctuation Word result; char last = word.charAt(word.length() - 1); if (!Character.isAlphabetic(last) && !Character.isDigit(last) && last != '\'') { result = new Word(word.substring(0, word.length() - 1)); result.addEndPunctuation(last); } else result = new Word(word); result.addEndPunctuation(' '); // check for capitalizable result.capitalizable = capitalizable; return result; }