From 8019a3521e8a41b195e1703b9c0fc4b949f62cc5 Mon Sep 17 00:00:00 2001 From: Elara Musayelyan Date: Mon, 4 Oct 2021 22:26:16 -0700 Subject: [PATCH] Add Chinese transliteration via Pinyin conversion library --- README.md | 1 + go.mod | 1 + go.sum | 2 ++ translit/chinese.go | 42 ++++++++++++++++++++++++++++++++++++++++++ translit/translit.go | 3 ++- 5 files changed, 48 insertions(+), 1 deletion(-) create mode 100644 translit/chinese.go diff --git a/README.md b/README.md index 0e3ca2e..9d9e920 100644 --- a/README.md +++ b/README.md @@ -58,6 +58,7 @@ Since the PineTime does not have enough space to store all unicode glyphs, it on - French - Armenian - Korean +- Chinese - Emoji Place the desired map names in an array as `notifs.translit.use`. They will be evaluated in order. You can also put custom transliterations in `notifs.translit.custom`. These take priority over any other maps. The `notifs.translit` config section should look like this: diff --git a/go.mod b/go.mod index b782fd2..7d21b81 100644 --- a/go.mod +++ b/go.mod @@ -15,6 +15,7 @@ require ( github.com/mattn/go-isatty v0.0.13 // indirect github.com/mattn/go-runewidth v0.0.13 // indirect github.com/mitchellh/mapstructure v1.4.1 + github.com/mozillazg/go-pinyin v0.18.0 github.com/rs/zerolog v1.23.0 github.com/sirupsen/logrus v1.8.1 // indirect github.com/spf13/cast v1.4.1 // indirect diff --git a/go.sum b/go.sum index 1e607dd..873e736 100644 --- a/go.sum +++ b/go.sum @@ -242,6 +242,8 @@ github.com/mitchellh/mapstructure v1.4.1/go.mod h1:bFUtVrKA4DC2yAKiSyO/QUcy7e+RR github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q= github.com/modern-go/reflect2 v0.0.0-20180701023420-4b7aa43c6742/go.mod h1:bx2lNnkwVCuqBIxFjflWJWanXIb3RllmbCylyMrvgv0= github.com/modern-go/reflect2 v1.0.1/go.mod h1:bx2lNnkwVCuqBIxFjflWJWanXIb3RllmbCylyMrvgv0= +github.com/mozillazg/go-pinyin v0.18.0 h1:hQompXO23/0ohH8YNjvfsAITnCQImCiR/Fny8EhIeW0= +github.com/mozillazg/go-pinyin v0.18.0/go.mod h1:iR4EnMMRXkfpFVV5FMi4FNB6wGq9NV6uDWbUuPhP4Yc= github.com/muka/go-bluetooth v0.0.0-20210812063148-b6c83362e27d h1:EG/xyWjHT19rkUpwsWSkyiCCmyqNwFovr9m10rhyOxU= github.com/muka/go-bluetooth v0.0.0-20210812063148-b6c83362e27d/go.mod h1:dMCjicU6vRBk34dqOmIZm0aod6gUwZXOXzBROqGous0= github.com/nfnt/resize v0.0.0-20180221191011-83c6a9932646/go.mod h1:jpp1/29i3P1S/RLdc7JQKbRpFeM1dOBd8T9ki5s+AY8= diff --git a/translit/chinese.go b/translit/chinese.go new file mode 100644 index 0000000..252210d --- /dev/null +++ b/translit/chinese.go @@ -0,0 +1,42 @@ +package translit + +import ( + "bytes" + "strings" + "unicode" + + "github.com/mozillazg/go-pinyin" +) + +// ChineseTranslit implements Transliterator using a pinyin +// conversion library. +type ChineseTranslit struct{} + +func (ct *ChineseTranslit) Transliterate(s string) string { + // Create buffer for final output + outBuf := &bytes.Buffer{} + // Create buffer to temporarily store chinese characters + tmpBuf := &bytes.Buffer{} + // For every character in string + for _, char := range s { + // If character in Han range + if unicode.Is(unicode.Han, char) { + // Write character to temporary buffer + tmpBuf.WriteRune(char) + } else { + // If buffer contains characters + if tmpBuf.Len() > 0 { + // Convert to pinyin (without tones) + out := pinyin.LazyConvert(tmpBuf.String(), nil) + // Write space-separated string to output + outBuf.WriteString(strings.Join(out, " ")) + // Reset temporary buffer + tmpBuf.Reset() + } + // Write character to output + outBuf.WriteRune(char) + } + } + // Return output string + return outBuf.String() +} diff --git a/translit/translit.go b/translit/translit.go index 63f8492..fd03bfd 100644 --- a/translit/translit.go +++ b/translit/translit.go @@ -464,5 +464,6 @@ var Transliterators = map[string]Transliterator{ "😴", ":zzz:", "💤", ":zzz:", }, - "Korean": &KoreanTranslit{}, + "Korean": &KoreanTranslit{}, + "Chinese": &ChineseTranslit{}, }