-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
session.go
62 lines (53 loc) · 1.53 KB
/
session.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
package main
import (
"errors"
"os"
"path/filepath"
)
// newSession provides init configuration for the parsing session.
func newSession(
jsonPath, intentsPath, filterPath, outputPath, contentField string,
minWordLen, chunkSize int,
) (*config, error) {
if minWordLen < 0 {
return nil, errors.New("can't parse data with negative word count")
}
if chunkSize <= 0 {
return nil, errors.New("can't create CSV chunk file with zero (or negative) size")
}
if contentField == "" {
return nil, errors.New("can't parse data without content name attribute")
}
// Read the given file with intents.
intentsFile, err := os.ReadFile(filepath.Clean(intentsPath))
if err != nil {
return nil, err
}
// Read the given file with filter.
filterFile, err := os.ReadFile(filepath.Clean(filterPath))
if err != nil {
return nil, err
}
// Read the given folder for output CSV files.
_, err = os.Stat(filepath.Clean(outputPath))
if os.IsNotExist(err) {
// Create a new folder if is not existed.
if err = os.MkdirAll(filepath.Clean(outputPath), 0750); err != nil {
return nil, errors.New("can't create folder to output CSV file")
}
}
// Read the given folder with JSON data.
jsonFiles, err := os.ReadDir(filepath.Clean(jsonPath))
if err != nil {
return nil, err
}
return &config{
minWordLen: minWordLen,
chunkSize: chunkSize,
intentsFile: intentsFile,
filterFile: filterFile,
outputFolder: filepath.Clean(outputPath),
contentField: contentField,
jsonFolder: jsonFolder{path: jsonPath, files: jsonFiles},
}, nil
}