/
main.go
183 lines (140 loc) · 3.22 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
178
179
180
181
182
183
// Copyright 2016 Mhd Sulhan <ms@kilabit.info>. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package main
import (
"flag"
"fmt"
"github.com/shuLhan/dsv"
"github.com/shuLhan/tabula"
"github.com/shuLhan/wvcgen/feature"
wvcgen "github.com/shuLhan/wvcgen/reader"
"github.com/shuLhan/wvcgen/revision"
"io"
"time"
)
var (
fInputDsv = "wvc2010_features.dsv"
)
func trace(s string) (string, time.Time) {
fmt.Println("START:", s)
return s, time.Now()
}
func un(s string, startTime time.Time) {
endTime := time.Now()
fmt.Println(" END:", s, "with elapsed time", endTime.Sub(startTime))
}
/*
initReader set configuration of generator.
*/
func initReader(reader *wvcgen.Reader) {
revision.SetDir(reader.RevisionDir)
revision.SetCleanDir(reader.RevisionCleanDir)
}
/*
InitReadWriter initialize reader and writer.
*/
func InitReadWriter(finput string) (reader *wvcgen.Reader,
writer *dsv.Writer) {
reader, e := wvcgen.NewReader(finput)
if e != nil {
panic(e)
}
writer, e = dsv.NewWriter(finput)
if e != nil {
panic(e)
}
initReader(reader)
return reader, writer
}
func runFeature(reader *wvcgen.Reader, ftrValues *tabula.Dataset,
md dsv.Metadata,
) {
defer un(trace(">>> computing feature " + md.Name))
ftr := feature.GetByName(md.Name)
// No feature name found, search the column name in
// input metadata.
if ftr == nil {
getAsInputColumn(reader, md.Name, ftrValues)
return
}
ds := reader.GetDataset().(tabula.DatasetInterface)
ftr.Compute(ds)
col := ftr.Interface().(*tabula.Column)
ftrValues.PushColumn(*col)
}
/*
computeFeatures will compute each feature listed in writer output metadata.
*/
func computeFeatures(reader *wvcgen.Reader, writer *dsv.Writer) (
ftrValues *tabula.Dataset,
) {
ftrValues = &tabula.Dataset{}
ftrValues.Init(tabula.DatasetModeColumns, nil, nil)
for _, md := range writer.OutputMetadata {
runFeature(reader, ftrValues, md)
}
return
}
/*
getAsInputColumn return feature values as in input column.
*/
func getAsInputColumn(reader *wvcgen.Reader, colName string,
ftrValues *tabula.Dataset,
) {
ds := reader.GetDataset().(tabula.DatasetInterface)
ftr := ds.GetColumnByName(colName)
if ftr == nil {
return
}
// Add column in input as feature
ftrValues.PushColumn(*ftr)
}
/*
Generate start computing the feature values which has been defined in
input file `finput`.
If `featureName` is not empty, it will be added to list of feature that will
be computed.
*/
func Generate(featureName, finput string) {
var ftrValues *tabula.Dataset
var e error
var n int
reader, writer := InitReadWriter(finput)
if featureName != "" {
ftrMd := dsv.Metadata{
Name: featureName,
}
writer.AddMetadata(ftrMd)
}
for {
n, e = dsv.Read(reader)
if n <= 0 {
break
}
ftrValues = computeFeatures(reader, writer)
_, ewrite := writer.WriteColumns(ftrValues.Columns, nil)
if ewrite != nil {
panic(e)
}
if e == io.EOF {
break
}
}
e = reader.Close()
if e != nil {
panic(e)
}
e = writer.Close()
if e != nil {
panic(e)
}
}
func main() {
flag.Parse()
if len(flag.Args()) >= 1 {
fInputDsv = flag.Arg(0)
}
defer un(trace(">>> Processing " + fInputDsv))
Generate("", fInputDsv)
}