Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
88 changes: 35 additions & 53 deletions parser/gradeLoader.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import (
"strings"
)

var grades = []string{"A+", "A", "A-", "B+", "B", "B-", "C+", "C", "C-", "D+", "D", "D-", "F", "W", "P", "CR", "NC", "I"}

func loadGrades(csvDir string) map[string]map[string][]int {

// MAP[SEMESTER] -> MAP[SUBJECT + NUMBER + SECTION] -> GRADE DISTRIBUTION
Expand Down Expand Up @@ -73,68 +75,48 @@ func csvToMap(csvFile *os.File, logFile *os.File) map[string][]int {
if err != nil {
log.Panicf("Error parsing %s: %s", csvFile.Name(), err.Error())
}
// look for the subject column and w column
subjectCol := -1
catalogNumberCol := -1
sectionCol := -1
wCol := -1
aPlusCol := -1

headerRow := records[0]

for j := 0; j < len(headerRow); j++ {
switch {
case headerRow[j] == "Subject":
subjectCol = j
case headerRow[j] == "Catalog Number" || headerRow[j] == "Catalog Nbr":
catalogNumberCol = j
case headerRow[j] == "Section":
sectionCol = j
case headerRow[j] == "W" || headerRow[j] == "Total W" || headerRow[j] == "W Total":
wCol = j
case headerRow[j] == "A+":
aPlusCol = j
}
if wCol == -1 || subjectCol == -1 || catalogNumberCol == -1 || sectionCol == -1 || aPlusCol == -1 {
continue
} else {
break

indexMap := make(map[string]int)

for j, col := range records[0] {
switch col {
case "Catalog Number", "Catalog Nbr":
indexMap["Catalog Number"] = j
case "W", "Total W", "W Total":
indexMap["W"] = j
default:
indexMap[col] = j
}
}

if wCol == -1 {
logFile.WriteString("could not find W column")
//log.Panicf("could not find W column")
}
if sectionCol == -1 {
logFile.WriteString("could not find Section column")
log.Panicf("could not find Section column")
}
if subjectCol == -1 {
logFile.WriteString("could not find Subject column")
log.Panicf("could not find Subject column")
}
if catalogNumberCol == -1 {
logFile.WriteString("could not find catalog # column")
log.Panicf("could not find catalog # column")
// required columns
for _, name := range []string{"Section", "Subject", "Catalog Number", "A+"} {
if _, ok := indexMap[name]; !ok {
logFile.WriteString(fmt.Sprintf("could not find %s column", name))
log.Panicf("could not find %s column", name)
}
}
if aPlusCol == -1 {
logFile.WriteString("could not find A+ column")
log.Panicf("could not find A+ column")

// optional columns
for _, name := range []string{"W", "P", "CR", "NC", "I"} {
if _, ok := indexMap[name]; !ok {
logFile.WriteString(fmt.Sprintf("could not find %s column\n", name))
}
}

sectionCol := indexMap["Section"]
subjectCol := indexMap["Subject"]
catalogNumberCol := indexMap["Catalog Number"]

distroMap := make(map[string][]int)

for _, record := range records {
for _, record := range records[1:] {
// convert grade distribution from string to int
intSlice := [14]int{}

for j := 0; j < 13; j++ {
intSlice[j], _ = strconv.Atoi(record[aPlusCol+j])
}
// add w number to the grade_distribution slice
if wCol != -1 {
intSlice[13], _ = strconv.Atoi(record[wCol])
intSlice := make([]int, len(grades))
for i, col := range grades {
if pos, ok := indexMap[col]; ok {
intSlice[i], _ = strconv.Atoi(record[pos])
}
}

// add new grade distribution to map, keyed by SUBJECT + NUMBER + SECTION
Expand Down