WordHuntGame doLearnWords(final MarkovPredictor mk, final String latestNews) { String textToLearn = latestNews.toUpperCase().replaceAll("</?[^>]+>", " ").replaceAll("[^A-Z]", " "); for (String word : textToLearn.split(" ")) { if (word.length() < 3) continue; mk.learn(word); } return this; }
BoardToken getGenerateToken( final int chainSize, final MarkovPredictor mk, final TokenArray a, final ArrayPosition p) { // Generate raw ideas final HashMap<String, Integer> ideas = new HashMap<String, Integer>(); final HashMap<String, Double> nearbyStuff = new HashMap<String, Double>(); for (final ArrayList<ArrayPosition> na : p.getNeighborChain(chainSize)) { final StringBuilder state = new StringBuilder(); for (final ArrayPosition n : na) { String c = a.get(n).state; state.append(c); Double v = 0.0; if (nearbyStuff.containsKey(c)) { v = nearbyStuff.get(c); } nearbyStuff.put(c, ++v); } for (int n = 0; n < 10; n++) { final String rand = mk.predict(state.toString()); int v = 0; if (ideas.containsKey(rand)) { v = ideas.get(rand); } ideas.put(rand, ++v); } } // Sort and collate final ArrayList<String> arrayList = new ArrayList<String>(ideas.keySet()); for (final String rand : arrayList) { int v = 0; if (ideas.containsKey(rand)) { v = ideas.get(rand); } if (nearbyStuff.containsKey(rand)) { v /= nearbyStuff.get(rand); } ideas.put(rand, (int) (100 * v * Math.random())); } Collections.sort( arrayList, new Comparator<String>() { public int compare(final String o1, final String o2) { return -ideas.get(o1).compareTo(ideas.get(o2)); } }); // Finish up String winningString; if (0 == arrayList.size()) { winningString = mk.predict(""); } else { winningString = arrayList.get(0); } a.put(p, a.new ElementState(winningString)); final IndexPosition position = new IndexPosition(p.row, p.col); final BoardToken token = new BoardToken(++cardIdCounter, "WORD1", winningString, null, null, false, position); token.setText(winningString); token.toggleImageId = winningString + "2"; token.toggleCommand = String.format("Select (%d, %d)", p.row, p.col); return token; }