Skip to content

Instantly share code, notes, and snippets.

@Alfex4936
Created May 12, 2023 08:28
Show Gist options
  • Save Alfex4936/b9cc073ea986a4242d5b5fe5b9d14afe to your computer and use it in GitHub Desktop.
Save Alfex4936/b9cc073ea986a4242d5b5fe5b9d14afe to your computer and use it in GitHub Desktop.
Korean jeongseong replace (한글 종성 변경) in Rust
use std::collections::HashMap;
fn get_jongseong_index(jongseong: char) -> Option<u16> {
let jongseong_map: HashMap<char, u16> = [
(' ', 0), ('ㄱ', 1), ('ㄲ', 2), ('ㄳ', 3),
('ㄴ', 4), ('ㄵ', 5), ('ㄶ', 6), ('ㄷ', 7),
('ㄹ', 8), ('ㄺ', 9), ('ㄻ', 10), ('ㄼ', 11),
('ㄽ', 12), ('ㄾ', 13), ('ㄿ', 14), ('ㅀ', 15),
('ㅁ', 16), ('ㅂ', 17), ('ㅄ', 18), ('ㅅ', 19),
('ㅆ', 20), ('ㅇ', 21), ('ㅈ', 22), ('ㅊ', 23),
('ㅋ', 24), ('ㅌ', 25), ('ㅍ', 26), ('ㅎ', 27),
].iter().cloned().collect();
jongseong_map.get(&jongseong).cloned()
}
fn replace_jongseong(s: &str, original: char, replacement: char) -> String {
let original_index = get_jongseong_index(original).unwrap();
let replacement_index = get_jongseong_index(replacement).unwrap();
s.chars()
.map(|c| {
let code = c as u32;
// Check if it's a Hangul syllable
if 0xAC00 <= code && code <= 0xD7A3 {
// Decompose the syllable
let syllable_index = code - 0xAC00;
let jongseong_index = syllable_index as u16 % 28;
if jongseong_index == original_index {
// If the jongseong matches the original, replace it
let new_code = code - (original_index as u32) + (replacement_index as u32);
std::char::from_u32(new_code).unwrap()
} else {
// If the jongseong doesn't match, leave it alone
c
}
} else {
// If it's not a Hangul syllable, leave it alone
c
}
})
.collect()
}
fn main() {
let hey = "깔깘깎꾼";
let result = replace_jongseong(hey, 'ㄽ', 'ㅋ');
let result = replace_jongseong(&result, 'ㄲ', 'ㄴ');
println!("{result}"); // 깔깤깐꾼
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment