Spaces:
Sleeping
Sleeping
try to catch office hours
Browse files
app.py
CHANGED
|
@@ -48,13 +48,14 @@ def is_valid_date(date_str):
|
|
| 48 |
if not date_str or len(date_str) < 3:
|
| 49 |
return False
|
| 50 |
|
| 51 |
-
# Check for month names
|
| 52 |
months = ['january', 'february', 'march', 'april', 'may', 'june',
|
| 53 |
-
'july', 'august', 'september', 'october', 'november', 'december'
|
|
|
|
| 54 |
has_month = any(month in date_str.lower() for month in months)
|
| 55 |
|
| 56 |
-
# Or check for date pattern like 9/24, 10/15
|
| 57 |
-
has_date_pattern = re.search(r'\d{1,2}/\d{1,2}', date_str)
|
| 58 |
|
| 59 |
return has_month or has_date_pattern
|
| 60 |
|
|
@@ -67,6 +68,7 @@ def is_valid_time(time_str):
|
|
| 67 |
patterns = [
|
| 68 |
r'\d{1,2}:\d{2}\s*(?:AM|PM|am|pm)', # 3:30 PM
|
| 69 |
r'\d{1,2}\s*(?:AM|PM|am|pm)', # 3 PM
|
|
|
|
| 70 |
]
|
| 71 |
|
| 72 |
return any(re.search(p, time_str) for p in patterns)
|
|
@@ -78,7 +80,7 @@ def is_valid_assignment_code(code):
|
|
| 78 |
|
| 79 |
# Must start with letter(s) followed by number(s)
|
| 80 |
pattern = r'^[A-Z]+\d+(?:\s*-\s*[A-Z]+\d*)?$'
|
| 81 |
-
return bool(re.match(pattern, code.strip()))
|
| 82 |
|
| 83 |
# ---------------------------------------
|
| 84 |
# Improved LLM extraction
|
|
@@ -126,9 +128,9 @@ CSV:"""
|
|
| 126 |
time = parts[2]
|
| 127 |
location = parts[3] if len(parts) > 3 else ""
|
| 128 |
|
| 129 |
-
#
|
| 130 |
-
valid_events = ["class", "office hours", "ta office hours", "office hour"]
|
| 131 |
-
if event_type.lower()
|
| 132 |
logger.info(f"Skipping invalid event type: {event_type}")
|
| 133 |
continue
|
| 134 |
|
|
@@ -136,8 +138,8 @@ CSV:"""
|
|
| 136 |
logger.info(f"Skipping invalid time: {time}")
|
| 137 |
continue
|
| 138 |
|
| 139 |
-
# Days should be short
|
| 140 |
-
if len(days) >
|
| 141 |
logger.info(f"Skipping invalid days: {days}")
|
| 142 |
continue
|
| 143 |
|
|
@@ -169,7 +171,7 @@ def extract_deadlines_with_llm(text):
|
|
| 169 |
|
| 170 |
all_events = []
|
| 171 |
|
| 172 |
-
for i, chunk in enumerate(chunks[:
|
| 173 |
logger.info(f"Processing deadline chunk {i+1}")
|
| 174 |
|
| 175 |
prompt = f"""Extract assignment due dates and exam dates from this text.
|
|
@@ -205,12 +207,12 @@ CSV:"""
|
|
| 205 |
event_name = parts[0].strip()
|
| 206 |
date_time = parts[1].strip()
|
| 207 |
|
| 208 |
-
# Validation: event name should be short
|
| 209 |
if len(event_name) > 50:
|
| 210 |
logger.info(f"Skipping too-long event: {event_name[:30]}...")
|
| 211 |
continue
|
| 212 |
|
| 213 |
-
# Check if it's valid assignment code or contains
|
| 214 |
is_assignment = is_valid_assignment_code(event_name)
|
| 215 |
is_exam = "exam" in event_name.lower() or "midterm" in event_name.lower() or "final" in event_name.lower()
|
| 216 |
is_project = "project" in event_name.lower() or "presentation" in event_name.lower()
|
|
@@ -263,18 +265,18 @@ def regex_extract_schedule(text):
|
|
| 263 |
time = match.group(2)
|
| 264 |
|
| 265 |
# Find location
|
| 266 |
-
context = text_header[max(0, match.start()-200):match.end()+
|
| 267 |
location = ""
|
| 268 |
|
| 269 |
loc_patterns = [
|
| 270 |
r'Room[:\s]*([A-Za-z]+\s+\d{3,4})',
|
| 271 |
-
r'(?:Building|Hall)[:\s]*([^\n]
|
| 272 |
]
|
| 273 |
|
| 274 |
for pattern in loc_patterns:
|
| 275 |
loc_match = re.search(pattern, context, re.IGNORECASE)
|
| 276 |
if loc_match:
|
| 277 |
-
location = loc_match.group(1).strip()[:
|
| 278 |
break
|
| 279 |
|
| 280 |
events.append({
|
|
@@ -285,17 +287,17 @@ def regex_extract_schedule(text):
|
|
| 285 |
logger.info(f"Regex class: {days} {time} at {location}")
|
| 286 |
|
| 287 |
# Pattern 2: "T R, 2:00 PM – 3:20 PM"
|
| 288 |
-
pattern2 = r"(?:Class Schedule|Lecture)[:\s]*([MTWRFS]+(?:\s+[MTWRFS]+)*)[,\s]+(\d{1,2}:\d{2}\s*(?:AM|PM)\s*[–-]\s*\d{1,2}:\d{2}\s*(?:AM|PM))"
|
| 289 |
|
| 290 |
for match in re.finditer(pattern2, text_header, re.IGNORECASE):
|
| 291 |
days = match.group(1).replace(" ", "")
|
| 292 |
time = match.group(2)
|
| 293 |
|
| 294 |
-
context = text_header[match.end():match.end()+
|
| 295 |
location = ""
|
| 296 |
loc_match = re.search(r'In-person[:\s]*([^\n]+)', context, re.IGNORECASE)
|
| 297 |
if loc_match:
|
| 298 |
-
location = loc_match.group(1).strip()[:
|
| 299 |
|
| 300 |
events.append({
|
| 301 |
"Event": "Class",
|
|
@@ -304,19 +306,40 @@ def regex_extract_schedule(text):
|
|
| 304 |
})
|
| 305 |
logger.info(f"Regex class: {days} {time} at {location}")
|
| 306 |
|
| 307 |
-
# Pattern 3: Office hours
|
| 308 |
-
|
|
|
|
|
|
|
|
|
|
| 309 |
|
| 310 |
-
for match in re.finditer(
|
| 311 |
-
days = match.group(1)
|
| 312 |
time = match.group(2)
|
| 313 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 314 |
events.append({
|
| 315 |
-
"Event":
|
| 316 |
"Date/Time": f"{days} {time}",
|
| 317 |
-
"Location":
|
| 318 |
})
|
| 319 |
-
logger.info(f"Regex
|
| 320 |
|
| 321 |
return events
|
| 322 |
|
|
@@ -325,7 +348,6 @@ def regex_extract_deadlines(text):
|
|
| 325 |
events = []
|
| 326 |
|
| 327 |
# Pattern 1: Due dates table with assignment codes
|
| 328 |
-
# Look for lines with assignment codes followed by dates
|
| 329 |
due_dates_section = re.search(
|
| 330 |
r'Due Dates:(.{0,3000}?)(?:Artificial Intelligence:|Honor Code:|Student Health:|$)',
|
| 331 |
text,
|
|
@@ -336,19 +358,25 @@ def regex_extract_deadlines(text):
|
|
| 336 |
section_text = due_dates_section.group(1)
|
| 337 |
logger.info(f"Found due dates section: {len(section_text)} chars")
|
| 338 |
|
| 339 |
-
# Pattern: Assignment code + date with time
|
| 340 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 341 |
|
| 342 |
-
for
|
| 343 |
-
|
| 344 |
-
|
| 345 |
-
|
| 346 |
-
|
| 347 |
-
|
| 348 |
-
|
| 349 |
-
|
| 350 |
-
|
| 351 |
-
|
|
|
|
| 352 |
|
| 353 |
# Pattern 2: Important dates
|
| 354 |
important_section = re.search(
|
|
@@ -362,8 +390,9 @@ def regex_extract_deadlines(text):
|
|
| 362 |
lines = section_text.split('\n')
|
| 363 |
|
| 364 |
for line in lines:
|
|
|
|
| 365 |
match = re.search(
|
| 366 |
-
r'\d+\.\s*(.+?)\s+((?:January|February|March|April|May|June|July|August|September|October|November|December)\s+\d{1,2}(?:st|nd|rd|th)?)',
|
| 367 |
line,
|
| 368 |
re.IGNORECASE
|
| 369 |
)
|
|
@@ -371,7 +400,7 @@ def regex_extract_deadlines(text):
|
|
| 371 |
event = match.group(1).strip()
|
| 372 |
date = match.group(2).strip()
|
| 373 |
|
| 374 |
-
if len(event) <
|
| 375 |
events.append({
|
| 376 |
"Event": event,
|
| 377 |
"Date/Time": date
|
|
@@ -385,9 +414,9 @@ def regex_extract_deadlines(text):
|
|
| 385 |
date = match.group(1)
|
| 386 |
desc = match.group(2).strip()
|
| 387 |
|
| 388 |
-
# Only keep if mentions assignment, exam, project
|
| 389 |
-
keywords = ['assignment', 'exam', 'present', 'submit', 'due', 'project', 'proposal']
|
| 390 |
-
if any(kw in desc.lower() for kw in keywords) and len(desc) <
|
| 391 |
events.append({
|
| 392 |
"Event": desc,
|
| 393 |
"Date/Time": date
|
|
@@ -410,10 +439,10 @@ def regex_extract_deadlines(text):
|
|
| 410 |
return events
|
| 411 |
|
| 412 |
# ---------------------------------------
|
| 413 |
-
# Improved cleaning
|
| 414 |
# ---------------------------------------
|
| 415 |
def clean_events(events, event_type="deadline"):
|
| 416 |
-
"""Remove duplicates and invalid events
|
| 417 |
cleaned = []
|
| 418 |
seen = set()
|
| 419 |
|
|
@@ -429,10 +458,10 @@ def clean_events(events, event_type="deadline"):
|
|
| 429 |
if len(event_name) < 1 or len(date_time) < 2:
|
| 430 |
continue
|
| 431 |
|
| 432 |
-
# For deadlines, apply
|
| 433 |
if event_type == "deadline":
|
| 434 |
# Must be reasonable length
|
| 435 |
-
if len(event_name) >
|
| 436 |
logger.info(f"Filtering (too long): {event_name[:50]}...")
|
| 437 |
continue
|
| 438 |
|
|
@@ -441,12 +470,11 @@ def clean_events(events, event_type="deadline"):
|
|
| 441 |
logger.info(f"Filtering (bad date): {event_name} - {date_time}")
|
| 442 |
continue
|
| 443 |
|
| 444 |
-
# Filter out course description junk
|
| 445 |
junk_keywords = [
|
| 446 |
-
'syllabus', 'course overview', 'learning objectives',
|
| 447 |
-
'elaborated by', 'computational methods', '
|
| 448 |
-
'
|
| 449 |
-
'reading materials', 'empirical data', 'instructor', 'office', 'building'
|
| 450 |
]
|
| 451 |
|
| 452 |
if any(junk in event_name.lower() for junk in junk_keywords):
|
|
|
|
| 48 |
if not date_str or len(date_str) < 3:
|
| 49 |
return False
|
| 50 |
|
| 51 |
+
# Check for month names (full and abbreviated)
|
| 52 |
months = ['january', 'february', 'march', 'april', 'may', 'june',
|
| 53 |
+
'july', 'august', 'september', 'october', 'november', 'december',
|
| 54 |
+
'jan', 'feb', 'mar', 'apr', 'jun', 'jul', 'aug', 'sep', 'oct', 'nov', 'dec']
|
| 55 |
has_month = any(month in date_str.lower() for month in months)
|
| 56 |
|
| 57 |
+
# Or check for date pattern like 9/24, 10/15, 24-Sep, etc.
|
| 58 |
+
has_date_pattern = re.search(r'\d{1,2}[/-]\d{1,2}', date_str)
|
| 59 |
|
| 60 |
return has_month or has_date_pattern
|
| 61 |
|
|
|
|
| 68 |
patterns = [
|
| 69 |
r'\d{1,2}:\d{2}\s*(?:AM|PM|am|pm)', # 3:30 PM
|
| 70 |
r'\d{1,2}\s*(?:AM|PM|am|pm)', # 3 PM
|
| 71 |
+
r'\d{1,2}:\d{2}', # 3:30 (24hr or no AM/PM)
|
| 72 |
]
|
| 73 |
|
| 74 |
return any(re.search(p, time_str) for p in patterns)
|
|
|
|
| 80 |
|
| 81 |
# Must start with letter(s) followed by number(s)
|
| 82 |
pattern = r'^[A-Z]+\d+(?:\s*-\s*[A-Z]+\d*)?$'
|
| 83 |
+
return bool(re.match(pattern, code.strip(), re.IGNORECASE))
|
| 84 |
|
| 85 |
# ---------------------------------------
|
| 86 |
# Improved LLM extraction
|
|
|
|
| 128 |
time = parts[2]
|
| 129 |
location = parts[3] if len(parts) > 3 else ""
|
| 130 |
|
| 131 |
+
# Flexible validation for event types
|
| 132 |
+
valid_events = ["class", "office hours", "ta office hours", "office hour", "lecture"]
|
| 133 |
+
if not any(ve in event_type.lower() for ve in valid_events):
|
| 134 |
logger.info(f"Skipping invalid event type: {event_type}")
|
| 135 |
continue
|
| 136 |
|
|
|
|
| 138 |
logger.info(f"Skipping invalid time: {time}")
|
| 139 |
continue
|
| 140 |
|
| 141 |
+
# Days should be short
|
| 142 |
+
if len(days) > 15 or len(days) < 1:
|
| 143 |
logger.info(f"Skipping invalid days: {days}")
|
| 144 |
continue
|
| 145 |
|
|
|
|
| 171 |
|
| 172 |
all_events = []
|
| 173 |
|
| 174 |
+
for i, chunk in enumerate(chunks[:3]): # Limit to 3 chunks
|
| 175 |
logger.info(f"Processing deadline chunk {i+1}")
|
| 176 |
|
| 177 |
prompt = f"""Extract assignment due dates and exam dates from this text.
|
|
|
|
| 207 |
event_name = parts[0].strip()
|
| 208 |
date_time = parts[1].strip()
|
| 209 |
|
| 210 |
+
# Validation: event name should be short
|
| 211 |
if len(event_name) > 50:
|
| 212 |
logger.info(f"Skipping too-long event: {event_name[:30]}...")
|
| 213 |
continue
|
| 214 |
|
| 215 |
+
# Check if it's valid assignment code or contains exam/project keywords
|
| 216 |
is_assignment = is_valid_assignment_code(event_name)
|
| 217 |
is_exam = "exam" in event_name.lower() or "midterm" in event_name.lower() or "final" in event_name.lower()
|
| 218 |
is_project = "project" in event_name.lower() or "presentation" in event_name.lower()
|
|
|
|
| 265 |
time = match.group(2)
|
| 266 |
|
| 267 |
# Find location
|
| 268 |
+
context = text_header[max(0, match.start()-200):match.end()+300]
|
| 269 |
location = ""
|
| 270 |
|
| 271 |
loc_patterns = [
|
| 272 |
r'Room[:\s]*([A-Za-z]+\s+\d{3,4})',
|
| 273 |
+
r'(?:Building|Hall)[:\s]*\|?\s*(?:Room|Rm)?\s*([^\n]{0,50})',
|
| 274 |
]
|
| 275 |
|
| 276 |
for pattern in loc_patterns:
|
| 277 |
loc_match = re.search(pattern, context, re.IGNORECASE)
|
| 278 |
if loc_match:
|
| 279 |
+
location = loc_match.group(1).strip()[:100]
|
| 280 |
break
|
| 281 |
|
| 282 |
events.append({
|
|
|
|
| 287 |
logger.info(f"Regex class: {days} {time} at {location}")
|
| 288 |
|
| 289 |
# Pattern 2: "T R, 2:00 PM – 3:20 PM"
|
| 290 |
+
pattern2 = r"(?:Class Schedule|Lecture|Class)[:\s]*([MTWRFS]+(?:\s+[MTWRFS]+)*)[,\s]+(\d{1,2}:\d{2}\s*(?:AM|PM)\s*[–-]\s*\d{1,2}:\d{2}\s*(?:AM|PM))"
|
| 291 |
|
| 292 |
for match in re.finditer(pattern2, text_header, re.IGNORECASE):
|
| 293 |
days = match.group(1).replace(" ", "")
|
| 294 |
time = match.group(2)
|
| 295 |
|
| 296 |
+
context = text_header[match.end():match.end()+300]
|
| 297 |
location = ""
|
| 298 |
loc_match = re.search(r'In-person[:\s]*([^\n]+)', context, re.IGNORECASE)
|
| 299 |
if loc_match:
|
| 300 |
+
location = loc_match.group(1).strip()[:100]
|
| 301 |
|
| 302 |
events.append({
|
| 303 |
"Event": "Class",
|
|
|
|
| 306 |
})
|
| 307 |
logger.info(f"Regex class: {days} {time} at {location}")
|
| 308 |
|
| 309 |
+
# Pattern 3: Office hours - multiple formats
|
| 310 |
+
oh_patterns = [
|
| 311 |
+
r"Office Hours[:\s]*([MTWRFS]+(?:h)?(?:[,\s]+[MTWRFS]+(?:h)?)*)[,\s]+(\d{1,2}:\d{2}\s*(?:AM|PM|am|pm)\s*[-–]\s*\d{1,2}:\d{2}\s*(?:AM|PM|am|pm))",
|
| 312 |
+
r"Office[:\s]+([A-Za-z0-9\s]+)(?:\n|\r)", # For unusual formats
|
| 313 |
+
]
|
| 314 |
|
| 315 |
+
for match in re.finditer(oh_patterns[0], text_header, re.IGNORECASE):
|
| 316 |
+
days = match.group(1).replace(" ", "")
|
| 317 |
time = match.group(2)
|
| 318 |
|
| 319 |
+
# Check context for TA
|
| 320 |
+
context_before = text_header[max(0, match.start()-150):match.start()]
|
| 321 |
+
is_ta = "TA" in context_before[-80:] or "Teaching Assistant" in context_before
|
| 322 |
+
event_type = "TA Office Hours" if is_ta else "Office Hours"
|
| 323 |
+
|
| 324 |
+
# Look for location
|
| 325 |
+
context_after = text_header[match.end():match.end()+200]
|
| 326 |
+
location = ""
|
| 327 |
+
loc_patterns = [
|
| 328 |
+
r'(?:in|at|room|building)\s+([A-Za-z]+\s+\d{3,4})',
|
| 329 |
+
r'([A-Za-z]+\s+\d{3,4})',
|
| 330 |
+
]
|
| 331 |
+
for lp in loc_patterns:
|
| 332 |
+
loc_match = re.search(lp, context_after, re.IGNORECASE)
|
| 333 |
+
if loc_match:
|
| 334 |
+
location = loc_match.group(1).strip()
|
| 335 |
+
break
|
| 336 |
+
|
| 337 |
events.append({
|
| 338 |
+
"Event": event_type,
|
| 339 |
"Date/Time": f"{days} {time}",
|
| 340 |
+
"Location": location
|
| 341 |
})
|
| 342 |
+
logger.info(f"Regex {event_type}: {days} {time} at {location}")
|
| 343 |
|
| 344 |
return events
|
| 345 |
|
|
|
|
| 348 |
events = []
|
| 349 |
|
| 350 |
# Pattern 1: Due dates table with assignment codes
|
|
|
|
| 351 |
due_dates_section = re.search(
|
| 352 |
r'Due Dates:(.{0,3000}?)(?:Artificial Intelligence:|Honor Code:|Student Health:|$)',
|
| 353 |
text,
|
|
|
|
| 358 |
section_text = due_dates_section.group(1)
|
| 359 |
logger.info(f"Found due dates section: {len(section_text)} chars")
|
| 360 |
|
| 361 |
+
# Pattern: Assignment code + date with time (more flexible)
|
| 362 |
+
patterns = [
|
| 363 |
+
# A0, A1-RR1, PMS1, etc. with full date
|
| 364 |
+
r'([A-Z]+\d+(?:\s*-\s*[A-Z]+\d*)?)\s+((?:January|February|March|April|May|June|July|August|September|October|November|December)\s+\d{1,2}(?:st|nd|rd|th)?,?\s+\d{1,2}:\d{2}\s*(?:AM|PM))',
|
| 365 |
+
# Also catch without time
|
| 366 |
+
r'([A-Z]+\d+(?:\s*-\s*[A-Z]+\d*)?)\s+((?:January|February|March|April|May|June|July|August|September|October|November|December)\s+\d{1,2}(?:st|nd|rd|th)?)',
|
| 367 |
+
]
|
| 368 |
|
| 369 |
+
for pattern in patterns:
|
| 370 |
+
for match in re.finditer(pattern, section_text, re.IGNORECASE):
|
| 371 |
+
assignment = match.group(1).strip()
|
| 372 |
+
date = match.group(2).strip()
|
| 373 |
+
|
| 374 |
+
if is_valid_assignment_code(assignment):
|
| 375 |
+
events.append({
|
| 376 |
+
"Event": assignment,
|
| 377 |
+
"Date/Time": date
|
| 378 |
+
})
|
| 379 |
+
logger.info(f"Regex extracted: {assignment} - {date}")
|
| 380 |
|
| 381 |
# Pattern 2: Important dates
|
| 382 |
important_section = re.search(
|
|
|
|
| 390 |
lines = section_text.split('\n')
|
| 391 |
|
| 392 |
for line in lines:
|
| 393 |
+
# Match numbered items
|
| 394 |
match = re.search(
|
| 395 |
+
r'(?:\d+\.)\s*(.+?)\s+((?:January|February|March|April|May|June|July|August|September|October|November|December)\s+\d{1,2}(?:st|nd|rd|th)?)',
|
| 396 |
line,
|
| 397 |
re.IGNORECASE
|
| 398 |
)
|
|
|
|
| 400 |
event = match.group(1).strip()
|
| 401 |
date = match.group(2).strip()
|
| 402 |
|
| 403 |
+
if len(event) < 100: # Reasonable length
|
| 404 |
events.append({
|
| 405 |
"Event": event,
|
| 406 |
"Date/Time": date
|
|
|
|
| 414 |
date = match.group(1)
|
| 415 |
desc = match.group(2).strip()
|
| 416 |
|
| 417 |
+
# Only keep if mentions assignment, exam, project, presentation, submit
|
| 418 |
+
keywords = ['assignment', 'exam', 'present', 'submit', 'due', 'project', 'proposal', '#1', '#2']
|
| 419 |
+
if any(kw in desc.lower() for kw in keywords) and len(desc) < 120:
|
| 420 |
events.append({
|
| 421 |
"Event": desc,
|
| 422 |
"Date/Time": date
|
|
|
|
| 439 |
return events
|
| 440 |
|
| 441 |
# ---------------------------------------
|
| 442 |
+
# Improved cleaning with less aggressive filtering
|
| 443 |
# ---------------------------------------
|
| 444 |
def clean_events(events, event_type="deadline"):
|
| 445 |
+
"""Remove duplicates and invalid events"""
|
| 446 |
cleaned = []
|
| 447 |
seen = set()
|
| 448 |
|
|
|
|
| 458 |
if len(event_name) < 1 or len(date_time) < 2:
|
| 459 |
continue
|
| 460 |
|
| 461 |
+
# For deadlines, apply validation
|
| 462 |
if event_type == "deadline":
|
| 463 |
# Must be reasonable length
|
| 464 |
+
if len(event_name) > 100:
|
| 465 |
logger.info(f"Filtering (too long): {event_name[:50]}...")
|
| 466 |
continue
|
| 467 |
|
|
|
|
| 470 |
logger.info(f"Filtering (bad date): {event_name} - {date_time}")
|
| 471 |
continue
|
| 472 |
|
| 473 |
+
# Filter out obvious course description junk (less aggressive)
|
| 474 |
junk_keywords = [
|
| 475 |
+
'syllabus', 'course overview', 'learning objectives',
|
| 476 |
+
'elaborated by', 'computational methods', 'natural world',
|
| 477 |
+
'reading materials', 'empirical data'
|
|
|
|
| 478 |
]
|
| 479 |
|
| 480 |
if any(junk in event_name.lower() for junk in junk_keywords):
|