-
Notifications
You must be signed in to change notification settings - Fork 0
/
transliteration.go
89 lines (80 loc) · 2.65 KB
/
transliteration.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
// Package transliteration does translit from Ukrainian to Latin.
//
// According to the rules of transliteration, that are described in the resolution
// of the Cabinet of Ministers of Ukraine №55 dated January 27, 2010.
//
// https://zakon.rada.gov.ua/laws/show/55-2010-%D0%BF#Text
package transliteration
import "unicode"
// UkrToLat transliterates Ukrainian text into Latin in accordance with established rules
func UkrToLat(ukrainianText string) (transliteratedText string) {
characters := []rune(ukrainianText)
numberOfChars := len(characters)
skipNextChar := false
for i, currentChar := range characters {
if skipNextChar {
skipNextChar = false
continue
}
if latChar, isUrkChar := ukrToLatRules[currentChar]; isUrkChar {
switch currentChar {
// Process exceptions for "Зг", "зг"
case 'З', 'з':
if i+1 != numberOfChars { // has next char
nextChar := characters[i+1]
if nextChar == 'Г' || nextChar == 'г' {
transliteratedText += processZghException(currentChar, nextChar)
skipNextChar = true
continue
}
}
// Process exceptions inside word
case 'є', 'ї', 'Й', 'й', 'ю', 'я':
// Has prev char
if i != 0 {
if _, prevCharacterIsUkrainian := ukrToLatRules[characters[i-1]]; prevCharacterIsUkrainian {
transliteratedText += insideWord[currentChar]
continue
}
}
// Process uppercase exceptions for vowels
case 'Ї', 'Є', 'Ю', 'Я':
// Has prev char
if i != 0 {
if _, prevCharIsUkr := ukrToLatRules[characters[i-1]]; prevCharIsUkr {
transliteratedText += insideWord[currentChar]
continue
}
}
// Previous or next character is also uppercase
if (i != 0 && unicode.IsUpper(characters[i-1])) || (i+1 != numberOfChars && unicode.IsUpper(characters[i+1])) {
transliteratedText += uppercaseVowels[currentChar]
continue
}
// Process uppercase exceptions for consonants
case 'Ж', 'Х', 'Ц', 'Ч', 'Ш', 'Щ':
// Previous or next character is also uppercase
if (i != 0 && unicode.IsUpper(characters[i-1])) || (i+1 != numberOfChars && unicode.IsUpper(characters[i+1])) {
transliteratedText += uppercaseConsonants[currentChar]
continue
}
}
transliteratedText += latChar
} else {
transliteratedText += string(currentChar)
}
}
return
}
func processZghException(firstChar, secondChar rune) (result string) {
if firstChar == 'З' && secondChar == 'г' {
result = "Zgh"
} else if firstChar == 'З' && secondChar == 'Г' {
result = "ZGH"
} else if firstChar == 'з' && secondChar == 'г' {
result = "zgh"
} else if firstChar == 'з' && secondChar == 'Г' {
result = "zGH"
}
return
}