-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
225 lines (188 loc) · 6.01 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
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
package main
import (
"encoding/csv"
"encoding/json"
"fmt"
"io/ioutil"
"log"
"os"
"sort"
"strings"
)
type Category struct {
ID string `json:"id"`
Name string `json:"name"`
}
type Subcategory struct {
ID string `json:"id"`
Name string `json:"name"`
Category Category `json:"category"`
}
func main() {
// Open and read the JSON file
file, err := os.Open("data.json")
if err != nil {
log.Fatalf("Failed to open file: %s", err)
}
defer file.Close()
// Read the file content into a byte slice
bytes, err := ioutil.ReadAll(file)
if err != nil {
log.Fatalf("Failed to read file: %s", err)
}
// Unmarshal the JSON data into a slice of Subcategory structs
var subcategories []Subcategory
err = json.Unmarshal(bytes, &subcategories)
if err != nil {
log.Fatalf("Failed to unmarshal JSON: %s", err)
}
writeGroupedOutput("grouped_output.txt", subcategories)
writeOriginalOutput("original_output.txt", subcategories)
writeCSVOutput("output.csv", subcategories)
writeCompactOutput("raw_output.txt", subcategories)
writeCompactGroupedOutput("grouped_output_compact.txt", subcategories)
fmt.Println("Data has been successfully written to grouped_output.txt, original_output.txt, raw_output.txt and output.csv")
}
func writeCompactOutput(fileName string, subcategories []Subcategory) {
file, err := os.Create(fileName)
if err != nil {
log.Fatalf("Failed to create output file: %s", err)
}
defer file.Close()
var outputParts []string
for i, subcategory := range subcategories {
part := fmt.Sprintf("main category #%d: %s subcategory: %s",
i+1, subcategory.Category.Name, subcategory.Name)
outputParts = append(outputParts, part)
}
output := strings.Join(outputParts, ", ")
_, err = file.WriteString(output)
if err != nil {
log.Fatalf("Failed to write to output file: %s", err)
}
}
func writeGroupedOutput(fileName string, subcategories []Subcategory) {
// Create and open a file to write the output
outputFile, err := os.Create(fileName)
if err != nil {
log.Fatalf("Failed to create output file: %s", err)
}
defer outputFile.Close()
// Group subcategories by main category
categoryMap := make(map[string][]Subcategory)
for _, subcategory := range subcategories {
categoryMap[subcategory.Category.Name] = append(categoryMap[subcategory.Category.Name], subcategory)
}
// Get sorted main category names
var mainCategories []string
for category := range categoryMap {
mainCategories = append(mainCategories, category)
}
sort.Strings(mainCategories)
// Write the grouped data to the file
var count int
for i, mainCategory := range mainCategories {
output := fmt.Sprintf("%d. Main Category: %s\n", i+1, mainCategory)
output += "Subcategories:\n"
for _, subcategory := range categoryMap[mainCategory] {
output += fmt.Sprintf("- %s\n", subcategory.Name)
count++
}
output += "\n Total Subcategories: " + fmt.Sprintf("%d\n\n", count)
count = 0
_, err := outputFile.WriteString(output)
if err != nil {
log.Fatalf("Failed to write to output file: %s", err)
}
}
}
func writeCompactGroupedOutput(fileName string, subcategories []Subcategory) {
file, err := os.Create(fileName)
if err != nil {
log.Fatalf("Failed to create output file: %s", err)
}
defer file.Close()
// Group subcategories by main category
categoryMap := make(map[string][]string)
for _, subcategory := range subcategories {
categoryMap[subcategory.Category.Name] = append(categoryMap[subcategory.Category.Name], subcategory.Name)
}
// Get sorted main category names
var mainCategories []string
for category := range categoryMap {
mainCategories = append(mainCategories, category)
}
sort.Strings(mainCategories)
var outputParts []string
for i, mainCategory := range mainCategories {
subcategoryNames := strings.Join(categoryMap[mainCategory], ", ")
part := fmt.Sprintf("main category #%d: %s subcategories: %s",
i+1, mainCategory, subcategoryNames)
outputParts = append(outputParts, part)
}
output := strings.Join(outputParts, ", ")
_, err = file.WriteString(output)
if err != nil {
log.Fatalf("Failed to write to output file: %s", err)
}
}
func writeOriginalOutput(fileName string, subcategories []Subcategory) {
// Create and open a file to write the output
outputFile, err := os.Create(fileName)
if err != nil {
log.Fatalf("Failed to create output file: %s", err)
}
defer outputFile.Close()
for i, subcategory := range subcategories {
output := fmt.Sprintf("Category #%d\n", i+1)
output += fmt.Sprintf("Main Category: %s\n", subcategory.Category.Name)
output += fmt.Sprintf("Subcategory: %s\n\n", subcategory.Name)
_, err := outputFile.WriteString(output)
if err != nil {
log.Fatalf("Failed to write to output file: %s", err)
}
}
}
func writeRawOutput(fileName string, subcategories []Subcategory) {
// Create and open a file to write the output
outputFile, err := os.Create(fileName)
if err != nil {
log.Fatalf("Failed to create output file: %s", err)
}
defer outputFile.Close()
for i, subcategory := range subcategories {
output := fmt.Sprintf("Category #%d\n", i+1)
output += fmt.Sprintf("Main Category: %s\n", subcategory.Category.Name)
output += fmt.Sprintf("Subcategory: %s\n\n", subcategory.Name)
_, err := outputFile.WriteString(output)
if err != nil {
log.Fatalf("Failed to write to output file: %s", err)
}
}
}
func writeCSVOutput(fileName string, subcategories []Subcategory) {
file, err := os.Create(fileName)
if err != nil {
log.Fatalf("Failed to create CSV file: %s", err)
}
defer file.Close()
writer := csv.NewWriter(file)
defer writer.Flush()
// Write header
header := []string{"Main Category ID", "Main Category Name", "Subcategory ID", "Subcategory Name"}
if err := writer.Write(header); err != nil {
log.Fatalf("Error writing CSV header: %s", err)
}
// Write data
for _, subcategory := range subcategories {
row := []string{
subcategory.Category.ID,
subcategory.Category.Name,
subcategory.ID,
subcategory.Name,
}
if err := writer.Write(row); err != nil {
log.Fatalf("Error writing CSV row: %s", err)
}
}
}