from lark import Lark, Transformer, v_args import json, sys, re, codecs import lark """describe college courses, their number of units, any other courses that are prerequisites, as well as various degrees which consist of groups of courses that must be taken. The groups have rules associated with them, such as: - take all (the following courses) - take n (where n=3 or 5 or something) - take at least n units from the following list (so the taken course's unit value must add up to n or greater) - and so on.""" def num(s): if s == '': return 0 try: return int(s) except ValueError: return float(s) class CourseOr: def __init__(self, subs): self.names = subs self.name = " or ".join(self.names) def __repr__(self): return f"[{self.name}]" class Course: def __init__(self, name, units, prerequisites=[]): self.name = name self.units = units self.prerequisites = prerequisites def __repr__(self): #return f"{self.name} ({self.units} units)" return f"{self.name}" class CourseList: def __init__(self, name, course_list): self.name = name self.course_list = course_list self.l = len(course_list) def __iter__(self): return iter(self.course_list) def __len__(self): print("What is this len? ", len(self.course_list), str(list(self.course_list)), str(type(self.course_list))) i = 1 for c in self.course_list: i += 1 return i #return len(self.course_list) def __repr__(self): return ", ".join([f"\"{str(x)}\"" for x in self.course_list]) # .course_list #return f"{s}" #return f"CourseList({self.name}, {s})" class DegreeRule: def __init__(self, rule_type, course_list): self.rule_type = rule_type if 'n' in rule_type.__dict__: self.n = rule_type.n self.course_list = course_list print("COURSELIST: ", type(self.course_list), self.course_list) def mz_rule(self,n): s = ",".join( [ f"{x}" for x in self.course_list]) i = 0 for c in self.course_list: i += 1 #print("What is this len? ", len(self.course_list), str(list(self.course_list)), str(type(self.course_list))) t = len(list(self.course_list)) if not type(self.course_list)==tuple: t = self.course_list.l return f"array[1..{t}] of string: rule_{n}_courses = [{s}];" def __repr__(self): return self.mz_rule(1) #return f"DegreeRule({self.rule_type}, {self.course_list})" class Degree: def __init__(self, name, degree_rules): self.name = name self.degree_rules = degree_rules def mz_rule(self): n = 1 s = f"% Degree: {self.name}\n" for r in self.degree_rules: s += r.mz_rule(n) n += 1 return s def __repr__(self): #x = ", ".join([str(x) for x in self.degree_rules]) #return f"Degree({self.name}, {x})" return self.mz_rule() class RuleType(): def __init__(self,what,n=0): self.what = what self.n = n def __repr__(self): return "" #f"RuleType({self.what}, {self.n})" class Rule: pass class TakeAll(Rule): def __repr__(self): return f"Take all:" class TakeN(Rule): def __init__(self, n): self.n = n def __repr__(self): return f"Take {self.n} courses:" class TakeNUnits(Rule): def __init__(self, n): self.n = n def __repr__(self): return f"Take {self.n} units:" @v_args(inline=True) class DSLTransformer(Transformer): def __init__(self): self.courses = {} self.degrees = {} self.lists = {} def course_declaration(self, name, units): #return Course(name, int(units), prerequisites.children) #print(items) #if type(items[0]) == str: c = Course(name.value, units) self.courses[name.value] = c #else: #options = [x.value for x in items[0].children] #label = "[OR " + options[0] + "...]" #c = CourseOr(label, options) #self.courses[label] = c '''def program(self, name, rules): self.degrees[name.value] = Degree(name.value, rules) return self.degrees[name.value]''' def rule_type(self, items): what = items[0] n = None if len(items) == 2: n = int(items[1]) return RuleType(what, n) def take_all(self): return TakeAll() def take_n_courses(self, n): return TakeN(n) def take_n_units(self, n): return TakeNUnits(n) def degree_rule(self, rule_type, *course_list): #if type(course_list[0])==type([]): # return DegreeRule(rule_type, course_list[0]) print("making DegreeRule: ", rule_type, type(course_list), course_list) return DegreeRule(rule_type, course_list) def list_declaration(self, name, course_list): print("declared course list: ", name.value, course_list) cl = CourseList(name.value, course_list) self.lists[name.value] = cl return cl def list_ref(self, name): print("referenced course list: ", name.value, type(self.lists[name.value]), self.lists[name.value]) return self.lists[name.value] def course(self, *items): return Course(items[0].value,"") def course_or(self, *items): return CourseOr([x.value for x in items]) def course_list(self, *items): #print(str(items)) print("course list: ", items) return CourseList("temp", items) #return [c for c in items] def program(self, *items): #print(items) name = items[0].value rules = items[1:] self.degrees[name] = Degree(name, rules) return self.degrees[name] """ dsl = DSLTransformer() dsl.course('CS101', 3) dsl.course('CS102', 3, prerequisites=['CS101']) dsl.course('MATH101', 3) dsl.degree('CS', [ dsl.take_all(['CS101', 'CS102']), dsl.take_n_units(['CS101', 'CS102', 'MATH101'], 9), dsl.take_n(['CS101', 'CS102', 'MATH101'], 2), ]) print(dsl.courses) print(dsl.degrees) """ #(course ","?)* grammar = """ start: _spec+ _spec: program | course_declaration | list_declaration program: "program" PROGRAMNAME degree_rule* degree_rule: "take" rule_type [course_list | list_ref] list_ref: LISTNAME rule_type: "all from" -> take_all | "at least" INT "units from" -> take_n_units | "at least" INT "courses from" -> take_n_courses | "at least" INT "course from" -> take_n_courses list_declaration: "list" LISTNAME ":" course_list course_declaration: "course" COURSECODE UNITAMOUNT "units" ["prerequisites" (COURSECODE ","?)*] course_list: [course | course_or] ["," [course | course_or]]* course: COURSECODE course_or: COURSECODE "or" COURSECODE COURSECODE: ("A".."Z")+ INT+ ["A".."Z"]* PROGRAMNAME: ("A".."Z" | "a".."z")+ LISTNAME: ("a".."z"["A".."Z" | "a".."z" | INT]+) UNITAMOUNT: NUMUNITS | NUMUNITS "-" NUMUNITS NUMUNITS: INT | INT "." INT %import common.INT %import common.WS %ignore WS """ def parser(): dsl = """ course CSIS1 2 units course CSIS2 4 units course ACCT120 3 units course ACCT20 4 units course BUS1 3 units course ENGL250 3 units course ACCT105 3 units course ACCT121 3 units course BOT100 3 units course BOT105B 3 units list a1: CMUN1A, CMUN5, CMUN8, CMUN10 list a2: ENGL1A list a3: PHIL2, PHIL4, ENGL1C, CMUN3 list b1: ASTR1, CHEM1A, CHEM1B, CHEM12A, CHEM12B list b2: AH15, ANTH1, BIO1, BIO2, BIO4, BIO5 list b3: CHEM1A, ANTH1, BIO4, BIO5 list b4: BUS14, BUS11 or ECON11, ECON14, MATH1A, MATH1B list c1: BUS1, ENGL250, ACCT120, BOT100, ACCT121, BOT105B program BusinessAccountingOption take all from CSIS1, CSIS2 take at least 6 units from ACCT120, ACCT20 or ECON20 take at least 3 courses from MUS5, MUS10, BUS1, ENGL250, ACCT120, BOT100, ACCT121, BOT105B take at least 1 course from c1 program CSUGenEd take at least 1 course from a1 take at least 1 course from a2 take at least 1 course from a3 """ do_parse(dsl) def do_parse(dsl): parser = Lark(grammar) print(parser.parse(dsl).pretty()) print("\n\n\n") print("\nTRANSFORMER: ") parser = Lark(grammar) transformer = DSLTransformer() def parse_dsl(dsl): tree = parser.parse(dsl) return transformer.transform(tree) result = parse_dsl(dsl) print(transformer.courses) print() print(transformer.degrees) print() print(transformer.lists) print() [print(c) for c in transformer.courses] print() print("\nDEGREES: ") for d,i in transformer.degrees.items(): print(str(i)) print() [print(d) for d in transformer.lists] def rule_check_units(): # Define the course units and passing grades course_units = { "A": 1, "B": 2, "C": 1, "D": 3 } # Define the students and their passed courses students = { "Peter": ["A", "B", "C"], "Mark": ["D", "B"], "Susan": ["A", "B"] } # Define the minimum units required to graduate min_units_to_graduate = 4 calcs = {} for student, passed_courses in students.items(): print("\n", student, passed_courses) for c in passed_courses: print(f"\t{c:15} {course_units[c]}") print(f"\t{'Total':15} {sum(course_units[c] for c in passed_courses)}") # Determine the graduating students graduating_students = [ student for student, passed_courses in students.items() if sum(course_units[course] for course in passed_courses) >= min_units_to_graduate ] # Print the graduating students if graduating_students: print("The graduating students are:", ", ".join(graduating_students)) else: print("No students meet the graduation requirements.") """ def compute_degree_progress(student_courses, degree): total_units_completed = 0 total_required_units = 0 for course in degree['required_courses']: if course['id'] in student_courses: total_units_completed += course['units'] if 'additional_rules' in course: for rule in course['additional_rules']: if rule['type'] == 'at_least_n_units_from_list': units_from_list = sum([c['units'] for c in degree['courses'] if c['id'] in student_courses and c['id'] in rule['course_list']]) if units_from_list < rule['n']: break total_required_units += course['units'] progress_percent = total_units_completed / total_required_units * 100 if total_required_units > 0 else 0 return progress_percent """ word_to_num = {'course':1, 'One':1, 'one':1, 'two':2, 'three':3, 'four':4, 'five':5, 'six':6, 'seven':7, 'eight':8, 'nine':9, 'ten':10, 'eleven':11,} def word2num(word, verbose=0): word = word.lower() ret = word_to_num[word] if word in word_to_num else word if verbose: print(f" word2num({word}) -> {ret}") return ret lab_classes = {} def load_lab_classes(): global lab_classes if lab_classes: return lab_classes for c in json.loads(codecs.open('cache/courses/courses_built.json','r','utf-8').read()).values(): #print(c) if 'min_lab_hour' in c and float(c['min_lab_hour']) > 0: lab_classes[c['dept'] + c['number']] = 1 print(lab_classes) return lab_classes def is_lab_class(c): lab_classes = load_lab_classes() if c in lab_classes: return True return False def is_noncourse_new_section(noncourse_line): from degree_vars import note_true_section, note_false_section for non in note_false_section: if re.search(non, noncourse_line): #print(f"- {noncourse_line}") return False for yes in note_true_section: if re.search(yes, noncourse_line): #print(f"+ {noncourse_line}") return True print(f" -> should this start a new rule/section? [{noncourse_line}]") return False rule_lookup = { 'take_all_prereq': ['RN PROGRAM PREREQUISITES', 'PREREQUISITES', ], 'take at least n courses': ['(\d+) courses total', 'SELECT (ONE|TWO) OF THE FOLLOWING', 'Select (one|two|three)', 'Select (\d+) courses', 'Choose (one) or more', 'Choose (one|two|three) of the classes listed', 'Choose (\w+) of the following','Choose (one|two|three)', 'Choose ([\d\w]+) courses from', 'ANY (COURSE) NOT USED IN', 'Select (1)', 'Select (one) of the following REQUIRED CORE', '(One) of the following:', 'LIST [AB]: Select (\d)', 'Choose (One) Course:', ], 'take at least n units': ['LIST A \((\d+) units\)', 'LIST B \((\d+) units\)', 'LIST C \- Any course .*\((\d+) units\)', '(\d+) units total', 'Select (\d+) units', 'Any combination totaling (\d+) units', 'Choose (\w+) units from classes listed', 'Choose a minimum of ([\w\d]+) units from', 'Choose any combination of courses for a minimum of ([\w\d]+) units', 'Choose ([\w\d]+) units', 'Choose courses for at least ([\w\d]+) units', 'Choose a minimum of (\d+) units', 'Select any (\d+)\-\d+ units from the following'], 'electives': ['Electives', 'Recommended electives?:', ], 'take_all': ['RN PROGRAM', 'REQUIRED CORE', 'CORE COURSES', 'ADDITIONAL REQUIREMENTS','REQUIREMENTS:', 'Requirements', 'Core Requirements', 'Required Core', 'REQUIRED', 'LVN PROGRAM', 'Student Teaching Practicum', '^LIST A:?$', '^LIST B:$', 'Program Requirements', 'Required Courses:', 'PROGRAM REQUIREMENTS (5 Units)', 'PROGRAM REQUIREMENTS (162 Hours)', ], } def lookup_rule(line): verbose = 0 for key in rule_lookup.keys(): for each in rule_lookup[key]: m = re.search(each, line) if m: num = None try: if m.group(1): num = m.group(1) except Exception as e: pass if verbose: print(f"line: {line} matched: {each} with {num}") return key,num return None,None def examine(li,award, verbose=0): summary = [x[0] for x in li] if summary[1] in ['and','or']: if verbose: print(" - ", summary) def check_ands_ors_pbd(award, pbd, verbose=0): verbose = 0 if verbose: print(f"check_ands_ors_pbd({award}, ...)") summary = [x[0] for x in pbd] if verbose: print(" ", summary) # iterate through in groups of 3, from 0/1/2 up to 3/4/5 # (for length 6. length n: n-2, n-1, n) for i in range(len(pbd)-2): examine(pbd[i:i+3], award, verbose) if verbose: print() def build_catalog(): cfile = "cache/courses/courses_active_built.json" pfile = "cache/programs/programs_built.json" courses = json.loads(codecs.open(cfile,'r','utf-8').read()) programs = json.loads(codecs.open(pfile,'r','utf-8').read()) course_spec = [] for index,c in courses.items(): try: d = c['dept'] n = c['number'] name = c['name'] u2 = num_units(c['min_units']) if 'max_units' in c: u1 = num_units(c['max_units']) else: u1 = u2 if u1 == u2: units = {'units': u1} u = u1 else: units = {'min_units': u2, 'max_units': u1} u = f"{u2}-{u1}" course_spec.append(f"course {d}{n} {u} units") except Exception as e: pass #print(e) #print(json.dumps(c,indent=2)) d_out = codecs.open('cache/prog_debug.txt','w','utf-8') def d(s): d_out.write(str(s) + "\n") for index,p in programs.items(): v2 = 0 # print debugging stuff # Each award (degree or certificate) award = p['award'] + " " + p['program_title'] d("\n" + p['award'] + " " + p['program_title']) print("\n" + award) this_program = p['award'] + " " + p['program_title'] this_rule = "" r = p['requirements'] course_count = 0 # Each numbered chunk (k) in the requirements section for k in sorted(r.keys()): # Each 'program block definition' # 1st is dict with unit totals, rest are lists. check_ands_ors_pbd( award, sorted( r[k][1:], key=lambda x: float(x[1])) ) for each_r in sorted( r[k][1:], key=lambda x: float(x[1])): if each_r[0] in ['and','or']: #print(' ', each_r[0],each_r[1]) pass if isinstance(each_r, list): #print(each_r) if each_r[0] == 'h3' or (each_r[0]=='noncourse' and is_noncourse_new_section(each_r[2])): # This is a rule title if this_rule and course_count: d(this_rule) # + f" ({course_count}) " course_count = 0 raw_rule = each_r[2] good_rule, num = lookup_rule(raw_rule) if good_rule: #print(f"\t{good_rule}") n = word2num(num) if num else "" if v2: print(f"\tn = {n}") actual_rule = re.sub(r'\sn\s',f' {n} ',good_rule) if v2: print(f"\tactual rule is: {actual_rule}") #this_rule = f"{good_rule} ({n}) [{raw_rule}] from " this_rule = f"{actual_rule} from " else: #print(f"\t{raw_rule}") if not this_rule: this_rule = "take_all from " this_rule = " * " + raw_rule + " " + "from " #elif each_r[0] == 'noncourse': # also a rule title, some kind of sub-rule? # d( f" ++ (noncourse) {each_r[2]}") elif each_r[0] == 'course': # course in a rule if not this_rule: this_rule = "take_all from " is_lab = '[L]' if is_lab_class(each_r[2]['code']) else '' #this_rule += f"{each_r[2]['code']}{is_lab}({each_r[1]}), " this_rule += f"{each_r[2]['code']}{is_lab}, " if v2: print(f"\t\tthis rule is now: {this_rule}") course_count += 1 if course_count: d(this_rule) # + f" ({course_count})" d_out.close() d_in = codecs.open('cache/prog_debug.txt','r','utf-8').readlines() progs = [] this_prog = [] for line in d_in: if line.strip() == '': if this_prog: progs.append(this_prog) this_prog = [] else: this_prog.append(line.strip()) okay = [] notokay = [] for p in progs: ok =1 for line in p: if line[0] == '*': notokay.append(p) ok = 0 continue if ok: okay.append(p) print("These programs are okay:") for p in okay: for l in p: print(l) print() print("\n\nThese programs are not okay:") for p in notokay: for l in p: print(l) print() print(f"okay: {len(okay)}") print(f"not okay: {len(notokay)}") return do_parse('\n'.join(course_spec)) ## TODO or and and ## How to see this as a 'fake' course: Eligible for English 1A ? ## How to include this as part of a list: 'ANY COURSE NOT USED IN LIST A (3 units)' if __name__ == "__main__": options = { 1: ['parsing example',parser] , 2: ['rule check by units',rule_check_units] , 3: ['load all programs',build_catalog] , } print ('') if len(sys.argv) > 1 and re.search(r'^\d+',sys.argv[1]): resp = int(sys.argv[1]) print("\n\nPerforming: %s\n\n" % options[resp][0]) else: print ('') for key in options: print(str(key) + '.\t' + options[key][0]) print('') resp = input('Choose: ') # Call the function in the options dict options[ int(resp)][1]()