forked from suggest-go/suggest
/
config.go
112 lines (89 loc) · 2.81 KB
/
config.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
package suggest
import (
"encoding/json"
"fmt"
"io/ioutil"
"os"
"path"
"github.com/teng231/suggest/pkg/analysis"
"github.com/teng231/suggest/pkg/index"
)
// Driver represents storage type of an inverted index
type Driver string
const (
// RAMDriver means that an inverted index is stored in RAM
RAMDriver Driver = "RAM"
// DiscDriver means that an inverted index is stored on FS and was indexed before
DiscDriver Driver = "DISC"
)
// IndexDescription is config for NgramIndex structure
type IndexDescription struct {
Driver Driver `json:"driver"`
Name string `json:"name"`
NGramSize int `json:"nGramSize"`
SourcePath string `json:"source"`
OutputPath string `json:"output"`
Alphabet []string `json:"alphabet"`
Pad string `json:"pad"`
Wrap [2]string `json:"wrap"`
basePath string
}
// GetDictionaryFile returns a path to a dictionary file from the configuration
func (d *IndexDescription) GetDictionaryFile() string {
return fmt.Sprintf("%s/%s.cdb", d.GetIndexPath(), d.Name)
}
// GetIndexPath returns a output path of the built index
func (d *IndexDescription) GetIndexPath() string {
if !path.IsAbs(d.OutputPath) {
return fmt.Sprintf("%s/%s", d.basePath, d.OutputPath)
}
return d.OutputPath
}
// GetSourcePath returns a source path of the index description
func (d *IndexDescription) GetSourcePath() string {
if !path.IsAbs(d.SourcePath) {
return fmt.Sprintf("%s/%s", d.basePath, d.SourcePath)
}
return d.SourcePath
}
// GetWriterConfig creates and returns IndexWriter config from the given index description
func (d *IndexDescription) GetWriterConfig() index.WriterConfig {
return index.WriterConfig{
HeaderFileName: d.getHeaderFile(),
DocumentListFileName: d.getDocumentListFile(),
}
}
// GetIndexTokenizer returns a tokenizer for indexing
func (d *IndexDescription) GetIndexTokenizer() analysis.Tokenizer {
return NewSuggestTokenizer(*d)
}
// getHeaderFile returns a path to a header file from the configuration
func (d *IndexDescription) getHeaderFile() string {
return fmt.Sprintf("%s.hd", d.Name)
}
// getDocumentListFile returns a path to a document list file from the configuration
func (d *IndexDescription) getDocumentListFile() string {
return fmt.Sprintf("%s.dl", d.Name)
}
// ReadConfigs reads and returns a list of IndexDescription from the given reader
func ReadConfigs(configPath string) ([]IndexDescription, error) {
configFile, err := os.Open(configPath)
if err != nil {
return nil, err
}
defer configFile.Close()
var configs []IndexDescription
data, err := ioutil.ReadAll(configFile)
if err != nil {
return nil, err
}
if err := json.Unmarshal(data, &configs); err != nil {
return nil, err
}
basePath := path.Dir(configPath)
for i, c := range configs {
c.basePath = basePath
configs[i] = c
}
return configs, nil
}