This repository has been archived by the owner on May 14, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 165
/
utilities.go
97 lines (84 loc) · 1.78 KB
/
utilities.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
package prose
import (
"bytes"
"encoding/gob"
"io/fs"
"path"
"strconv"
"strings"
)
// checkError panics if `err` is not `nil`.
func checkError(err error) {
if err != nil {
panic(err)
}
}
// min returns the minimum of `a` and `b`.
func min(a, b int) int {
if a < b {
return a
}
return b
}
// isPunct determines if the string represents a number.
func isNumeric(s string) bool {
_, err := strconv.ParseFloat(s, 64)
return err == nil
}
// stringInSlice determines if `slice` contains the string `a`.
func stringInSlice(a string, slice []string) bool {
for _, b := range slice {
if a == b {
return true
}
}
return false
}
func getAsset(folder, name string) *gob.Decoder {
b, err := Asset(path.Join("model", folder, name))
checkError(err)
return gob.NewDecoder(bytes.NewReader(b))
}
func getDiskAsset(file fs.File) *gob.Decoder {
return gob.NewDecoder(file)
}
func hasAnyPrefix(s string, prefixes []string) bool {
n := len(s)
for _, prefix := range prefixes {
if n > len(prefix) && strings.HasPrefix(s, prefix) {
return true
}
}
return false
}
func hasAnySuffix(s string, suffixes []string) bool {
n := len(s)
for _, suffix := range suffixes {
if n > len(suffix) && strings.HasSuffix(s, suffix) {
return true
}
}
return false
}
func hasAnyIndex(s string, suffixes []string) int {
n := len(s)
for _, suffix := range suffixes {
idx := strings.Index(s, suffix)
if idx >= 0 && n > len(suffix) {
return idx
}
}
return -1
}
func nSuffix(word string, length int) string {
return strings.ToLower(word[len(word)-min(len(word), length):])
}
func nPrefix(word string, length int) string {
return strings.ToLower(word[:min(len(word), length)])
}
func isBasic(word string) string {
if stringInSlice(word, enWordList) {
return "True"
}
return "False"
}