forked from wboag/SPlanner
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsplanner.rkt
322 lines (272 loc) · 11.8 KB
/
splanner.rkt
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
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; CourseGrid:splaner.rkt ;;
;; ;;
;; Purpose: Main application. ;;
;; ;;
;; Author: Willie Boag [email protected] ;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
#lang racket
(require "course.rkt")
(require "scraper.rkt")
(require "annotations.rkt")
; List of loaded courses
(define courses '())
; test whether two strings represent courses from the same department
; ex. (same-department? "18.01" "18.63A") --> true)
; ex. (same-department? "18.01" "6.046") --> false)
(define (same-department? num1 num2)
(let
((d1 (regexp-match #rx"[\\.]+\\." (string-upcase num1)))
(d2 (regexp-match #rx"[\\.]+\\." (string-upcase num2))))
(equal? d1 d2)))
; convert course id string into two-decimal numeric value
; ex. "18.013A" -> "18.01"
(define (numeric-value course-id)
(let
((num (regexp-match #rx"[^\\.]+\\.[0-9][0-9]+"
(string-upcase course-id)))
(goofy (regexp-match #rx"[^\\.]+\\.S[0-9][0-9]"
(string-upcase course-id))))
(cond
; Heuristic: Assume xx.Sxx is largest course number in dept.
(goofy (+ (string->number (car (regexp-match #rx"[0-9]|" course-id)))
.999))
(num (string->number (car num)))
(else (error "Could not extract course number")))))
; Get list of prereqs (strings) from a course object
(define (course->prereqs-list course)
; already done?
(if (hand-annotated? course)
; if available: lookup of hand-annotated preqs
(get-annotations course)
; else: attempt automatic prereq extraction
(let
((course-id (course-number course))
(descr (course->prereqs-text course)))
(if (symbol? descr)
; error getting prereqs?
'()
; prereqs text found; extract data
(let
((course-no (numeric-value course-id))
(numbers (regexp-match* #rx"[\\.]+\\.[0-9]+[A-Z]*" descr)))
(let
((filtered (filter (lambda (n)
; Heuristic: Only consider inter-department
(and (same-department? course-id n)
(< (numeric-value n) course-no)))
numbers)))
; OR relationship?
(if (regexp-match "(?i: or )" descr)
(if (not (empty? filtered))
(list filtered) ; if OR occurs in text, apply OR to all
'())
(map list filtered)
)))))))
; get a course object with a given course number
(define (get-course-from-number num)
; First try: determine if course data has already been fetched
(let ((matches (filter (lambda (c) (equal? (course-number c)
(string-upcase num)))
courses)))
; if course data already fetched?
(if (not (empty? matches))
(car matches)
; else: retrieve courses for given department and re-check
(begin
; retrieve all deprtment courses
(let ((dept (number->department num)))
(if (symbol? dept)
dept
(let ((dept-courses (department->courses dept)))
(set! courses (append dept-courses courses))
; Second try: determine if course data is available
(let
((second-matches (filter (lambda (c)
(equal? (course-number c)
(string-upcase num)))
courses)))
; if course data already fetched?
(if (not (empty? second-matches))
(car second-matches)
false)))))))))
; ANNOTATED
; mathematics
; electrical-engineering-and-computer-science
#|
; list of undergraduate courses (as course objects)
;(define dept "electrical-engineering-and-computer-science")
(define dept "linguistics-and-philosophy")
(define all-courses (department->courses dept))
;(displayln all-courses)
(cond ((not (equal? all-courses 'BAD-QUERY))
(set! courses (filter (lambda (c)
(equal? "Undergraduate" (course-level c)))
all-courses))
;(set! courses (filter (lambda (c)
; (equal? "6.837" (course-number c)))
; courses))
; list of all of the official course numbers
;(define course-numbers (map course-number courses))
; list of prerequisite descrptions
;(define prereq-texts (map course->prereqs-text courses))
; list of (list of prerequisites for each course)
;(define prereq-lists (map course->prereqs-list courses))
#|
; display data
(for-each (lambda (c t)
(displayln (course-name c))
(displayln (course-number c))
(displayln (course-url c))
(displayln t)
(displayln "----------"))
courses prereq-texts)
|#
#|
(for-each (lambda (c t p)
(if (equal? p p)
(begin
(displayln (course-name c))
(displayln (course-number c))
(displayln (course-url c))
(displayln t)
(displayln p)
(displayln "----------")
)
void))
courses prereq-texts prereq-lists)
|#
void))
|#
; map course department number to department name
(define depts (make-hash))
(hash-set! depts "4" "architecture")
(hash-set! depts "MAS" "media-arts-and-sciences") ; FIXME: generalize
(hash-set! depts "11" "urban-studies-and-planning")
(hash-set! depts "16" "aeronautics-and-astronautics")
(hash-set! depts "20" "biological-engineering")
(hash-set! depts "10" "chemical-engineering")
(hash-set! depts "1" "civil-and-environmental-engineering")
(hash-set! depts "6" "electrical-engineering-and-computer-science")
(hash-set! depts "ESD" "engineering-systems-division") ; FIXME: generalize
(hash-set! depts "HST" "health-sciences-and-technology") ; FIXME: generalie
(hash-set! depts "3" "materials-science-and-engineering")
(hash-set! depts "2" "mechanical-engineering")
(hash-set! depts "22" "nuclear-engineering")
(hash-set! depts "21A" "anthropology") ; FIXME: generalize
(hash-set! depts "CMS" "comparative-media-studies") ; FIXME: generalize
(hash-set! depts "14" "economics")
(hash-set! depts "21F" "foreign-languages-and-literatures") ; FIXME: generalize
(hash-set! depts "21H" "history") ; FIXME: generalize
(hash-set! depts "24" "linguistics-and-philosophy")
(hash-set! depts "21L" "literature") ; FIXME: generalize
(hash-set! depts "21M" "music-and-theater-arts") ; FIXME: generalize
(hash-set! depts "17" "political-science")
(hash-set! depts "STS" "science-technology-and-society") ; FIXME: generalize
(hash-set! depts "21W" "writing-and-humanistic-studies") ; FIXME: generalize
(hash-set! depts "WGS" "womens-and-gender-studies") ; FIXME: generalize
(hash-set! depts "7" "biology")
(hash-set! depts "9" "brain-and-cognitive-sciences")
(hash-set! depts "5" "chemistry")
(hash-set! depts "12" "earth-atmospheric-and-planetary-sciences")
(hash-set! depts "18" "mathematics")
(hash-set! depts "8" "physics")
(hash-set! depts "15" "sloan-school-of-management")
(hash-set! depts "PE" "athletics-physical-education-and-recreation") ; FIXME: generalize
(hash-set! depts "CC" "concourse") ; FIXME: generalize
(hash-set! depts "ES" "experimental-study-group") ; FIXME: generalize
(hash-set! depts "SP" "special-programs")
(define (number->department course-no)
(let
((dept-no (regexp-match #rx"[^\\.]+" (string-upcase course-no))))
;(displayln dept-no)
(if (empty? dept-no)
'BAD-INPUT
(hash-ref depts (string-upcase (car dept-no)) 'UNRECOGNIZED-DEPT))))
(set! courses (department->courses "mathematics"))
; REPL
; TODO: Add lots of different commands
; ex. (get class from number, lookup number from keywords, etc)
(define (repl)
(displayln "What action would you like to select? ")
(let
((input (read-line)))
(cond
; done
((or (equal? input eof) (equal? input "exit"))
true)
; help
((equal? input "help")
(begin
(displayln "Actions:")
(displayln " lookup - find course with given course number (ex. 18.02)")
(displayln " exit - close SPlanner")
(displayln " keywords - find courses about particular keyword")
(displayln " load - load data for a given department")
(newline)
(repl)))
; load department data
((equal? input "load")
(begin
(displayln "From which department would you like to load data?")
(let ((dept (read-line)))
; FIXME - dont reload courses
(set! courses (append courses (department->courses dept)))
(newline)
(repl))))
; keyword search
((equal? input "keywords")
(begin
(displayln "Please enter a set of keywords to search")
(let* ((keys (string-split (string-downcase (read-line))))
(relevant (filter (lambda (c)
(foldl (lambda (a b) (or a b))
#f
(map (lambda (word)
(member word keys))
(string-split
(string-downcase
(course-name
c))))))
courses)))
(displayln (list "keys" keys))
(map (lambda (c)
(displayln (list (course-name c) (course-number c))))
relevant)
(newline)
(repl))))
; lookup from course number
((equal? input "lookup")
(begin
(displayln "What course would you like to know about? ")
(let* ((course-no (read-line))
(course (get-course-from-number course-no)))
(cond
((symbol? course)
(begin
(displayln (string-append "I'm sorry. "
"I don't recognize "
"that department "))))
((equal? course false)
(begin
(displayln (string-append "I'm sorry. "
"I don't recognize "
"course "
input))))
(else
(begin
(displayln "You chose")
(displayln (course-name course))
(display "prerequisites: ")
(displayln (course->prereqs-list course)))))
(newline)
(repl))))
(else
(displayln (string-append "unrecognized command "
input))
(newline)
(repl)))
))
(define ans (repl))
(displayln "Happy learning!")
; MAJOR TODO - allow it to fetch course from number