add katakana support

This commit is contained in:
Hamcha 2024-10-31 23:34:05 +01:00
parent 8e830d03cb
commit 2bd66c5fa0
Signed by: hamcha
GPG key ID: 1669C533B8CF6D89
4 changed files with 136 additions and 4 deletions

2
Cargo.lock generated
View file

@ -235,7 +235,7 @@ dependencies = [
[[package]] [[package]]
name = "weeblib" name = "weeblib"
version = "0.1.2" version = "0.2.0"
dependencies = [ dependencies = [
"console_error_panic_hook", "console_error_panic_hook",
"wasm-bindgen", "wasm-bindgen",

View file

@ -1,6 +1,6 @@
[package] [package]
name = "weeblib" name = "weeblib"
version = "0.1.2" version = "0.2.0"
edition = "2021" edition = "2021"
authors = ["Hamcha <hamcha@crunchy.rocks>"] authors = ["Hamcha <hamcha@crunchy.rocks>"]

View file

@ -1,5 +1,5 @@
use crate::romanization::romanize; use crate::romanization::romanize;
use crate::syllable::to_hiragana; use crate::syllable::{to_hiragana, to_katakana};
use wasm_bindgen::prelude::wasm_bindgen; 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() 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)] #[cfg(test)]
mod tests { mod tests {
use super::*; use super::*;
@ -26,4 +32,14 @@ mod tests {
fn test_convert_to_hiragana_symbols() { fn test_convert_to_hiragana_symbols() {
assert_eq!(convert_to_hiragana("asso-lutamente no!1"), "あっそ-るためんて の!1"); 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");
}
} }

View file

@ -236,8 +236,124 @@ pub(crate) fn to_hiragana(syllable: &Syllable) -> String {
} }
} }
impl Display for Syllable {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { pub(crate) fn to_katakana(syllable: &Syllable) -> String {
write!(f, "{}", to_hiragana(self)) 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_katakana(self))
} }
} }