-
Notifications
You must be signed in to change notification settings - Fork 6
/
helpers.go
120 lines (108 loc) · 2.53 KB
/
helpers.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
package iuliia
import (
"fmt"
"sort"
"strings"
"unicode"
"unicode/utf8"
)
func isCyrillic(c rune) bool {
return unicode.Is(unicode.Cyrillic, c)
}
// splitSentence splits the sentences
// by only the words and non-words correctly
func splitSentence(source string) []string {
// first element "0" already in the slice
chunks := make([]int, 1, 32)
wasLetter := false
for i, rune := range source {
switch {
case isCyrillic(rune) && !wasLetter:
wasLetter = true
if i == 0 {
continue
}
chunks = append(chunks, i)
case !isCyrillic(rune) && wasLetter:
wasLetter = false
chunks = append(chunks, i)
default:
continue
}
}
// add last element
chunks = append(chunks, len(source))
res := make([]string, len(chunks)-1)
for i := 0; i < len(chunks)-1; i++ {
res[i] = source[chunks[i]:chunks[i+1]]
}
return res
}
// splitWord to stem and ending
func splitWord(word string) (string, string) {
endingLength := 2
lenOfWord := utf8.RuneCountInString(word)
if lenOfWord <= endingLength {
return word, ""
}
wordInRune := []rune(word)
return string(wordInRune[:lenOfWord-endingLength]),
string(wordInRune[lenOfWord-endingLength:])
}
// capitalize uppers only first letter of the string
func capitalize(in string) string {
firstLetter, size := utf8.DecodeRuneInString(in)
if firstLetter == utf8.RuneError {
return in
}
return string(unicode.ToUpper(firstLetter)) + in[size:]
}
// sliding window 3 size on word
func readLetters(in string) [][]rune {
if in == "" {
return [][]rune{make([]rune, 3)}
}
inRune := append([]rune(in), make([]rune, 2)...)
copy(inRune[1:], inRune[:])
inRune[0] = rune(0)
res := make([][]rune, 0, len(inRune)-2)
for i := 0; i < len(inRune)-2; i++ {
res = append(res, inRune[i:i+3])
}
return res
}
// SchemaPrinter prints schemas line by line
func SchemaPrinter(schemas map[string]*Schema) string {
res := strings.Builder{}
sortedKeys := make([]string, 0, len(schemas))
for k := range schemas {
sortedKeys = append(sortedKeys, k)
}
sort.Strings(sortedKeys)
for _, schemaName := range sortedKeys {
schema := schemas[schemaName]
num := 20 - utf8.RuneCountInString(schema.Name)
res.WriteString(
fmt.Sprintf(
"%s:%s%s\n",
schema.Name,
strings.Repeat(" ", num),
schema.Desc,
),
)
}
return res.String()
}
// Get pairs from rune slice
func getPairs(in []rune) (prev, curr, next string) {
if in[0] > rune(0) {
prev += string(in[0])
}
prev += string(in[1])
curr = string(in[1])
next += string(in[1])
if in[2] > rune(0) {
next += string(in[2])
}
return
}