diff --git a/parser/gradeLoader.go b/parser/gradeLoader.go index aad3004..2d37f64 100644 --- a/parser/gradeLoader.go +++ b/parser/gradeLoader.go @@ -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 @@ -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