!7 static int maxWords = 100000; static int maxWordLength = 10; static int topTenTargetLength = 10; static Map map; static L topTen; p-subst { tt(); centerHigherConsole(); map = l_persistentTreeMap("Words + count"); topTen = l_persistentList("Top Ten"); printTopTen(); botIfMain(); } svoid addWordHits(Collection words) { for (S word : words) { word = shorten(word, maxWordLength); long count = longMultiSet_add(map, word); insertToTopTen(word, count); } } svoid insertToTopTen(S word, long count) { int originalIndex = indexOf(topTen, word); int i = originalIndex < 0 ? l(topTen) : originalIndex; while (i > 0 && count >= toLong(map.get(topTen.get(i-1)))) --i; if (i >= topTenTargetLength || i == originalIndex) ret; if (originalIndex >= 0) topTen.remove(originalIndex); topTen.add(i, word); truncateList(topTen, topTenTargetLength); } svoid printTopTen { printAsciiHeading("TOP TEN"); pnl(map(topTen, func(S s) { s + " - " + map.get(s) })); } answer { if "clear top ten" { topTen.clear(); ret "OK"; } addWordHits(words2(s)); printTopTen(); try answer "OK"; }