-
Notifications
You must be signed in to change notification settings - Fork 0
/
leetspeak.go
executable file
·49 lines (39 loc) · 999 Bytes
/
leetspeak.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
package leetspeak
import (
"encoding/json"
"io/ioutil"
"math/rand"
"os"
"strings"
)
func getAlphabets() map[string][]string {
baseDir, _ := os.Getwd()
//var alphabetics *Alphabet
alphabetics := make(map[string][]string)
// Open our jsonFile
jsonFile, err := os.Open(baseDir + "/alphabet.json")
// if we os.Open returns an error then handle it
if err != nil {
panic(err)
}
byteValue, _ := ioutil.ReadAll(jsonFile)
// defer the closing of our jsonFile so that we can parse it later on
defer jsonFile.Close()
err = json.Unmarshal(byteValue, &alphabetics)
return alphabetics
}
func getRndLeet(substitutions []string) string {
rnd := rand.Intn(len(substitutions))
rndLeet := substitutions[rnd]
return rndLeet
}
func ConvertToLeet(text string) string {
alphabetics := getAlphabets()
var leet string = ""
for _, char := range strings.ToLower(text) {
substitutions := alphabetics[string(char)]
rndLeet := getRndLeet(substitutions)
leet += rndLeet
}
return leet
}