Use interface to allow for more complex transliteration implementations

This commit is contained in:
Elara 2021-10-04 17:45:26 -07:00
parent 9df6531023
commit 73f16fcfef
3 changed files with 69 additions and 44 deletions

View File

@ -73,11 +73,10 @@ func initNotifRelay(dev *infinitime.Device) error {
} }
maps := viper.GetStringSlice("notifs.translit.maps.use") maps := viper.GetStringSlice("notifs.translit.maps.use")
translit.Maps["custom"] = viper.GetStringSlice("notifs.translit.maps.custom") translit.Maps["custom"] = translit.Map(viper.GetStringSlice("notifs.translit.maps.custom"))
replacer := translit.NewReplacer(maps...) sender = translit.Transliterate(sender, maps...)
sender = replacer.Replace(sender) summary = translit.Transliterate(summary, maps...)
summary = replacer.Replace(summary) body = translit.Transliterate(body, maps...)
body = replacer.Replace(body)
var msg string var msg string
// If summary does not exist, set message to body. // If summary does not exist, set message to body.

View File

@ -171,10 +171,11 @@ func handleConnection(conn net.Conn, dev *infinitime.Device) {
break break
} }
maps := viper.GetStringSlice("notifs.translit.maps.use") maps := viper.GetStringSlice("notifs.translit.maps.use")
translit.Maps["custom"] = viper.GetStringSlice("notifs.translit.maps.custom") translit.Maps["custom"] = translit.Map(viper.GetStringSlice("notifs.translit.maps.custom"))
replacer := translit.NewReplacer(maps...) title := translit.Transliterate(reqData.Title, maps...)
body := translit.Transliterate(reqData.Body, maps...)
// Send notification to watch // Send notification to watch
err = dev.Notify(replacer.Replace(reqData.Title), replacer.Replace(reqData.Body)) err = dev.Notify(title, body)
if err != nil { if err != nil {
connErr(conn, err, "Error sending notification") connErr(conn, err, "Error sending notification")
break break

View File

@ -4,17 +4,60 @@ import (
"strings" "strings"
) )
// Transliterate runs the given maps on s and returns the result
func Transliterate(s string, useMaps ...string) string {
// Create variable to store modified string
out := s
// If custom map exists
if customMap, ok := Maps["custom"]; ok {
// Perform transliteration with it
out = customMap.Transliterate(out)
}
// For every map to use
for _, useMap := range useMaps {
// If custom, skip
if useMap == "custom" {
continue
}
// Get requested map
translitMap, ok := Maps[useMap]
if !ok {
continue
}
// Perform transliteration
out = translitMap.Transliterate(out)
}
// Return result
return out
}
// Transliterator is implemented by anything with a
// Transliterate method, which performs transliteration
// and returns the resulting string.
type Transliterator interface {
Transliterate(string) string
}
// Map implements Transliterator using a slice where
// every odd element is a key and every even one is a value
// which replaces the key.
type Map []string
func (mt Map) Transliterate(s string) string {
return strings.NewReplacer(mt...).Replace(s)
}
// Maps stores transliteration maps as slices to preserve order. // Maps stores transliteration maps as slices to preserve order.
// Some of these maps were sourced from https://codeberg.org/Freeyourgadget/Gadgetbridge // Some of these maps were sourced from https://codeberg.org/Freeyourgadget/Gadgetbridge
var Maps = map[string][]string{ var Maps = map[string]Transliterator{
"eASCII": { "eASCII": Map{
"œ", "oe", "œ", "oe",
"ª", "a", "ª", "a",
"°", "o", "°", "o",
"«", `"`, "«", `"`,
"»", `"`, "»", `"`,
}, },
"Scandinavian": { "Scandinavian": Map{
"Æ", "Ae", "Æ", "Ae",
"æ", "ae", "æ", "ae",
"Ø", "Oe", "Ø", "Oe",
@ -22,7 +65,7 @@ var Maps = map[string][]string{
"Å", "Aa", "Å", "Aa",
"å", "aa", "å", "aa",
}, },
"German": { "German": Map{
"ä", "ae", "ä", "ae",
"ö", "oe", "ö", "oe",
"ü", "ue", "ü", "ue",
@ -32,7 +75,7 @@ var Maps = map[string][]string{
"ß", "ss", "ß", "ss",
"ẞ", "SS", "ẞ", "SS",
}, },
"Hebrew": { "Hebrew": Map{
"א", "a", "א", "a",
"ב", "b", "ב", "b",
"ג", "g", "ג", "g",
@ -61,7 +104,7 @@ var Maps = map[string][]string{
"ם", "m", "ם", "m",
"ן", "n", "ן", "n",
}, },
"Greek": { "Greek": Map{
"α", "a", "α", "a",
"ά", "a", "ά", "a",
"β", "v", "β", "v",
@ -132,11 +175,11 @@ var Maps = map[string][]string{
"Ω", "O", "Ω", "O",
"Ώ", "O", "Ώ", "O",
}, },
"Russian": { "Russian": Map{
"Ё", "Йo", "Ё", "Йo",
"ё", "йo", "ё", "йo",
}, },
"Ukranian": { "Ukranian": Map{
"ґ", "gh", "ґ", "gh",
"є", "je", "є", "je",
"і", "i", "і", "i",
@ -146,7 +189,7 @@ var Maps = map[string][]string{
"І", "I", "І", "I",
"Ї", "JI", "Ї", "JI",
}, },
"Arabic": { "Arabic": Map{
"ا", "a", "ا", "a",
"ب", "b", "ب", "b",
"ت", "t", "ت", "t",
@ -194,7 +237,7 @@ var Maps = map[string][]string{
"٨", "8", "٨", "8",
"٩", "9", "٩", "9",
}, },
"Farsi": { "Farsi": Map{
"پ", "p", "پ", "p",
"چ", "ch", "چ", "ch",
"ژ", "zh", "ژ", "zh",
@ -223,11 +266,11 @@ var Maps = map[string][]string{
"ُ", "o", "ُ", "o",
"ّ", "", "ّ", "",
}, },
"Polish": { "Polish": Map{
"Ł", "L", "Ł", "L",
"ł", "l", "ł", "l",
}, },
"Lithuanian": { "Lithuanian": Map{
"ą", "a", "ą", "a",
"č", "c", "č", "c",
"ę", "e", "ę", "e",
@ -238,7 +281,7 @@ var Maps = map[string][]string{
"ū", "u", "ū", "u",
"ž", "z", "ž", "z",
}, },
"Estonian": { "Estonian": Map{
"ä", "a", "ä", "a",
"Ä", "A", "Ä", "A",
"ö", "o", "ö", "o",
@ -248,13 +291,13 @@ var Maps = map[string][]string{
"ü", "u", "ü", "u",
"Ü", "U", "Ü", "U",
}, },
"Icelandic": { "Icelandic": Map{
"Þ", "Th", "Þ", "Th",
"þ", "th", "þ", "th",
"Ð", "D", "Ð", "D",
"ð", "d", "ð", "d",
}, },
"Czeck": { "Czeck": Map{
"ř", "r", "ř", "r",
"ě", "e", "ě", "e",
"ý", "y", "ý", "y",
@ -268,7 +311,7 @@ var Maps = map[string][]string{
"ť", "t", "ť", "t",
"ň", "n", "ň", "n",
}, },
"French": { "French": Map{
"à", "a", "à", "a",
"â", "a", "â", "a",
"é", "e", "é", "e",
@ -280,7 +323,7 @@ var Maps = map[string][]string{
"ÿ", "y", "ÿ", "y",
"ç", "c", "ç", "c",
}, },
"Armenian": { "Armenian": Map{
"աու", "au", "աու", "au",
"բու", "bu", "բու", "bu",
"գու", "gu", "գու", "gu",
@ -393,7 +436,7 @@ var Maps = map[string][]string{
"ֆ", "f", "ֆ", "f",
"ւ", "", "ւ", "",
}, },
"Emoji": { "Emoji": Map{
"😂", ":')", "😂", ":')",
"😊", ":)", "😊", ":)",
"😃", ":)", "😃", ":)",
@ -422,21 +465,3 @@ var Maps = map[string][]string{
"💤", ":zzz:", "💤", ":zzz:",
}, },
} }
func NewReplacer(useMaps ...string) *strings.Replacer {
var replace []string
if customMap, ok := Maps["custom"]; ok {
replace = append(replace, customMap...)
}
for _, useMap := range useMaps {
if useMap == "custom" {
continue
}
translitMap, ok := Maps[useMap]
if !ok {
continue
}
replace = append(replace, translitMap...)
}
return strings.NewReplacer(replace...)
}