-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy patharguments.go
165 lines (157 loc) · 3.92 KB
/
arguments.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
// Copyright 2018 Chris Pearce
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package main
import (
"fmt"
"os"
"strconv"
)
type arguments struct {
input string
output string
minSupport float64
minConfidence float64
minLift float64
itemsetsPath string
profile bool
}
const usage = `Arguments:
--input file_path Input dataset in CSV format.
--output file_path File path in which to store output rules. Format:
antecedent -> consequent, confidence, lift, support.
--min-support threshold
Minimum itemset support threshold, in range [0,1].
--min-confidence threshold
Minimum rule confidence threshold, in range [0,1].
--min-lift threshold Minimum rule lift confidence threshold, in range
[1,∞] (optional).
--itemsets file_path File path in which to store generated itemsets
(optional).
--profile Enables profiling via 'profile' package (optional).
`
func parseArgsOrDie() arguments {
result := arguments{}
args := os.Args[1:]
gotMinConf := false
gotMinSup := false
minSupErrMsg := "Expected --min-support argument followed by float in range [0,1.0]."
minConfErrMsg := "Expected --min-confidence argument followed by float in range [0,1.0]."
minLiftErrMsg := "Expected --min-lift argument followed by float in range [1.0,∞]."
if len(args) == 0 {
fmt.Print(usage)
os.Exit(-1)
}
for i := 0; i < len(args); i++ {
switch args[i] {
case "--input":
{
if i+1 > len(args) {
fmt.Println(
"Expected --input to be followed by input CSV path.",
)
os.Exit(-1)
}
result.input = args[i+1]
i++
}
case "--output":
{
if i+1 > len(args) {
fmt.Println(
"Expected --output to be followed by output rule path.",
)
os.Exit(-1)
}
result.output = args[i+1]
i++
}
case "--itemsets":
{
if i+1 > len(args) {
fmt.Println(
"Expected --itemsets to be followed by output itemsets path.",
)
os.Exit(-1)
}
result.itemsetsPath = args[i+1]
i++
}
case "--min-support":
{
if i+1 > len(args) {
fmt.Println(minSupErrMsg)
os.Exit(-1)
}
f, err := strconv.ParseFloat(args[i+1], 64)
if err != nil || f < 0.0 || f > 1.0 {
fmt.Println(minSupErrMsg)
os.Exit(-1)
}
result.minSupport = f
gotMinSup = true
i++
}
case "--min-confidence":
{
if i+1 > len(args) {
fmt.Println(minConfErrMsg)
os.Exit(-1)
}
f, err := strconv.ParseFloat(args[i+1], 64)
if err != nil || f < 0.0 || f > 1.0 {
fmt.Println(minConfErrMsg)
os.Exit(-1)
}
result.minConfidence = f
gotMinConf = true
i++
}
case "--min-lift":
{
if i+1 > len(args) {
fmt.Println(minLiftErrMsg)
os.Exit(-1)
}
f, err := strconv.ParseFloat(args[i+1], 64)
if err != nil || f < 1.0 {
fmt.Println(minLiftErrMsg)
os.Exit(-1)
}
result.minLift = f
i++
}
case "--profile":
{
result.profile = true
}
}
}
if len(result.input) == 0 {
fmt.Println("Missing required parameter '--input $csv_path")
os.Exit(-1)
}
if len(result.output) == 0 {
fmt.Println("Missing required parameter '--output $rule_path")
os.Exit(-1)
}
if !gotMinSup {
fmt.Println(minSupErrMsg)
os.Exit(-1)
}
if !gotMinConf {
fmt.Println(minConfErrMsg)
os.Exit(-1)
}
return result
}