-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprogram.go
267 lines (229 loc) · 4.7 KB
/
program.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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
package main
import (
"bufio"
"errors"
"fmt"
"log"
"os"
"path/filepath"
"regexp"
"strings"
)
type label struct {
value string
emn string
}
var labelMap = map[string]label{
"Toka Nurslurgers": {
"",
"",
},
"Toka Yakias": {
"",
"",
},
"Toka Djaigi": {
"",
"",
},
"Toka Syimbawk": {
"",
"",
},
"Toka Katisu": {
"",
"",
},
"Toka Byiogo": {
"",
"",
},
"Toka Muuoki": {
"",
"",
},
"Toka Qiiburs": {
"",
"",
},
"Toka Vosoks": {
"",
"",
},
"Toka Iok": {
"",
"",
},
"Toka Tarrahic": {
"",
"",
},
"Toka Ufalnakoke": {
"",
"",
},
"Toka Ybarsk": {
"",
"",
},
"Toka Wawwapysuk": {
"",
"",
},
"Toka Simi Sammuq": {
"",
"",
},
"Toka Cataujar": {
"",
"",
},
"Riniawask": {
"",
"",
},
"Raicop": {
"",
"",
},
"Marra Vi 1": {
"",
"",
},
"Marra Vi 2": {
"",
"",
},
"GABAV": {
"",
"",
},
}
type tokas struct {
one string
two string
}
func main() {
file1 := os.Args[1]
file2 := os.Args[2]
fileOneLines := getLines(file1)
fileTwoLines := getLines(file2)
if len(fileOneLines) != len(fileTwoLines) {
errors.New("files do not contain an equal number of lines")
}
var outputLines []string
for index := range fileOneLines {
if index%2 != 0 {
for key := range labelMap {
fileOneLabel := fileOneLines[index][key]
fileTwoLabel := fileTwoLines[index][key]
if len(fileOneLabel.value) > 0 && len(fileTwoLabel.value) > 0 {
if strings.Contains(key, "Toka") {
p := tokas{fileOneLabel.value, fileTwoLabel.value}
pPrime := formatTokas(p)
fileOneLabel.value = pPrime.one
fileTwoLabel.value = pPrime.two
}
if fileOneLabel.value != fileTwoLabel.value {
fileOneValueOutput := fmt.Sprintf("%v, %v, %v, %v, %v", filepath.Base(file1), index, fileOneLabel.emn, key, fileOneLabel.value)
fileTwoValueOutput := fmt.Sprintf("%v, %v, %v, %v, %v", filepath.Base(file2), index, fileTwoLabel.emn, key, fileTwoLabel.value)
outputLines = append(outputLines, fileOneValueOutput)
outputLines = append(outputLines, fileTwoValueOutput)
}
}
}
}
}
writeFile(outputLines, file1, file2)
}
func getLines(filename string) []map[string]label {
file, err := os.Open(filename)
if err != nil {
log.Fatalf("failed opening file: %s", err)
}
scanner := bufio.NewScanner(file)
scanner.Split(bufio.ScanLines)
var textLines []string
for scanner.Scan() {
textLines = append(textLines, scanner.Text())
}
file.Close()
lineNumber := 1
var lineSlice []map[string]label
for _, line := range textLines {
fileLabelMap := make(map[string]label)
for key, value := range labelMap {
fileLabelMap[key] = value
}
for key := range fileLabelMap {
if key == "GABAV" {
fileLabelMap[key] = label{findLabelValues(line, fmt.Sprintf("[%s]", key), "|"), findLabelValues(line, "[EMN]", "[")}
} else if key == "Raicop" {
s := findLabelValues(line, fmt.Sprintf("[%s]", key), "[")
if len(s) > 0 {
s = string(s[0])
fileLabelMap[key] = label{s, findLabelValues(line, "[EMN]", "[")}
}
} else {
fileLabelMap[key] = label{findLabelValues(line, fmt.Sprintf("[%s]", key), "["), findLabelValues(line, "[EMN]", "[")}
}
}
lineSlice = append(lineSlice, fileLabelMap)
lineNumber += 1
}
return lineSlice
}
func findLabelValues(line string, left string, right string) string {
rx := regexp.MustCompile(`(?s)` + regexp.QuoteMeta(left) + `(.*?)` + regexp.QuoteMeta(right))
matches := rx.FindAllStringSubmatch(line, -1)
for _, v := range matches {
return v[1]
}
return ""
}
func writeFile(lines []string, file1 string, file2 string) {
filename1 := strings.Split(filepath.Base(file1), ".")[0]
filename2 := strings.Split(filepath.Base(file2), ".")[0]
err := os.MkdirAll("output", os.ModePerm)
if err != nil {
log.Fatalf("failed opening file: %s", err)
}
outputDirectory := "output"
outputFile := "diff-" + filename1 + "-" + filename2 + ".csv"
file, err := os.Create(filepath.Join(outputDirectory, filepath.Base(outputFile)))
if err != nil {
fmt.Println(err)
file.Close()
return
}
for _, value := range lines {
fmt.Fprintln(file, value)
if err != nil {
fmt.Println(err)
return
}
}
err = file.Close()
if err != nil {
fmt.Println(err)
return
}
fmt.Printf("%s written successfully\n", outputFile)
}
func formatTokas(p tokas) tokas {
s1 := strings.ReplaceAll(p.one, ",", "")
s2 := strings.ReplaceAll(p.two, ",", "")
s1a := strings.Split(s1, ";")
s2a := strings.Split(s2, ";")
for index := range s1a {
s1a[index] = s1a[index][1:]
s2a[index] = s2a[index][1:]
if s1a[index] == s2a[index] {
s1a[index] = "***"
s2a[index] = "***"
}
}
s1b := strings.Join(s1a, "|")
s2b := strings.Join(s2a, "|")
return tokas{s1b, s2b}
}