BlueGit

timfa bluegit

Files

bluegit / internal / i18n / cuneiform.go

cuneiform.go

fix: schrijf de tagpagina volledig in spijkerschrift
timfa project_87923614_bot_3aff574f6f98414b945a8799374f9092 · pushed by timfa · 3 weeks ago
Failed eb7dced7 History
cuneiform.go 7.1 KiB Download Raw
1package i18n
2
3import "strings"
4
5// Transliterate writes English phonetically in Sumero-Akkadian
6// cuneiform (Unicode U+12000). Digits and punctuation stay, so a
7// sentence keeps its shape. Printf verbs (%s, %d, %q, %%) stay
8// literal, so a dialog can still substitute a name. The spelling
9// is syllabic: consonants without a following vowel take an
10// inherent a, o is written u, and there is no separate sign for
11// f, v or o. igh and eigh are the same diphthong as ai.
12func Transliterate(s string) string {
13 var b strings.Builder
14 word := strings.Builder{}
15 flush := func() {
16 if word.Len() == 0 {
17 return
18 }
19 b.WriteString(encodeWord(word.String()))
20 word.Reset()
21 }
22 rs := []rune(s)
23 for i := 0; i < len(rs); i++ {
24 if n := printfVerb(rs[i:]); n > 0 {
25 flush()
26 b.WriteString(string(rs[i : i+n]))
27 i += n - 1
28 continue
29 }
30 r := rs[i]
31 if isLetter(r) || r == '\'' {
32 word.WriteRune(r)
33 continue
34 }
35 flush()
36 b.WriteRune(r)
37 }
38 flush()
39 return b.String()
40}
41
42// printfVerb is the length of a Go fmt verb at the start of rs,
43// including %%. Zero means rs does not begin with a verb.
44func printfVerb(rs []rune) int {
45 if len(rs) < 2 || rs[0] != '%' {
46 return 0
47 }
48 if rs[1] == '%' {
49 return 2
50 }
51 i := 1
52 for i < len(rs) && strings.ContainsRune("#0-+ ", rs[i]) {
53 i++
54 }
55 if i < len(rs) && rs[i] == '[' {
56 j := i + 1
57 for j < len(rs) && rs[j] >= '0' && rs[j] <= '9' {
58 j++
59 }
60 if j > i+1 && j < len(rs) && rs[j] == ']' {
61 i = j + 1
62 }
63 }
64 for i < len(rs) && rs[i] >= '0' && rs[i] <= '9' {
65 i++
66 }
67 if i < len(rs) && rs[i] == '.' {
68 k := i + 1
69 for k < len(rs) && rs[k] >= '0' && rs[k] <= '9' {
70 k++
71 }
72 if k > i+1 {
73 i = k
74 }
75 }
76 if i < len(rs) && isPrintfVerb(rs[i]) {
77 return i + 1
78 }
79 return 0
80}
81
82func isPrintfVerb(r rune) bool {
83 switch r {
84 case 'b', 'c', 'd', 'e', 'E', 'f', 'g', 'G', 'o', 'p', 'q', 's', 't', 'T', 'U', 'v', 'w', 'x', 'X':
85 return true
86 default:
87 return false
88 }
89}
90
91func isLetter(r rune) bool {
92 return (r >= 'a' && r <= 'z') || (r >= 'A' && r <= 'Z')
93}
94
95func encodeWord(raw string) string {
96 ph := phonemes(raw)
97 if len(ph) == 0 {
98 return ""
99 }
100 var out []rune
101 i := 0
102 for i < len(ph) {
103 if i+2 < len(ph) && isC(ph[i]) && isV(ph[i+1]) && isC(ph[i+2]) && (i+3 == len(ph) || isC(ph[i+3])) {
104 if sign, ok := signs[ph[i]+ph[i+1]+ph[i+2]]; ok {
105 out = append(out, sign)
106 i += 3
107 continue
108 }
109 }
110 if i+1 < len(ph) && isC(ph[i]) && isV(ph[i+1]) {
111 if sign, ok := cv(ph[i], ph[i+1]); ok {
112 out = append(out, sign)
113 i += 2
114 continue
115 }
116 }
117 if i+1 < len(ph) && isV(ph[i]) && isC(ph[i+1]) && (i+2 == len(ph) || isC(ph[i+2])) {
118 if sign, ok := signs[ph[i]+ph[i+1]]; ok {
119 out = append(out, sign)
120 i += 2
121 continue
122 }
123 }
124 if isV(ph[i]) {
125 if sign, ok := signs[ph[i]]; ok {
126 out = append(out, sign)
127 }
128 i++
129 continue
130 }
131 if sign, ok := cv(ph[i], "a"); ok {
132 out = append(out, sign)
133 }
134 i++
135 }
136 return string(out)
137}
138
139func cv(c, v string) (rune, bool) {
140 if v == "e" {
141 if sign, ok := signs[c+"e"]; ok {
142 return sign, true
143 }
144 if sign, ok := signs[c+"i"]; ok {
145 return sign, true
146 }
147 }
148 sign, ok := signs[c+v]
149 return sign, ok
150}
151
152func isC(s string) bool { return s != "a" && s != "e" && s != "i" && s != "u" }
153func isV(s string) bool { return !isC(s) }
154
155func phonemes(raw string) []string {
156 s := strings.ToLower(raw)
157 var out []string
158 for i := 0; i < len(s); i++ {
159 if i+3 < len(s) && s[i:i+4] == "eigh" {
160 out = append(out, "e")
161 i += 3
162 continue
163 }
164 if i+2 < len(s) && s[i:i+3] == "igh" {
165 out = append(out, "e")
166 i += 2
167 continue
168 }
169 if i+1 < len(s) {
170 switch s[i : i+2] {
171 case "qu":
172 out = append(out, "k", "w")
173 i++
174 continue
175 case "ck":
176 out = append(out, "k")
177 i++
178 continue
179 case "ph":
180 out = append(out, "p")
181 i++
182 continue
183 case "th":
184 out = append(out, "t")
185 i++
186 continue
187 case "sh":
188 out = append(out, "š")
189 i++
190 continue
191 case "ch":
192 out = append(out, "š")
193 i++
194 continue
195 case "wh":
196 out = append(out, "w")
197 i++
198 continue
199 case "ng":
200 out = append(out, "n")
201 i++
202 continue
203 case "ee", "ea":
204 out = append(out, "i")
205 i++
206 continue
207 case "oo", "ou":
208 out = append(out, "u")
209 i++
210 continue
211 case "ai", "ay":
212 out = append(out, "e")
213 i++
214 continue
215 }
216 }
217 c := s[i]
218 switch c {
219 case 'a', 'e', 'i', 'u':
220 out = append(out, string(c))
221 case 'o':
222 out = append(out, "u")
223 case 'y':
224 if i == 0 || isVowelByte(s[i-1]) {
225 out = append(out, "y")
226 } else {
227 out = append(out, "i")
228 }
229 case 'c':
230 if i+1 < len(s) && (s[i+1] == 'e' || s[i+1] == 'i' || s[i+1] == 'y') {
231 out = append(out, "s")
232 } else {
233 out = append(out, "k")
234 }
235 case 'f':
236 out = append(out, "p")
237 case 'v':
238 out = append(out, "b")
239 case 'j':
240 out = append(out, "y")
241 case 'q':
242 out = append(out, "k")
243 case 'x':
244 out = append(out, "k", "s")
245 case 'b', 'd', 'g', 'h', 'k', 'l', 'm', 'n', 'p', 'r', 's', 't', 'w', 'z':
246 out = append(out, string(c))
247 }
248 }
249 compact := out[:0]
250 for _, p := range out {
251 if len(compact) > 0 && compact[len(compact)-1] == p && isC(p) {
252 continue
253 }
254 compact = append(compact, p)
255 }
256 return compact
257}
258
259func isVowelByte(c byte) bool {
260 return c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u' || c == 'y'
261}
262
263// signs maps a conventional Akkadian reading to one sign.
264// pu is written with BU, and w+vowel with PI, which are the usual values.
265var signs = map[string]rune{
266 "a": '𒀀', "e": '𒂊', "i": '𒄿', "u": '𒌋',
267 "ba": '𒁀', "bi": '𒁉', "bu": '𒁍', "pu": '𒁍',
268 "da": '𒁕', "di": '𒁲', "du": '𒁺',
269 "ga": '𒂵', "gi": '𒄀', "gu": '𒄖',
270 "ha": '𒄩', "hi": '𒄭', "hu": '𒄷',
271 "ka": '𒅗', "ki": '𒆠', "ku": '𒆪',
272 "la": '𒆷', "li": '𒇷', "lu": '𒇻',
273 "ma": '𒈠', "me": '𒈨', "mi": '𒈪', "mu": '𒈬',
274 "na": '𒈾', "ne": '𒉈', "ni": '𒉌', "nu": '𒉡',
275 "pa": '𒉺', "pi": '𒉿',
276 "ra": '𒊏', "ri": '𒊑', "ru": '𒊒',
277 "sa": '𒊓', "si": '𒋛', "su": '𒋢',
278 "ša": '𒊭', "še": '𒊺', "ši": '𒅆', "šu": '𒋗',
279 "ta": '𒋫', "te": '𒋼', "ti": '𒋾', "tu": '𒌅',
280 "wa": '𒉿', "we": '𒉿', "wi": '𒉿', "wu": '𒉿',
281 "ya": '𒅀', "za": '𒍝', "zi": '𒍣', "zu": '𒍪',
282 "ab": '𒀊', "ad": '𒀜', "ak": '𒀝', "al": '𒀠', "an": '𒀭', "aš": '𒀸',
283 "el": '𒂖', "en": '𒂗', "eš": '𒂠',
284 "ib": '𒅁', "ig": '𒅅', "il": '𒅋', "im": '𒅎', "in": '𒅔', "ir": '𒅕', "iš": '𒅖',
285 "ub": '𒌒', "ud": '𒌓', "um": '𒌝', "un": '𒌦', "ur": '𒌨', "uš": '𒍑',
286 "bad": '𒁁', "bal": '𒁄', "bar": '𒁇',
287 "dag": '𒁖', "dam": '𒁮', "dar": '𒁯',
288 "dib": '𒁳', "dim": '𒁴', "din": '𒁷',
289 "dub": '𒁾', "dug": '𒂁', "dun": '𒂄',
290 "gal": '𒃲', "gam": '𒃵', "gan": '𒃶', "gar": '𒃻',
291 "gul": '𒄢', "gur": '𒄥',
292 "hal": '𒄬',
293 "kal": '𒆗', "kin": '𒆥', "kul": '𒆰', "kur": '𒆳',
294 "lal": '𒇲', "lam": '𒇴', "lum": '𒈝',
295 "mar": '𒈥',
296 "nam": '𒉆', "nun": '𒉣',
297 "pad": '𒉻', "pap": '𒉽',
298 "rab": '𒊐',
299 "sag": '𒊕', "sar": '𒊬',
300 "tab": '𒋰', "tag": '𒋳', "tar": '𒋻',
301 "tum": '𒌈', "tur": '𒌉',
302 "zag": '𒍠',
303}