-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig.go
53 lines (44 loc) · 1.31 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
package bag
import "fmt"
const (
// DefaultNGramSize is set to 3 (trigram)
DefaultNGramSize = 3
// DefaultNGramType is set to word
DefaultNGramType = "word"
// DefaultSmoothingParameter is set to 1 (common Laplace smoothing value)
DefaultSmoothingParameter = 1
)
type Config struct {
// NGramSize represents the NGram size (unigram, bigram, trigram, etc - default is trigram)
NGramSize int `yaml:"ngram-size"`
// NGramType represents the NGram type (word or character - default is word)
NGramType string `yaml:"ngram-type"`
// SmoothingParameter represents the smoothing value used for the Laplace Smoothing (default is 1)
SmoothingParameter float64 `yaml:"smoothing-parameter"`
}
func (c *Config) Validate() (err error) {
c.fill()
// Check to see if n-gram type is supported
switch c.NGramType {
case "word":
case "character":
case "":
default:
return fmt.Errorf("invalid ngram-type, <%s> is not supported", c.NGramType)
}
return
}
func (c *Config) fill() {
if c.NGramSize == 0 {
// NGramSize doesn't exist, set to default
c.NGramSize = DefaultNGramSize
}
if c.SmoothingParameter == 0 {
// SmoothingParameter doesn't exist, set to default
c.SmoothingParameter = DefaultSmoothingParameter
}
if c.NGramType == "" {
// NGramType doesn't exist, set to default
c.NGramType = DefaultNGramType
}
}