From 2bd66c5fa0bae8d04ab7472561fb22970f220614 Mon Sep 17 00:00:00 2001 From: Hamcha Date: Thu, 31 Oct 2024 23:34:05 +0100 Subject: [PATCH] add katakana support --- Cargo.lock | 2 +- Cargo.toml | 2 +- src/lib.rs | 18 +++++++- src/syllable.rs | 118 +++++++++++++++++++++++++++++++++++++++++++++++- 4 files changed, 136 insertions(+), 4 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index f93fda1..3aac30d 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -235,7 +235,7 @@ dependencies = [ [[package]] name = "weeblib" -version = "0.1.2" +version = "0.2.0" dependencies = [ "console_error_panic_hook", "wasm-bindgen", diff --git a/Cargo.toml b/Cargo.toml index 3ef50ad..ac9122d 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "weeblib" -version = "0.1.2" +version = "0.2.0" edition = "2021" authors = ["Hamcha "] diff --git a/src/lib.rs b/src/lib.rs index 76b939f..7b31ce2 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,5 +1,5 @@ use crate::romanization::romanize; -use crate::syllable::to_hiragana; +use crate::syllable::{to_hiragana, to_katakana}; use wasm_bindgen::prelude::wasm_bindgen; @@ -13,6 +13,12 @@ pub fn convert_to_hiragana(text: &str) -> String { romanize(text).iter().map(|r| to_hiragana(r)).collect() } +/// Converts italian text to katakana trying to keep the phonemes the same +#[wasm_bindgen] +pub fn convert_to_katakana(text: &str) -> String { + romanize(text).iter().map(|r| to_katakana(r)).collect() +} + #[cfg(test)] mod tests { use super::*; @@ -26,4 +32,14 @@ mod tests { fn test_convert_to_hiragana_symbols() { assert_eq!(convert_to_hiragana("asso-lutamente no!1"), "あっそ-るためんて の!1"); } + + #[test] + fn test_convert_to_katakana_words() { + assert_eq!(convert_to_katakana("assolutamente"), "アッソルタメンテ"); + } + + #[test] + fn test_convert_to_katakana_symbols() { + assert_eq!(convert_to_katakana("asso-lutamente no!1"), "アッソ-ルタメンテ ノ!1"); + } } \ No newline at end of file diff --git a/src/syllable.rs b/src/syllable.rs index d86f883..84287b9 100644 --- a/src/syllable.rs +++ b/src/syllable.rs @@ -236,8 +236,124 @@ pub(crate) fn to_hiragana(syllable: &Syllable) -> String { } } + +pub(crate) fn to_katakana(syllable: &Syllable) -> String { + match syllable { + A => "ア".to_string(), + I => "イ".to_string(), + U => "ウ".to_string(), + E => "エ".to_string(), + O => "オ".to_string(), + Ka => "カ".to_string(), + Ki => "キ".to_string(), + Ku => "ク".to_string(), + Ke => "ケ".to_string(), + Ko => "コ".to_string(), + Sa => "サ".to_string(), + Shi => "シ".to_string(), + Su => "ス".to_string(), + Se => "セ".to_string(), + So => "ソ".to_string(), + Ta => "タ".to_string(), + Chi => "チ".to_string(), + Tsu => "ツ".to_string(), + Te => "テ".to_string(), + To => "ト".to_string(), + Na => "ナ".to_string(), + Ni => "ニ".to_string(), + Nu => "ヌ".to_string(), + Ne => "ネ".to_string(), + No => "ノ".to_string(), + Ha => "ハ".to_string(), + Hi => "ヒ".to_string(), + Fu => "フ".to_string(), + He => "ヘ".to_string(), + Ho => "ホ".to_string(), + Ma => "マ".to_string(), + Mi => "ミ".to_string(), + Mu => "ム".to_string(), + Me => "メ".to_string(), + Mo => "モ".to_string(), + Ya => "ヤ".to_string(), + Yu => "ユ".to_string(), + Yo => "ヨ".to_string(), + Ra => "ラ".to_string(), + Ri => "リ".to_string(), + Ru => "ル".to_string(), + Re => "レ".to_string(), + Ro => "ロ".to_string(), + Wa => "ワ".to_string(), + N => "ン".to_string(), + Ga => "ガ".to_string(), + Gi => "ギ".to_string(), + Gu => "グ".to_string(), + Ge => "ゲ".to_string(), + Go => "ゴ".to_string(), + Za => "ザ".to_string(), + Ji => "ジ".to_string(), + Zu => "ズ".to_string(), + Ze => "ゼ".to_string(), + Zo => "ゾ".to_string(), + Da => "ダ".to_string(), + De => "デ".to_string(), + Do => "ド".to_string(), + Ba => "バ".to_string(), + Bi => "ビ".to_string(), + Bu => "ブ".to_string(), + Be => "ベ".to_string(), + Bo => "ボ".to_string(), + Pa => "パ".to_string(), + Pi => "ピ".to_string(), + Pu => "プ".to_string(), + Pe => "ペ".to_string(), + Po => "ポ".to_string(), + Kya => "キャ".to_string(), + Kyu => "キュ".to_string(), + Kyo => "キョ".to_string(), + Sha => "シャ".to_string(), + Shu => "シュ".to_string(), + Sho => "ショ".to_string(), + Cha => "チャ".to_string(), + Chu => "チュ".to_string(), + Cho => "チョ".to_string(), + Rya => "リャ".to_string(), + Ryu => "リュ".to_string(), + Ryo => "リョ".to_string(), + LittleTsu => "ッ".to_string(), + LongVowel => "ー".to_string(), + ChiE => "チェ".to_string(), + ToU => "トゥ".to_string(), + TeI => "トィ".to_string(), + VuA => "ヴァ".to_string(), + VuI => "ヴィ".to_string(), + Vu => "ヴ".to_string(), + VuE => "ヴェ".to_string(), + VuO => "ヴォ".to_string(), + Ja => "ジャ".to_string(), + Ju => "ジュ".to_string(), + Jo => "ジョ".to_string(), + Je => "ジェ".to_string(), + Zi => "ヂィ".to_string(), + Gya => "ギャ".to_string(), + Gyu => "ギュ".to_string(), + Gyo => "ギョ".to_string(), + Bya => "ビャ".to_string(), + Byu => "ビュ".to_string(), + Byo => "ビョ".to_string(), + Pya => "ピャ".to_string(), + Pyu => "ピュ".to_string(), + Pyo => "ピョ".to_string(), + Zyo => "ずょ".to_string(), + Zya => "ズャ".to_string(), + Zyu => "ズュ".to_string(), + Zye => "ズョ".to_string(), + NonAlpha(char) => char.to_string() + } +} + + impl Display for Syllable { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - write!(f, "{}", to_hiragana(self)) + write!(f, "{}", to_katakana(self)) } } \ No newline at end of file