-
Notifications
You must be signed in to change notification settings - Fork 1
/
language.go
183 lines (167 loc) · 5.11 KB
/
language.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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
// Copyright © 2019 rangertaha rangertaha@gmail.com
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
package languages
import (
"strings"
)
type (
// Language type
Language struct {
Code string `json:"code,omitempty"`
Name string `json:"name,omitempty"`
Numerals map[string][]string `json:"numerals,omitempty"`
Graphemes []string `json:"graphemes,omitempty"`
Vowels []string `json:"vowels,omitempty"`
Misspellings [][]string `json:"misspellings,omitempty"`
Homophones [][]string `json:"homophones,omitempty"`
Antonyms map[string][]string `json:"antonyms,omitempty"`
Homoglyphs map[string][]string `json:"homoglyphs,omitempty"`
}
// Keyboard type
Keyboard struct {
Code string `json:"code,omitempty"`
Name string `json:"name,omitempty"`
Description string `json:"description,omitempty"`
Language Language `json:"language,omitempty"`
Layout []string `json:"layout,omitempty"`
}
// KeyboardGroup type
KeyboardGroup struct {
Code string `json:"code,omitempty"`
Keyboards []string `json:"keyboards,omitempty"`
Description string `json:"description,omitempty"`
}
// KeyboardRegistry stores registered keyboards and groups
KeyboardRegistry struct {
registry map[string][]Keyboard
}
)
// KEYBOARDS stores all the registered keyboards
var KEYBOARDS = NewKeyboardRegistry()
// NewKeyboardRegistry returns a new KeyboardRegistry
func NewKeyboardRegistry() KeyboardRegistry {
return KeyboardRegistry{
registry: make(map[string][]Keyboard),
}
}
// Add allows you to add keyboards to the registry
func (kb *KeyboardRegistry) Add(keyboards []Keyboard) {
for _, board := range keyboards {
kb.registry[strings.ToUpper(board.Code)] = []Keyboard{board}
}
}
// Append allows you to append keyboards to a group name
func (kb *KeyboardRegistry) Append(name string, keyboards []Keyboard) {
key := strings.ToUpper(name)
kbs, ok := kb.registry[key]
if ok {
for _, board := range keyboards {
kbs = append(kbs, board)
}
kb.registry[key] = kbs
} else {
kb.registry[key] = keyboards
}
}
// Keyboards looks up and returns Keyboards.
func (kb *KeyboardRegistry) Keyboards(names ...string) (kbs []Keyboard) {
for _, name := range names {
keyboards, ok := kb.registry[strings.ToUpper(name)]
if ok {
for _, keyboard := range keyboards {
kbs = append(kbs, keyboard)
}
}
}
return
}
// Adjacent returns adjacent characters on the given keyboard
func (urli *Keyboard) Adjacent(char string) (chars []string) {
for r, row := range urli.Layout {
for c := range row {
var top, bottom, left, right string
if char == string(urli.Layout[r][c]) {
if r > 0 {
top = string(urli.Layout[r-1][c])
if top != " " {
chars = append(chars, top)
}
}
if r < len(urli.Layout)-1 {
bottom = string(urli.Layout[r+1][c])
if bottom != " " {
chars = append(chars, bottom)
}
}
if c > 0 {
left = string(urli.Layout[r][c-1])
if left != " " {
chars = append(chars, left)
}
}
if c < len(row)-1 {
right = string(urli.Layout[r][c+1])
if right != " " {
chars = append(chars, right)
}
}
}
}
}
return chars
}
// SimilarChars ...
func (lang *Language) SimilarChars(key string) (chars []string) {
char, ok := lang.Homoglyphs[key]
if ok {
chars = append(chars, char...)
}
return chars
}
// SimilarSpellings ...
func (lang *Language) SimilarSpellings(str string) (words []string) {
for _, wordset := range lang.Misspellings {
for _, word := range wordset {
if strings.Contains(str, word) {
for _, w := range wordset {
if w != word {
words = append(words, strings.Replace(str, word, w, -1))
}
}
}
}
}
return
}
// SimilarSounds ...
func (lang *Language) SimilarSounds(str string) (words []string) {
for _, wordset := range lang.Homophones {
for _, word := range wordset {
if strings.Contains(str, word) {
for _, w := range wordset {
if w != word {
words = append(words, strings.Replace(str, word, w, -1))
}
}
}
}
}
return
}