-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmissingCourses.py
More file actions
84 lines (70 loc) · 2.75 KB
/
Copy pathmissingCourses.py
File metadata and controls
84 lines (70 loc) · 2.75 KB
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
import csv
from rapidfuzz import fuzz
def find_missing_courses(
file_a_path,
file_b_path,
output_missing_path,
output_matched_path,
course_threshold=90,
grade_threshold=95,
):
# File A (raw data)
with open(file_a_path, "r", encoding="utf-8") as f:
reader = csv.reader(f)
file_a_rows = list(reader)
# File B (truth)
with open(file_b_path, "r", encoding="utf-8") as f:
reader = csv.reader(f)
truth_rows = [row for row in reader if row and len(row) >= 1]
missing = []
matched = []
total = len(truth_rows)
for truth_row in truth_rows:
course_b = truth_row[0].strip().upper().replace(" ", " ")
grade_b = (
truth_row[1].strip().upper().replace(" ", "") if len(truth_row) > 1 else ""
)
match_found = False
for row in file_a_rows:
tokens = [
cell.strip().upper().replace(" ", "") for cell in row if cell.strip()
]
candidates = []
# 2–4 token combinations for course matching
for i in range(len(tokens)):
for j in range(i + 1, min(i + 4, len(tokens))): # 2–4 tokens
course_candidate = " ".join(tokens[i : j + 1])
candidates.append(course_candidate)
for candidate in candidates:
course_sim = fuzz.ratio(
course_b.replace(" ", ""), candidate.replace(" ", "")
)
if course_sim >= course_threshold:
for cell in row:
grade_candidate = cell.strip().upper().replace(" ", "")
grade_sim = fuzz.ratio(grade_b, grade_candidate)
if grade_sim >= grade_threshold:
match_found = True
matched.append(truth_row)
break
if match_found:
break
if match_found:
break
if not match_found:
missing.append(truth_row)
with open(output_missing_path, "w", newline="", encoding="utf-8") as f:
writer = csv.writer(f)
writer.writerow(["Course", "Grade"])
writer.writerows(missing)
with open(output_matched_path, "w", newline="", encoding="utf-8") as f:
writer = csv.writer(f)
writer.writerow(["Course", "Grade"])
writer.writerows(matched)
matched = total - len(missing)
match_percent = (matched / total) * 100 if total > 0 else 0
print(f"Total courses in truth set: {total}")
print(f"Matched: {matched}")
print(f"Missing: {len(missing)}")
print(f"Match %: {match_percent:.2f}%")
print(f"Missing courses written to: {output_missing_path}")