-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
177 lines (165 loc) · 3.51 KB
/
main.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
package main
import (
"bufio"
"fmt"
"io"
"log"
"os"
"strings"
translator "github.com/reagere/translate-verbs-dictionary"
_ "github.com/mattn/go-sqlite3"
)
func main() {
lang := os.Args[1]
file := os.Args[2]
db, err := translator.NewDB()
if err != nil {
log.Fatal(err)
}
defer db.Close()
sqlStmt := `
CREATE TABLE %s (id INTEGER PRIMARY KEY AUTOINCREMENT, verb_inf TEXT, verb TEXT, itr NUMERIC, tr NUMERIC, langues TEXT, flex TEXT, flexOpts TEXT, pers INTEGER, plur INTEGER, form TEXT, source TEXT);
CREATE INDEX idx_verb_%s ON %s (verb);
CREATE INDEX idx_form_%s ON %s (pers, plur, form);
`
_, err = db.Exec(fmt.Sprintf(sqlStmt, lang, lang, lang, lang, lang))
if err != nil {
log.Fatal(err)
}
f, err := os.Open(file)
if err != nil {
log.Fatal(err)
}
r := bufio.NewReader(f)
tx, err := db.Begin()
if err != nil {
log.Fatal(err)
}
stmt, err := tx.Prepare("INSERT INTO " + lang + " (verb_inf, verb, itr, tr, langues, flex, flexOpts, pers, plur, form, source) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)")
if err != nil {
log.Fatal(err)
}
defer stmt.Close()
for {
l, err := r.ReadString('\n')
if err == io.EOF {
break
}
if err != nil {
log.Fatal(err)
}
cols := strings.Split(l, ",")
itr := false
tr := false
langues := ""
flex := ""
flexOpts := []string{}
options := strings.Split(strings.Join(cols[2:], ","), "+")
pers := 0
plur := 0 // indefini, 1: singulier, 2: pluriel
form := ""
for i := range options {
opt := options[i]
switch opt {
case "ITR":
itr = true
case "TR":
tr = true
default:
if strings.HasPrefix(opt, "FR=") || strings.HasPrefix(opt, "QU=") {
if langues != "" {
langues += ","
}
opt = strings.Replace(opt, `"`, "", -1)
opt = strings.Trim(opt, ` `)
langues += opt
} else if strings.HasPrefix(opt, "FLX=") {
flex = opt[4:]
flexOpts = options[i+1:]
for _, flexOpt := range flexOpts {
switch flexOpt {
case "1":
pers = 1
case "2":
pers = 2
case "3":
pers = 3
case "s":
plur = 1
case "p":
plur = 2
case "pin":
plur = 3
case "pex":
plur = 4
// francais
case "PR":
form = flexOpt
case "F":
form = flexOpt
case "G":
form = flexOpt
case "C":
form = flexOpt
case "S":
form = flexOpt
case "IP":
form = flexOpt
case "I":
form = flexOpt
case "PP":
form = flexOpt
case "INF":
form = flexOpt
// quechua
case "FA":
form = flexOpt
case "FP":
form = flexOpt
case "GER":
form = flexOpt
case "GER2":
form = flexOpt
case "OBL":
form = flexOpt
case "PRES":
form = flexOpt
case "PASS":
form = flexOpt
case "PASS1":
form = flexOpt
case "PASS2":
form = flexOpt
case "PPA":
form = flexOpt
case "PPA2":
form = flexOpt
case "PAPT":
form = flexOpt
case "PPAT":
form = flexOpt
case "PPI":
form = flexOpt
case "RQUF":
form = flexOpt
case "RS1":
form = flexOpt
case "SUBI":
form = flexOpt
case "TI":
form = flexOpt
}
}
}
}
}
_, err = stmt.Exec(cols[1], cols[0], itr, tr, langues, flex, strings.Join(flexOpts, "+"), pers, plur, form, strings.Join(cols, ","))
if err != nil {
log.Fatal(err)
}
}
err = tx.Commit()
if err != nil {
log.Fatal(err)
}
}