diff options
author | Marko Zajc <marko@zajc.eu.org> | 2023-07-30 16:41:44 +0200 |
---|---|---|
committer | Marko Zajc <marko@zajc.eu.org> | 2023-08-02 00:59:00 +0200 |
commit | 8c67d5cedb932a11ca95f45394a5a0d55164ca3c (patch) | |
tree | 654b63511a28bdf388b661727d80352b3bf0496f | |
parent | fae363875600a96dda2797ba25d29a10be3d1d5b (diff) |
[module-search] Use the API-provided sorting in UrbanDictionary
-rw-r--r-- | module-search/pom.xml | 7 | ||||
-rw-r--r-- | module-search/src/main/java/libot/commands/UrbanDictionaryCommand.java | 46 |
2 files changed, 8 insertions, 45 deletions
diff --git a/module-search/pom.xml b/module-search/pom.xml index 4f44de5..c315892 100644 --- a/module-search/pom.xml +++ b/module-search/pom.xml | |||
@@ -42,13 +42,6 @@ | |||
42 | <groupId>com.konghq</groupId> | 42 | <groupId>com.konghq</groupId> |
43 | <artifactId>unirest-java</artifactId> | 43 | <artifactId>unirest-java</artifactId> |
44 | </dependency> | 44 | </dependency> |
45 | |||
46 | <!-- Wilson score calculation --> | ||
47 | <dependency> | ||
48 | <groupId>org.apache.commons</groupId> | ||
49 | <artifactId>commons-math3</artifactId> | ||
50 | <version>3.6.1</version> | ||
51 | </dependency> | ||
52 | </dependencies> | 45 | </dependencies> |
53 | 46 | ||
54 | <build> | 47 | <build> |
diff --git a/module-search/src/main/java/libot/commands/UrbanDictionaryCommand.java b/module-search/src/main/java/libot/commands/UrbanDictionaryCommand.java index 4e9f12b..21d3d63 100644 --- a/module-search/src/main/java/libot/commands/UrbanDictionaryCommand.java +++ b/module-search/src/main/java/libot/commands/UrbanDictionaryCommand.java | |||
@@ -3,7 +3,6 @@ package libot.commands; | |||
3 | import static com.google.common.cache.CacheBuilder.newBuilder; | 3 | import static com.google.common.cache.CacheBuilder.newBuilder; |
4 | import static java.lang.String.format; | 4 | import static java.lang.String.format; |
5 | import static java.nio.charset.StandardCharsets.UTF_8; | 5 | import static java.nio.charset.StandardCharsets.UTF_8; |
6 | import static java.util.Arrays.sort; | ||
7 | import static java.util.concurrent.TimeUnit.DAYS; | 6 | import static java.util.concurrent.TimeUnit.DAYS; |
8 | import static java.util.regex.Pattern.compile; | 7 | import static java.util.regex.Pattern.compile; |
9 | import static kong.unirest.Unirest.spawnInstance; | 8 | import static kong.unirest.Unirest.spawnInstance; |
@@ -21,8 +20,6 @@ import java.util.regex.Pattern; | |||
21 | 20 | ||
22 | import javax.annotation.*; | 21 | import javax.annotation.*; |
23 | 22 | ||
24 | import org.apache.commons.math3.stat.interval.WilsonScoreInterval; | ||
25 | |||
26 | import com.google.common.cache.Cache; | 23 | import com.google.common.cache.Cache; |
27 | 24 | ||
28 | import kong.unirest.*; | 25 | import kong.unirest.*; |
@@ -32,12 +29,10 @@ import libot.core.extensions.EmbedPrebuilder; | |||
32 | 29 | ||
33 | public class UrbanDictionaryCommand extends Command { | 30 | public class UrbanDictionaryCommand extends Command { |
34 | 31 | ||
35 | private static final WilsonScoreInterval WILSON = new WilsonScoreInterval(); | ||
36 | |||
37 | static record Definition(String definition, int thumbs_up, String author, String word, String example, | 32 | static record Definition(String definition, int thumbs_up, String author, String word, String example, |
38 | int thumbs_down, boolean empty) { | 33 | int thumbs_down) { |
39 | 34 | ||
40 | public static final Definition BLANK = new Definition(null, 0, null, null, null, 0, true); | 35 | public static final Definition BLANK = new Definition(null, 0, null, null, null, 0); |
41 | 36 | ||
42 | @SuppressWarnings("null") | 37 | @SuppressWarnings("null") |
43 | public String definition() { | 38 | public String definition() { |
@@ -82,8 +77,10 @@ public class UrbanDictionaryCommand extends Command { | |||
82 | try { | 77 | try { |
83 | var term = TERMS_CACHE.get(c.params().get(0).toLowerCase(), () -> getTerm(c.params())); | 78 | var term = TERMS_CACHE.get(c.params().get(0).toLowerCase(), () -> getTerm(c.params())); |
84 | var def = DEFINITION_CACHE.get(term, () -> getDefinition(term)); | 79 | var def = DEFINITION_CACHE.get(term, () -> getDefinition(term)); |
85 | if (def.empty()) | 80 | |
81 | if (def.definition() == null) | ||
86 | throw c.errorf("Looks like UrbanDictionary can't define '%s'.", DISABLED, escape(c.params().get(0))); | 82 | throw c.errorf("Looks like UrbanDictionary can't define '%s'.", DISABLED, escape(c.params().get(0))); |
83 | |||
87 | var b = new EmbedPrebuilder(LITHIUM).setAuthorf(FORMAT_AUTHOR, def.author()) | 84 | var b = new EmbedPrebuilder(LITHIUM).setAuthorf(FORMAT_AUTHOR, def.author()) |
88 | .setDescriptionf(FORMAT_DESCRIPTION, def.definition()) | 85 | .setDescriptionf(FORMAT_DESCRIPTION, def.definition()) |
89 | .setTitle(format(FORMAT_TITLE, def.word()), | 86 | .setTitle(format(FORMAT_TITLE, def.word()), |
@@ -127,37 +124,10 @@ public class UrbanDictionaryCommand extends Command { | |||
127 | if (response.isEmpty()) | 124 | if (response.isEmpty()) |
128 | return BLANK; | 125 | return BLANK; |
129 | 126 | ||
130 | var definitions = new Definition[response.length()]; | 127 | var json = response.getJSONObject(0); |
131 | for (int i = 0; i < response.length(); i++) { | ||
132 | var json = response.getJSONObject(i); | ||
133 | definitions[i] = | ||
134 | new Definition(json.getString("definition"), json.getInt("thumbs_up"), json.getString("author"), | ||
135 | json.getString("word"), json.getString("example"), json.getInt("thumbs_down"), false); | ||
136 | } | ||
137 | |||
138 | String finalTerm = term; // lambda shenanigans | ||
139 | sort(definitions, (d1, d2) -> { | ||
140 | boolean d1Match = finalTerm.equals(d1.word()); | ||
141 | boolean d2Match = finalTerm.equals(d2.word()); | ||
142 | if (!d1Match && d2Match) | ||
143 | return 1; | ||
144 | else if (d1Match && !d2Match || d2.thumbs_down() == 0 && d1.thumbs_down() != 0) | ||
145 | return -1; | ||
146 | else if (d2.thumbs_down() != 0 && d1.thumbs_down() == 0) | ||
147 | return 1; | ||
148 | |||
149 | return Double.compare(calculateScore(d2), calculateScore(d1)); | ||
150 | }); | ||
151 | return definitions[0]; | ||
152 | } | ||
153 | |||
154 | private static double calculateScore(Definition def) { | ||
155 | int totalScore = def.thumbs_up() + def.thumbs_down(); | ||
156 | if (totalScore == 0) | ||
157 | return 0; | ||
158 | 128 | ||
159 | var wilson1 = WILSON.createInterval(totalScore, def.thumbs_up(), .95F); | 129 | return new Definition(json.getString("definition"), json.getInt("thumbs_up"), json.getString("author"), |
160 | return (wilson1.getUpperBound() + wilson1.getLowerBound()) / 2; | 130 | json.getString("word"), json.getString("example"), json.getInt("thumbs_down")); |
161 | } | 131 | } |
162 | 132 | ||
163 | @Override | 133 | @Override |