Skip to content

Commit 60161a3

Browse files
a23kumaralfredzimmer
authored andcommitted
feat: update api service to parse and return classroom
1 parent adeb731 commit 60161a3

File tree

2 files changed

+37
-0
lines changed

2 files changed

+37
-0
lines changed

flow/api/parse/schedule/schedule.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,16 +14,23 @@ type Summary struct {
1414
// Class numbers are four digits (e.g. 4895)
1515
// and uniquely identify a section of a course within a term.
1616
ClassNumbers []int
17+
// Classrooms identify the location of the class (e.g. DWE 3422, ONLN - Online)
18+
Classrooms []string
1719
}
1820

1921
var (
2022
termRegexp = regexp.MustCompile(`(Spring|Fall|Winter)\s+(\d{4})`)
23+
2124
// Class numbers are *the* four or five digit sequences
2225
// which occur on a separate line, perhaps parenthesized.
2326
// To be safe, we pre-emptively handle sequences up to length 8.
2427
// This should be fine since the only other numbers that appear
2528
// on their own line are the course code numbers (length 2 or 3).
2629
classNumberRegexp = regexp.MustCompile(`\n\(?(\d{4,8})\)?\n`)
30+
31+
// Matches room locations that appear on their own line
32+
// Building codes (alphanumeric with at least one letter) + space + room numbers, or TBA, or ONLN - Online
33+
classroomRegexp = regexp.MustCompile(`(?m)^([A-Z0-9]*[A-Z][A-Z0-9]*\s+\d+|TBA|ONLN - Online)$`)
2734
)
2835

2936
func extractTerm(text string) (int, error) {
@@ -56,6 +63,16 @@ func extractClassNumbers(text string) ([]int, error) {
5663
return classNumbers, nil
5764
}
5865

66+
func extractClassrooms(text string) ([]string, error) {
67+
submatches := classroomRegexp.FindAllStringSubmatchIndex(text, -1)
68+
classrooms := make([]string, len(submatches))
69+
for i, submatch := range submatches {
70+
matchText := text[submatch[2]:submatch[3]]
71+
classrooms[i] = matchText
72+
}
73+
return classrooms, nil
74+
}
75+
5976
func Parse(text string) (*Summary, error) {
6077
term, err := extractTerm(text)
6178
if err != nil {
@@ -65,9 +82,14 @@ func Parse(text string) (*Summary, error) {
6582
if err != nil {
6683
return nil, fmt.Errorf("extracting class numbers: %w", err)
6784
}
85+
classrooms, err := extractClassrooms(text)
86+
if err != nil {
87+
return nil, fmt.Errorf("extracting classrooms: %w", err)
88+
}
6889
summary := &Summary{
6990
TermId: term,
7091
ClassNumbers: classNumbers,
92+
Classrooms: classrooms,
7193
}
7294
return summary, nil
7395
}

flow/api/parse/schedule/schedule_test.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@ func TestParseSchedule(t *testing.T) {
2121
ClassNumbers: []int{
2222
4896, 4897, 4899, 4741, 4742, 5003, 4747, 4748, 7993, 7994, 7995, 4751, 4752,
2323
},
24+
Classrooms: []string{
25+
"MC 2038", "MC 4064", "DWE 2527", "E3 2119", "CPH 3681", "CPH 3681", "CPH 3681", "CPH 3681", "CPH 3681", "MC 2034", "CPH 3681", "CPH 1346", "CPH 3681", "CPH 3681",
26+
},
2427
},
2528
},
2629
// This schedule does not have parentheses around class numbers.
@@ -31,6 +34,9 @@ func TestParseSchedule(t *testing.T) {
3134
ClassNumbers: []int{
3235
5211, 8052, 9289, 6394, 5867, 6321, 6205, 7253, 7254,
3336
},
37+
Classrooms: []string{
38+
"E7 2317", "RCH 101", "MC 2034", "TBA", "MC 2017", "TBA", "AL 124", "DC 1351", "DC 1351",
39+
},
3440
},
3541
},
3642
// This schedule is old (carried over from Flow 1.0)
@@ -41,6 +47,9 @@ func TestParseSchedule(t *testing.T) {
4147
ClassNumbers: []int{
4248
3370, 3077, 3078, 3166, 2446, 4106, 4107, 4108, 4111, 4117, 4118, 4110,
4349
},
50+
Classrooms: []string{
51+
"MC 4040", "QNC 1502", "QNC 1502", "TBA", "STP 105", "RCH 307", "MC 2038", "MC 2038", "TBA", "TBA", "MC 2038", "TBA", "TBA", "TBA",
52+
},
4453
},
4554
},
4655
// This schedule has an abnormal amount of whitespace
@@ -51,6 +60,9 @@ func TestParseSchedule(t *testing.T) {
5160
ClassNumbers: []int{
5261
4669, 4658, 4660, 4699, 4655, 4656, 4661, 4662, 4850, 4664, 4666, 4936, 4639, 4668, 7634,
5362
},
63+
Classrooms: []string{
64+
"E5 3102", "E5 3102", "E5 3101", "E5 3101", "E5 3101", "E5 3101", "DWE 3518", "CPH 1346", "E5 3102", "E5 3101", "E5 3101", "MC 4063", "E5 3101", "E5 3102", "E5 3101", "E5 3101", "E3 3164", "E5 3101", "E5 3102", "MC 4060", "E2 2363", "E2 2363", "E2 2363", "E2 2363", "E2 2363", "E5 3101", "E5 3101", "E5 3101", "EV3 4412", "TBA",
65+
},
5466
},
5567
},
5668
// This schedule has class codes longer than 4 digits
@@ -61,6 +73,9 @@ func TestParseSchedule(t *testing.T) {
6173
ClassNumbers: []int{
6274
4262, 11810, 9336, 6336, 6367, 10692, 10310, 8204, 10376,
6375
},
76+
Classrooms: []string{
77+
"ONLN - Online", "ONLN - Online", "ONLN - Online", "ONLN - Online", "ONLN - Online", "ONLN - Online", "ONLN - Online", "ONLN - Online", "ONLN - Online",
78+
},
6479
},
6580
},
6681
}

0 commit comments

Comments
 (0)