-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsinglish.go
108 lines (102 loc) · 2.25 KB
/
singlish.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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
package singlish
import (
"strings"
"unicode"
)
const escape = ` !"#$%&'()*+,-./:;<=>?@[\]^_{|}~` + "`"
// TranslateString will return the translated sinhala word and english words only.
// this function will remove any existing escape characters defined in this library.
func TranslateString(text string) (translated string) {
l := []rune(text)
for i, c := range l {
if 0 < c && c < 128 {
if strings.Contains(escape, string(c)) {
if len(translated) > 0 && string(translated[len(translated)-1]) != " " {
translated = translated + " "
}
continue
}
translated = translated + string(c)
} else if unicode.Is(unicode.Sinhala, c) {
if unicode.IsLetter(c) {
if VowelsStart <= c && c <= VowelsEnd {
let, ok := Vowels[string(c)]
if !ok {
continue
}
translated += let
continue
}
if ConstStart <= c && c <= ConstEnd {
let, ok := Constants[string(c)]
if !ok {
continue
}
if len(l)-1 >= i+1 && !unicode.IsMark(l[i+1]) || len(l) == i+1 {
let += "a"
}
translated += let
continue
}
} else if unicode.IsMark(c) {
if i > 0 {
if let, ok := R[string(l[i-1])]; ok {
translated += let
continue
}
}
let, ok := LangModifiers[string(c)]
if !ok {
continue
}
translated += let
continue
}
}
}
return translated
}
func TranslateSinhalaStr(text string) (translated string) {
l := []rune(text)
for i, c := range l {
if unicode.Is(unicode.Sinhala, c) {
if unicode.IsLetter(c) {
if VowelsStart <= c && c <= VowelsEnd {
let, ok := Vowels[string(c)]
if !ok {
continue
}
translated += let
continue
}
if ConstStart <= c && c <= ConstEnd {
let, ok := Constants[string(c)]
if !ok {
continue
}
if len(l)-1 >= i+1 && !unicode.IsMark(l[i+1]) || len(l) == i+1 {
let += "a"
}
translated += let
continue
}
} else if unicode.IsMark(c) {
if i > 0 {
if let, ok := R[string(l[i-1])]; ok {
translated += let
continue
}
}
let, ok := LangModifiers[string(c)]
if !ok {
continue
}
translated += let
continue
}
} else if unicode.IsSpace(c) {
translated += " "
}
}
return translated
}