Text

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

#unicode#languages#forms#text

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) → str

Kana 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) → str

Romaji 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) → str

Everything in hiragana: katakana and romaji converted ("カタカナ" is "かたかな"); a long-vowel mark ー becomes its vowel unless keep_long_mark.

kana_to_katakana(text: str) → str

Everything in katakana: hiragana and romaji converted ("ひらがな" is "ヒラガナ"), for names in forms and furigana fields.

kana_fullwidth(text: str) → str

Half-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) → any

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.

Get it

$ quillo install kana               # kana.qwer into this folder$ quillo install kana --example     # and its example, kana.qwe

Or with curl:

$ curl -O https://quillolang.org/plugins/files/kana.qwer$ curl -O https://quillolang.org/plugins/files/kana.qwe

Then, in your program, all of it or only what you call:

main.qwe
use kana.qweruse kana_to_romaji, kana_from_romaji from kana      // Quillo 0.6: only these

It uses these crates, which cargo fetches the first time the plugin is built:

[dependencies]
wana_kana = "=5.0.0"unicode-normalization = "0.1"serde_json = { version = "1", features = ["preserve_order"] }

Example

kana.qwe
// 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

kana.qwer
//! 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 },    })}
Thread
610Useful? Like it, or add to its thread below.

Thread 0 posts

On Japanese kana: questions, tips, corrections, and code that uses it.

No posts yet. The first one opens the thread.