Keeps stats on what completions tend to get completed
| 12 | * Keeps stats on what completions tend to get completed |
| 13 | */ |
| 14 | public class CompletionStats { |
| 15 | |
| 16 | public static CompletionStats stats = new CompletionStats(); |
| 17 | |
| 18 | protected CompletionStats() { |
| 19 | } |
| 20 | |
| 21 | LinkedHashMap<String, Double> counts = AutoPersist.persist("completion_statistics", () -> new LinkedHashMap<>(), x -> { |
| 22 | x.entrySet().forEach(y -> { |
| 23 | y.setValue(y.getValue() * 0.5f); |
| 24 | }); |
| 25 | return x; |
| 26 | }); |
| 27 | |
| 28 | LinkedHashMap<String, String> uuidToCompletions = new LinkedHashMap<String, String>() { |
| 29 | |
| 30 | @Override |
| 31 | protected boolean removeEldestEntry(Map.Entry<String, String> entry) { |
| 32 | return this.size() > 500; |
| 33 | } |
| 34 | }; |
| 35 | |
| 36 | public void autosuggestCommands(List<Triple<String, String, Runnable>> m) { |
| 37 | m.forEach(x -> { |
| 38 | uuidToCompletions.put(x.first, x.first); |
| 39 | }); |
| 40 | |
| 41 | m.sort((a, b) -> { |
| 42 | double cA = counts.getOrDefault(a.first, 0d); |
| 43 | double cB = counts.getOrDefault(b.first, 0d); |
| 44 | if (cA == cB) { |
| 45 | return String.CASE_INSENSITIVE_ORDER.compare(a.first, b.first); |
| 46 | } else { |
| 47 | return -Double.compare(cA, cB); |
| 48 | } |
| 49 | }); |
| 50 | |
| 51 | double[] n = {0}; |
| 52 | int[] num = {0}; |
| 53 | m.forEach(x -> { |
| 54 | Double cc = counts.get(x.first); |
| 55 | if (cc != null) { |
| 56 | num[0]++; |
| 57 | n[0] += cc; |
| 58 | } |
| 59 | }); |
| 60 | |
| 61 | if (num[0] == 0) { |
| 62 | |
| 63 | } else { |
| 64 | List<Triple<String, String, Runnable>> out = m.stream().map(x -> { |
| 65 | Double cc = counts.get(x.first); |
| 66 | if (cc != null) { |
| 67 | double score = Math.max(1, Math.min(3, 3 * cc / (n[0] / num[0]))); |
| 68 | String second = x.second + "</span><span class=\"stars\">" + num(score, "★") + " "; |
| 69 | |
| 70 | Triple<String, String, Runnable> t = new Triple<>(x.first, second, x.third); |
| 71 |