From 376fba6795aafb479b44abbc4608a8ec487bb6e5 Mon Sep 17 00:00:00 2001 From: ElnuDev Date: Thu, 13 Apr 2023 19:31:20 -0700 Subject: [PATCH] Make word lookup more verbose --- src/main.rs | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/src/main.rs b/src/main.rs index d31236c..f1a830f 100644 --- a/src/main.rs +++ b/src/main.rs @@ -64,6 +64,25 @@ fn broadcast_player_count(clients: &mut HashMap) { } } +fn lookup(input: &str) -> Option<&jisho::Entry> { + let input = input.trim(); + for word in jisho::lookup(input) { + if + // If input has no kanji, + // we can just compare the input to the reading verbatim + // ensuring both are hiragana + (input.is_kana() && input.to_hiragana() == word.reading.to_hiragana()) || + // Otherwise, we have to ensure that the input + // is verbosely the same. + // However, this will cause problems for some words. + // For example, 照り焼き will be accepted but 照焼 won't. + (input == word.kanji) { + return Some(word); + } + } + return None; +} + fn main() { let event_hub = simple_websockets::launch(8080) .expect("failed to listen on port 8080"); @@ -102,7 +121,7 @@ fn main() { message: String::from("It's not your turn!"), } } else { - match jisho::lookup(&message.trim()).iter().next() { + match lookup(&message) { Some(entry) => { if entry.reading.chars().last().unwrap().to_string().to_hiragana() == "ん" { MessageResponseData::Error {