forked from advancedlogic/go-freeling
-
Notifications
You must be signed in to change notification settings - Fork 0
/
database.go
99 lines (86 loc) · 1.47 KB
/
database.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
package nlp
import (
cache "github.com/pmylund/go-cache"
"io/ioutil"
"strings"
)
const (
DB_MAP = iota
DB_PREFTREE
)
type Database struct {
DBType int
dbmap map[string]string
dbptree *cache.Cache
}
func NewDatabase(t int) *Database {
this := Database{
DBType: t,
}
if t == DB_MAP {
this.dbmap = make(map[string]string)
} else if t == DB_PREFTREE {
this.dbptree = cache.New(0, 0)
}
return &this
}
func NewDatabaseFromFile(dbFile string) *Database {
this := Database{
DBType: DB_MAP,
dbmap: make(map[string]string),
}
if dbFile != "" {
filestr, err := ioutil.ReadFile(dbFile)
if err != nil {
return nil
}
lines := strings.Split(string(filestr), "\n")
if lines[0] == "DB_PREFTREE" {
this.DBType = DB_PREFTREE
}
for i := 1; i < len(lines); i++ {
line := lines[i]
if line != "" {
pos := strings.Index(line, " ")
key := line[0:pos]
data := line[pos+1:]
this.addDatabase(key, data)
}
}
}
return &this
}
func (this *Database) addDatabase(key string, data string) {
if this.DBType == DB_MAP {
p := this.dbmap[key]
if p != "" {
this.dbmap[key] = p + " " + data
} else {
this.dbmap[key] = data
}
} else {
_, found := this.dbptree.Get(key)
if found {
}
}
}
func (this *Database) accessDatabase(key string) string {
switch this.DBType {
case DB_MAP:
{
p := this.dbmap[key]
if p != "" {
return p
}
break
}
case DB_PREFTREE:
{
//TODO
break
}
default:
break
}
return ""
}