From daa97e34c23c257c6ca87a8f594623fe164f2fec Mon Sep 17 00:00:00 2001 From: Guimc Date: Fri, 9 Aug 2024 23:31:18 +0800 Subject: [PATCH] optimize code --- build.gradle.kts | 1 - .../ltd/guimc/lgzbot/command/LGZBotCommand.kt | 12 ------- .../lgzbot/listener/message/MessageFilter.kt | 8 ----- .../kotlin/ltd/guimc/lgzbot/word/WordUtils.kt | 33 ------------------- src/main/resources/serious.txt | 2 -- src/test/java/Test.java | 21 +----------- 6 files changed, 1 insertion(+), 76 deletions(-) delete mode 100644 src/main/kotlin/ltd/guimc/lgzbot/word/WordUtils.kt diff --git a/build.gradle.kts b/build.gradle.kts index 629e3de..dbb7c17 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -17,7 +17,6 @@ dependencies { implementation("io.ktor:ktor-server-netty:$ktor_version") implementation("ch.qos.logback:logback-classic:1.4.12") implementation("com.github.promeg:tinypinyin:2.0.3") - implementation("com.huaban:jieba-analysis:+") implementation("com.github.promeg:tinypinyin-lexicons-java-cncity:2.0.3") implementation("org.json:json:20231013") implementation("org.apache.httpcomponents:httpclient:4.5.14") diff --git a/src/main/kotlin/ltd/guimc/lgzbot/command/LGZBotCommand.kt b/src/main/kotlin/ltd/guimc/lgzbot/command/LGZBotCommand.kt index 186ce9a..e94fcd8 100644 --- a/src/main/kotlin/ltd/guimc/lgzbot/command/LGZBotCommand.kt +++ b/src/main/kotlin/ltd/guimc/lgzbot/command/LGZBotCommand.kt @@ -10,13 +10,11 @@ package ltd.guimc.lgzbot.command import ltd.guimc.lgzbot.PluginMain -import ltd.guimc.lgzbot.counter.VLManager import ltd.guimc.lgzbot.files.ModuleStateConfig import ltd.guimc.lgzbot.listener.message.MessageFilter import ltd.guimc.lgzbot.utils.LL4JUtils import ltd.guimc.lgzbot.utils.MessageUtils.getPlainText import ltd.guimc.lgzbot.utils.OverflowUtils -import ltd.guimc.lgzbot.word.WordUtils import net.mamoe.mirai.Bot import net.mamoe.mirai.console.command.* import net.mamoe.mirai.contact.Member @@ -37,16 +35,6 @@ object LGZBotCommand : CompositeCommand( sendMessage("Pong!") } - @SubCommand("word") - @Description("词频统计") - suspend fun CommandSender.word(user: Member,time: String) { - try { - sendMessage("他的词频信息:\n${WordUtils.hashMapToString(WordUtils.sortAndTrim(VLManager.getCounter(user).wordFrequency,Integer.parseInt(time)))}") - } catch (e: Exception) { - sendMessage("Oops! Something went wrong! ${e.message}") - } - } - @SubCommand("mute") @Description("把某人的嘴巴用胶布粘上") suspend fun CommandSender.mute(user: Member, time: String, reason: String) { diff --git a/src/main/kotlin/ltd/guimc/lgzbot/listener/message/MessageFilter.kt b/src/main/kotlin/ltd/guimc/lgzbot/listener/message/MessageFilter.kt index a43c2b3..0015fc8 100644 --- a/src/main/kotlin/ltd/guimc/lgzbot/listener/message/MessageFilter.kt +++ b/src/main/kotlin/ltd/guimc/lgzbot/listener/message/MessageFilter.kt @@ -26,7 +26,6 @@ import ltd.guimc.lgzbot.utils.MessageUtils.getPlainText import ltd.guimc.lgzbot.utils.RegexUtils import ltd.guimc.lgzbot.utils.TextUtils.findSimilarity import ltd.guimc.lgzbot.utils.TextUtils.removeNonVisible -import ltd.guimc.lgzbot.word.WordUtils import net.mamoe.mirai.console.permission.Permission import net.mamoe.mirai.console.permission.PermissionService.Companion.hasPermission import net.mamoe.mirai.console.permission.PermitteeId.Companion.permitteeId @@ -308,13 +307,6 @@ object MessageFilter { } val counter = VLManager.getCounter(e.sender) - WordUtils.filter(e.message.content).forEach { - if (counter.wordFrequency[it] != null) { - counter.wordFrequency[it] = counter.wordFrequency[it]!! + 1 - } else { - counter.wordFrequency[it] = 0 - } - } if ((e.sender.permitteeId.hasPermission(PluginMain.blocked) && !e.sender.permitteeId.hasPermission(bypassMute)) || (e.group.permitteeId.hasPermission( PluginMain.blocked diff --git a/src/main/kotlin/ltd/guimc/lgzbot/word/WordUtils.kt b/src/main/kotlin/ltd/guimc/lgzbot/word/WordUtils.kt deleted file mode 100644 index c3eb626..0000000 --- a/src/main/kotlin/ltd/guimc/lgzbot/word/WordUtils.kt +++ /dev/null @@ -1,33 +0,0 @@ -package ltd.guimc.lgzbot.word - -import com.huaban.analysis.jieba.JiebaSegmenter - -object WordUtils { - fun containsChinese(text: String): Boolean { - val regex = Regex("[\\u4E00-\\u9FFF]+") - return regex.containsMatchIn(text) - } - fun hashMapToString(wordFrequency: HashMap): String { - val sb = StringBuilder() - for ((key, value) in wordFrequency) { - sb.append("$key: $value\n") - } - return sb.toString() - } - fun filter(message:String):List{ - return JiebaSegmenter().sentenceProcess(message).filter { it.length>=2 && containsChinese(it) } - } - fun sortAndTrim(wordFrequency: HashMap,n: Int): HashMap { - val sortedList = wordFrequency.toList().sortedByDescending { it.second } - val result = hashMapOf() - var count = 0 - for ((key, value) in sortedList) { - if (count >= n) { - break - } - result[key] = value - count++ - } - return result - } -} \ No newline at end of file diff --git a/src/main/resources/serious.txt b/src/main/resources/serious.txt index c95f35a..7a72453 100644 --- a/src/main/resources/serious.txt +++ b/src/main/resources/serious.txt @@ -322,8 +322,6 @@ engadget中文网 民国 民进党 民运 -民主 -民主潮 摩门教 纳粹 南华早报 diff --git a/src/test/java/Test.java b/src/test/java/Test.java index 7d732b1..0101e4e 100644 --- a/src/test/java/Test.java +++ b/src/test/java/Test.java @@ -1,24 +1,5 @@ -import ltd.guimc.lgzbot.utils.LL4JUtils; - -import java.nio.charset.Charset; -import java.util.Arrays; public class Test { - public static void main(String[] args) { - LL4JUtils.INSTANCE.init(); - System.out.println(Charset.defaultCharset()); - System.out.println(Arrays.toString(LL4JUtils.INSTANCE.predictAllResult(""))); - System.out.println(LL4JUtils.INSTANCE.getModel().predict(LL4JUtils.INSTANCE.getTokenizer().tokenize(0, " Oyster 新内步" + - "学生党の首选" + - "[+]吻订沙路,让您所向披靡" + - "[+]反击退,让您不再惧怕掉进虚空" + - "[+]叨抱,让沙路的抱力更上一层楼" + - "[+]搭路,吻订不吞方块" + - "[+]瞬移,远程偷家让别人 die 都不知道怎么 die 的" + - "[+]视觉美丽的同时让您 fps++" + - "[+]完美结局总动 GG,装 b 更加彻底" + - "[+]万能的管理不会不耐烦" + - "[+]我们承诺永不跑路" + - "内步 15r buy➕1632596365").values)); + public static void main(String[] args) throws InterruptedException { } }