/
tools.go
318 lines (257 loc) · 7.32 KB
/
tools.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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
package tools
import (
"math/rand"
"io/ioutil"
"strings"
"errors"
"regexp"
"bufio"
"bytes"
"time"
"fmt"
"os"
"io"
)
// Flush append data to file
func Flush(filePath string, str string) error {
fileHandle, err := os.OpenFile(filePath, os.O_APPEND|os.O_WRONLY, 0666)
if err != nil {
// If file doesn't exist he create him
if os.IsNotExist(err) {
fileHandle, err = os.Create(filePath)
if err != nil{
return err
}
} else{
return err
}
}
// Closing the file at the end of the function
defer fileHandle.Close()
// Flush data to file
fileHandle.WriteString(str)
return nil
}
// FileContains check that string exist in file
func FileContains(filePath string, str string) (bool, error) {
fileHandle, err := os.Open(filePath)
if err != nil {
return false, err
}
// Closing the file at the end of the function
defer fileHandle.Close()
// Making loop for file lines
fileHandleScan := bufio.NewScanner(fileHandle)
for fileHandleScan.Scan() {
if strings.Contains(fileHandleScan.Text(), str) {
return true, nil
}
}
return false, nil
}
// FileLinesCounter counts lines of file
func FileLinesCounter(filePath string) (int, error) {
fileHandle, err := os.Open(filePath)
if err != nil {
return 0, err
}
// Closing the file at the end of the function
defer fileHandle.Close()
buf := make([]byte, 1024)
fileLines := 1
for {
readBytes, err := fileHandle.Read(buf)
if err != nil {
if readBytes == 0 && err == io.EOF {
break
}
return 0, err
}
fileLines += bytes.Count(buf[:readBytes], []byte{'\n'})
}
return fileLines, nil
}
// SpecifyLineByNumber return specific text of line in file
func SpecifyLineByNumber(filePath string, specifyLine int) (string, error) {
fileHandle, err := os.Open(filePath)
if err != nil {
return "", err
}
// Closing the file at the end of the function
defer fileHandle.Close()
// Preparing variable for result
var lines int
// Making loop for file lines
fileHandleScan := bufio.NewScanner(fileHandle)
for fileHandleScan.Scan() {
if lines <= specifyLine{
// Return result with removed all whitespaces and suffixes
return fmt.Sprint(strings.TrimSpace(fileHandleScan.Text())), nil
}
lines++
}
return "", errors.New("The line cannot be found")
}
// SpecifyLineByText return specific text of line in file
func SpecifyLineByText(filePath string, str string) (string, error) {
fileHandle, err := os.Open(filePath)
if err != nil {
return "", err
}
// Closing the file at the end of the function
defer fileHandle.Close()
// Making loop for file lines
fileHandleScan := bufio.NewScanner(fileHandle)
for fileHandleScan.Scan() {
// Return result with removed all whitespaces and suffixes
if strings.Contains(fileHandleScan.Text(), str) {
return fmt.Sprint(strings.TrimSpace(fileHandleScan.Text())), nil
}
}
return "", errors.New("The line cannot be found")
}
// Synonym return combine text from file
func FileMix(filePath string, start string, end string, betweenWords string) (string, error) {
fileHandle, err := ioutil.ReadFile(filePath)
if err != nil {
return "", err
}
// Convert bytes to strings
byteToString := string(fileHandle)
// Make loop for choose synonyms
for {
// Too fast, too furious
time.Sleep(time.Microsecond * 100)
// Getting words between brackets
synonym, done, err := GetStringInBetween(byteToString, start, end)
if done{
break
}
if err != nil{
return "", err
}
// Split and add make list
synonymSplited := strings.Split(synonym, betweenWords)
// Choose synonym
byteToString = strings.Replace(byteToString, fmt.Sprint(start, synonym, end), synonymSplited[Random(0, cap(synonymSplited)-1)], 1)
}
return byteToString, nil
}
// Synonym return combine text from string
func StringMix(text string, start string, end string, betweenWords string) (string, error){
for {
// Too fast, too furious
time.Sleep(time.Microsecond * 100)
// Getting words between brackets
synonym, done, err := GetStringInBetween(text, start, end)
if done{
break
}
if err != nil{
return "", err
}
// Split and add make list
synonymSplited := strings.Split(synonym, betweenWords)
// Choose synonym
text = strings.Replace(text, fmt.Sprint(start, synonym, end), synonymSplited[Random(0, cap(synonymSplited)-1)], 1)
}
return text, nil
}
// FileMixRegExp return combine text from file
func FileMixRegExp(filePath string, start string, end string, betweenWords string) (string, error) {
fileHandle, err := ioutil.ReadFile(filePath)
if err != nil {
return "", err
}
// Convert bytes to strings
byteToString := string(fileHandle)
pattern := fmt.Sprintf("%s(.*?)%s", start, end)
re := regexp.MustCompile(pattern)
results := re.FindAllString(byteToString, -1)
for _, result := range results {
result = result[1:len(result)-1]
synonymSplited := strings.Split(result, betweenWords)
byteToString = strings.Replace(byteToString, fmt.Sprint(start, result, end), synonymSplited[Random(0, cap(synonymSplited)-1)], 1)
}
return byteToString, nil
}
// StringMixWithRegExp works better with
func StringMixWithRegExp(text string, start string, end string, betweenWords string) string{
pattern := fmt.Sprintf("%s(.*?)%s", start, end)
re := regexp.MustCompile(pattern)
results := re.FindAllString(text, -1)
for _, result := range results {
result = result[1:len(result)-1]
synonymSplited := strings.Split(result, betweenWords)
text = strings.Replace(text, fmt.Sprint(start, result, end), synonymSplited[Random(0, cap(synonymSplited)-1)], 1)
}
return text
}
// Exists check a file exists
func Exists(filePath string) (bool, error) {
_, err := os.Stat(filePath)
if err != nil{
// Not exist
if os.IsNotExist(err) {
return false, nil
}
return false, err
}
// Exist
return true, nil
}
// GetStringInBetween return string between two characters
func GetStringInBetween(str string, homeChar string, endChar string) (string, bool, error) {
home := strings.Index(str, homeChar)
if home == -1 {
return "", true, errors.New("No strings to find")
}
home += len(homeChar)
end := strings.Index(str, endChar)
if home > end{
return "", false, errors.New("Syntax error with mixing text")
}
return str[home:end], false, nil
}
// Random return random numbers in rage
func Random(min int, max int) int {
rand.Seed(time.Now().UnixNano())
return rand.Intn(max-min) + min
}
// RandomFileLine return random line in file
func RandomFileLine(filePath string) (string, error){
fileHandle, err := os.Open(filePath)
if err != nil {
return "", err
}
// Closing the file at the end of the function
defer fileHandle.Close()
buf := make([]byte, 1024)
fileLines := 0
for {
readBytes, err := fileHandle.Read(buf)
if err != nil {
if readBytes == 0 && err == io.EOF {
break
}
return "", err
}
fileLines += bytes.Count(buf[:readBytes], []byte{'\n'})
}
randomLine := Random(0, fileLines)
actualLine := 0
// Making loop for file fileLines
fileHandle.Seek(0, io.SeekStart)
fileHandleScan := bufio.NewScanner(fileHandle)
for fileHandleScan.Scan() {
if actualLine >= randomLine {
// Return result with removed all whitespaces and suffixes
if fileLines == 0 && fileHandleScan.Text() == "" {
return "", errors.New("File is empty")
}
return strings.TrimSpace(fileHandleScan.Text()), nil
}
actualLine++
}
return "", errors.New("Func had problem with get random line")
}