Japanese kana
Japanese kana: romaji typed as hiragana or katakana, kana written in Hepburn romaji, hiragana and katakana each to the other, half-width katakana from bank and till systems made full-width, and which scripts a text uses.
Version 1.0.0 · 6 functions · 3 crates
Functions
What use kana.qwer gives a program, with the types Quillo sees. To take only some, or under other names: use kana_to_romaji, kana_from_romaji from kana (Quillo 0.6).
kana_to_romaji(text: str) → strKana as Hepburn romaji ("とうきょう" is "toukyou", "ラーメン" "raamen"), kanji and other text left as they are: for URLs, search and sorting names.
kana_from_romaji(text: str) → strRomaji as an input method types it: lower case to hiragana, upper case to katakana ("konnichiha" is "こんにちは", "KOOHII" "コオヒイ"), other kana kept.
kana_to_hiragana(text: str, keep_long_mark: bool) → strEverything in hiragana: katakana and romaji converted ("カタカナ" is "かたかな"); a long-vowel mark ー becomes its vowel unless keep_long_mark.
kana_to_katakana(text: str) → strEverything in katakana: hiragana and romaji converted ("ひらがな" is "ヒラガナ"), for names in forms and furigana fields.
kana_fullwidth(text: str) → strHalf-width katakana ("ガイド", as old bank, POS and payment systems send it) as full-width ("ガイド"), voiced marks joined; the rest left as it is.
kana_info(text: str) → anyWhich scripts a text uses: { japanese, hiragana, katakana, kanji, romaji, mixed, counts: { hiragana, katakana, kanji, latin, other } }; japanese when all of it is (punctuation included), mixed when it has both kana and romaji.
Get it
$ quillo install kana # kana.qwer into this folder$ quillo install kana --example # and its example, kana.qweOr with curl:
$ curl -O https://quillolang.org/plugins/files/kana.qwer$ curl -O https://quillolang.org/plugins/files/kana.qweThen, in your program, all of it or only what you call:
use kana.qweruse kana_to_romaji, kana_from_romaji from kana // Quillo 0.6: only theseIt uses these crates, which cargo fetches the first time the plugin is built:
wana_kana = "=5.0.0"unicode-normalization = "0.1"serde_json = { version = "1", features = ["preserve_order"] }Example
// A Japanese shop's customer form: names typed in romaji become kana, kana names get romaji for the address label, and half-width katakana from the bank's file is cleaned up.//// quillo run kana.qwe then open http://localhost:8080/kanause "kana.qwer"route GET /kana { { typed: kana_from_romaji("konnichiha"), typed_katakana: kana_from_romaji("KOOHII"), romaji: kana_to_romaji("とうきょう"), ramen: kana_to_romaji("ラーメン"), hiragana: kana_to_hiragana("カタカナ", false), hiragana_long: kana_to_hiragana("スーパー", true), katakana: kana_to_katakana("ひらがな"), bank_name: kana_fullwidth("ヤマダ タロウ ガイド"), info: kana_info("東京タワーへ行く"), }}// /furigana?name=やまだ たろう: the reading of a name as katakana for the furigana field, and romaji for the label.route GET /furigana ? { name: str } = { katakana: kana_to_katakana(name), romaji: upper(kana_to_romaji(name)) }$ quillo run kana.qwe$ curl 'localhost:8080/kana'$ curl 'localhost:8080/furigana?name=%E3%82%84%E3%81%BE%E3%81%A0%20%E3%81%9F%E3%82%8D%E3%81%86'Source
//! Japanese kana: romaji to hiragana and katakana as an input method types them, kana to Hepburn romaji for URLs and sorting, hiragana and katakana each to the other, half-width katakana (from bank and till systems) made full-width, and which scripts a text uses.//! version: 1.0.0//!//! [dependencies]//! wana_kana = "=5.0.0"//! unicode-normalization = "0.1"//! serde_json = { version = "1", features = ["preserve_order"] }use serde_json::{json, Value};use unicode_normalization::UnicodeNormalization;use wana_kana::{ConvertJapanese, IsJapaneseChar, IsJapaneseStr};/// Kana as Hepburn romaji (`"とうきょう"` is `"toukyou"`, `"ラーメン"` `"raamen"`), kanji and other text left as they are: for URLs, search and sorting names.pub fn kana_to_romaji(text: &str) -> String { text.to_romaji()}/// Romaji as an input method types it: lower case to hiragana, upper case to katakana (`"konnichiha"` is `"こんにちは"`, `"KOOHII"` `"コオヒイ"`), other kana kept.pub fn kana_from_romaji(text: &str) -> String { text.to_kana()}/// Everything in hiragana: katakana and romaji converted (`"カタカナ"` is `"かたかな"`); a long-vowel mark `ー` becomes its vowel unless `keep_long_mark`.pub fn kana_to_hiragana(text: &str, keep_long_mark: bool) -> String { text.to_hiragana_with_opt(wana_kana::Options { keep_prolonged_sound_mark: keep_long_mark, ..Default::default() })}/// Everything in katakana: hiragana and romaji converted (`"ひらがな"` is `"ヒラガナ"`), for names in forms and furigana fields.pub fn kana_to_katakana(text: &str) -> String { text.to_katakana()}/// Half-width katakana (`"ガイド"`, as old bank, POS and payment systems send it) as full-width (`"ガイド"`), voiced marks joined; the rest left as it is.pub fn kana_fullwidth(text: &str) -> String { let mut out = String::new(); let mut run = String::new(); for c in text.chars() { if ('\u{FF61}'..='\u{FF9F}').contains(&c) { run.push(c); } else { if !run.is_empty() { out.extend(run.nfkc()); run.clear(); } out.push(c); } } if !run.is_empty() { out.extend(run.nfkc()) } out}/// Which scripts a text uses: `{ japanese, hiragana, katakana, kanji, romaji, mixed, counts: { hiragana, katakana, kanji, latin, other } }`; `japanese` when all of it is (punctuation included), `mixed` when it has both kana and romaji.pub fn kana_info(text: &str) -> Value { let (mut h, mut k, mut kj, mut l, mut o) = (0, 0, 0, 0, 0); for c in text.chars() { if c.is_whitespace() { continue } if c.is_hiragana() { h += 1 } else if c.is_katakana() { k += 1 } else if c.is_kanji() { kj += 1 } else if c.is_ascii_alphabetic() { l += 1 } else { o += 1 } } json!({ "japanese": !text.trim().is_empty() && text.is_japanese(), "hiragana": !text.is_empty() && text.is_hiragana(), "katakana": !text.is_empty() && text.is_katakana(), "kanji": !text.is_empty() && text.is_kanji(), "romaji": !text.is_empty() && text.is_romaji(), "mixed": text.is_mixed(), "counts": { "hiragana": h, "katakana": k, "kanji": kj, "latin": l, "other": o }, })}Related
unicode.qwer · 1.0.0Unicode text made comparable and measured: NFC and NFKC forms, accents removed, characters counted as people see them, safe truncation and terminal width.5 views · 8 downloadsCase case.qwer · 1.0.0Text in snake_case, camelCase, PascalCase, kebab-case, CONSTANT_CASE, Title Case and Sentence case: column names as API keys and labels.7 views · 8 downloadsChinese words chinese_segment.qwer · 1.0.0Chinese text split into words for search indexes and counts, with parts of speech, and written in pinyin with tones, tone numbers or initials for sorting and finding names.7 views · 7 downloadsEmoji emoji.qwer · 1.0.0Emoji for chat and comments: :shortcodes: turned into emoji and back, emoji found with their names, stripped from subjects, and searched by name for a picker.7 views · 8 downloadsHumanize humanize.qwer · 1.0.0Durations, file sizes and relative times as people write them and back: 2h 30m, 1.5 GB, 2 GiB, 3 days ago, in 2 hours.7 views · 11 downloadsHyphenation hyphenation.qwer · 1.0.0Words hyphenated by the rules of 80 languages and spellings, patterns built in: a word's syllables, soft hyphens put in text and HTML for narrow columns and e-mails, and text wrapped at a width with words broken where they may be.6 views · 7 downloadsThread 0 posts
On Japanese kana: questions, tips, corrections, and code that uses it.
No posts yet. The first one opens the thread.