Spaces:
Sleeping
Sleeping
Download app/source_scope.py from fdsafd4555/campus-rag-backend: direct link, hf CLI and curl.
- Browser
- Download file 10.1 kB
-
https://huggingface.co/spaces/fdsafd4555/campus-rag-backend/resolve/main/app/source_scope.py
- Command line
-
hf download hf://spaces/fdsafd4555/campus-rag-backend/app/source_scope.py
-
curl -L -o source_scope.py https://huggingface.co/spaces/fdsafd4555/campus-rag-backend/resolve/main/app/source_scope.py
10.1 kB
| """Check a document's applicability independently of its retrieval score.""" | |
| from __future__ import annotations | |
| import re | |
| from datetime import date | |
| from urllib.parse import urlparse | |
| from . import department_focus as departments | |
| from .query_plan import AUDIENCES, PROGRAM_CONTEXTS, SCHOOL_ALIASES, SearchTask, department_catalog | |
| def source_department(row: dict) -> str | None: | |
| m = row.get("metadata") or {} | |
| identity = m.get("department_id") | |
| if identity: | |
| return str(identity) | |
| dataset = m.get("source_dataset") or m.get("ingestion_dataset") | |
| catalog = department_catalog() | |
| if any(e.get("dataset") == dataset for e in catalog): | |
| return str(dataset) | |
| url = row.get("url") or row.get("uri") or m.get("doc_url") or "" | |
| parsed = urlparse(str(url)) | |
| for entry in catalog: | |
| host = urlparse(entry.get("base_url") or "").hostname | |
| prefix = entry.get("site_prefix") | |
| if (host and host not in {"www.pknu.ac.kr", "pknu.ac.kr", "icms.pknu.ac.kr"} and parsed.hostname == host) or (prefix and parsed.hostname == "icms.pknu.ac.kr" and parsed.path.startswith(f"/{prefix}/")): | |
| return entry.get("dataset") | |
| return None | |
| def scope_reason(row: dict, task: SearchTask) -> str | None: | |
| """None means compatible, not proof that every statement is supported.""" | |
| m = row.get("metadata") or {} | |
| title = str(m.get("doc_title") or row.get("title") or "") | |
| content = str(row.get("content") or "") | |
| target = task.target | |
| table = m.get("structured_table") or {} | |
| table_rows = table.get("rows") or [] | |
| fields = table_rows[0].get("fields", {}) if table.get("kind") == "spreadsheet" and len(table_rows) == 1 else {} | |
| staff = str(fields.get("staff_name") or m.get("staff_name") or "") | |
| spreadsheet = table.get("kind") == "spreadsheet" or str(m.get("source_ext") or "").lower() in {".xlsx", ".xls"} | |
| if target.instructor and spreadsheet and re.search(r"κ°μ|μμ |κ³Όλͺ©|κ°μ’", task.text) and not fields.get("staff_name"): | |
| return "unverified_spreadsheet_instructor" | |
| if target.instructor and ((staff and staff != target.instructor) or (task.conditions.kind == "courses" and staff != target.instructor)): | |
| return "different_instructor" | |
| if target.level and (fields.get("level") or m.get("level")): | |
| level = str(fields.get("level") or m["level"]) | |
| if not (level.endswith("λνμ") if target.level == "λνμ" else level == target.level): | |
| return "different_course_level" | |
| if fields: | |
| for key, expected in (("year", task.conditions.year), ("term", task.conditions.term)): | |
| if expected is not None and fields.get(key) and str(fields[key]) != str(expected): | |
| return "different_course_term" | |
| amendment = re.search(r"(?:κ°μ |μν)\s*\(?\s*(20\d{2})\s*[.-]\s*(\d{1,2})\s*[.-]\s*(\d{1,2})", title) | |
| if amendment and task.conditions.kind == "search": | |
| try: | |
| if date(*(int(x) for x in amendment.groups())) > task.conditions.as_of.date(): | |
| return "future_amendment" | |
| except ValueError: | |
| pass | |
| if task.conditions.kind == "search": | |
| for context, pattern in PROGRAM_CONTEXTS.items(): | |
| if context not in target.contexts and re.search(pattern, title): | |
| return "different_program_context" | |
| syllabus = row.get("_syllabus") or m.get("source_dataset") == "pknu_syllabus" or m.get("rpc_name") == "match_pknu_syllabus_documents" | |
| if (syllabus and not row.get("_syllabus") | |
| and re.search(r"μ 곡\s*(?:νμ|곡ν΅)|κ΅μ‘κ³Όμ |κ΅κ³Όκ³Όμ |μ΄μν", task.text) | |
| and not re.search(r"κ°μκ³νμ|λ΄λΉ\s*κ΅μ|νμλ²νΈ|λΆλ°|νκ°", task.text)): | |
| return "different_document_purpose" | |
| if syllabus and not row.get("_syllabus") and not re.search(r"κ°μ|μμ |κ³Όλͺ©|κ΅μ|νμλ²νΈ|λΆλ°", task.text): | |
| return "different_document_purpose" | |
| infection_exception = re.search(r"λ°±μ |κ°μΌ\s*μ¦μ|μ½λ‘λ", content) | |
| common_absence_rules = re.search(r"κ²½μ‘°μ¬|λ³μ|곡μ μΈ?\s*μ무|μ§κ³|μ¬λ§|λν", content) | |
| if infection_exception and not common_absence_rules and "κ°μΌλ³" not in target.contexts: | |
| if re.search(r"곡결|μΆμμΈμ ", task.text): | |
| return "conditional_exception" | |
| school = str(m.get("applicable_school") or m.get("university") or m.get("school") or "") | |
| school = SCHOOL_ALIASES.get(school, school).removeprefix("κ΅λ¦½") | |
| exchange = re.search(r"ν\s*λν\s*\(([^)]+)\)", title) | |
| if exchange: | |
| school = SCHOOL_ALIASES.get(exchange[1], exchange[1]).removeprefix("κ΅λ¦½") | |
| if school and school != target.school: | |
| if not (exchange and re.search(r"κ΅λ₯|κ΅ν|ν\s*λν", task.text)): | |
| return "different_school" | |
| # Publishing a document on pknu.ac.kr does not make another school's rules applicable. | |
| identity = source_department(row) | |
| if target.department_id and identity and identity != target.department_id: | |
| return "different_department" | |
| # Several majors share one dataset (e.g. ce). A dataset match alone must | |
| # not make a sibling major's explicit curriculum heading applicable. | |
| if target.department: | |
| wanted_major = departments.normalize(target.department) | |
| heading = departments.normalize(title + " " + str(m.get("subcategory") or "")) | |
| for entry in department_catalog(): | |
| majors = [departments.normalize(name.strip()) for value in entry.get("majors") or [] | |
| for name in str(value).split(",") if name.strip()] | |
| if wanted_major in majors and any(name in heading for name in majors) and wanted_major not in heading: | |
| return "different_major" | |
| if task.conditions.kind == "latest" and target.department_id and identity != target.department_id: | |
| return "department_unverified" | |
| applicable = m.get("applicable_department") | |
| if applicable: | |
| variants = departments.focus_variants(target.department or "") | |
| if not variants or not departments.mentions(str(applicable), variants): | |
| return "different_department" | |
| # A latest-notice catalogue describes documents, not the user's eligibility. | |
| catalogue = task.conditions.kind == "latest" | |
| restricted = str(m.get("applicable_audience") or m.get("audience") or "") | |
| heading = f"{title} {m.get('subcategory') or ''} {restricted}" | |
| if "νλΆ" in target.audiences and "λνμ" in heading: | |
| return "different_level" | |
| if task.conditions.kind == "search" and re.search(r"(?:μμ|μ μ)μ ν\s*ν©κ²©μ|νΈμ μ\s*ν©κ²©μ", content) and "μ¬νμ" not in content: | |
| if not set(target.audiences) & {"μ μ μ", "νΈμ μ"}: | |
| return "restricted_student_section" | |
| for audience in ("μΈκ΅μΈ", "λνμ", "μ μ μ", "νΈμ μ"): | |
| if any(w in heading.lower() for w in AUDIENCES[audience]): | |
| if not catalogue and audience not in target.audiences: | |
| # 'μ Β·νΈμ μ' is one combined population; either explicit type suffices. | |
| if audience in {"μ μ μ", "νΈμ μ"} and set(target.audiences) & {"μ μ μ", "νΈμ μ"}: | |
| continue | |
| return "restricted_audience" | |
| grades = m.get("applicable_grades") | |
| if target.grade and isinstance(grades, list) and str(target.grade) not in {str(g) for g in grades}: | |
| return "different_grade" | |
| cohorts = m.get("applicable_admission_years") | |
| if target.admission_year and isinstance(cohorts, list) and str(target.admission_year) not in {str(y) for y in cohorts}: | |
| return "different_admission_year" | |
| if task.purpose == "eligibility" and re.search(r"μ μ²|μ§μ", task.text): | |
| if re.search(r"μ΄μ\s*쑰건", content) and not re.search(r"(?:μ μ²|μ§μ)\s*(?:μ격|λμ|κ°λ₯)|μ μ²ν \s*μ", content): | |
| return "completion_not_application_eligibility" | |
| # Per-major selection rules must not become university-wide requirements. | |
| named = [name for name in departments.DEPARTMENT_NAMES if departments.normalize(name) in departments.normalize(content)] | |
| # Historical/renamed majors may no longer appear in today's registry. | |
| named += [name for name in re.findall(r"[κ°-ν£Β·]{3,}(?:νκ³Ό|νλΆ|μ 곡)", content) | |
| if name not in {"볡μμ 곡", "λΆμ 곡", "μ£Όμ 곡", "λ€μ 곡", "ν΄λΉνκ³Ό", "μμνκ³Ό", "ν΄λΉμ 곡"}] | |
| selection_table = bool(re.search(r"μΈλΆ\s*μ λ°|μ μ \s*μ λ°|μ μΆ\s*μ λ°|νκ³Όλ³\s*κΈ°μ€", title + " " + content)) | |
| if selection_table and named: | |
| variants = departments.focus_variants(target.department or "") | |
| if not variants: | |
| return "department_specific_rule" | |
| if not departments.mentions(content, variants): | |
| return "different_department" | |
| if re.search(r"κ°μΌλ³\s*νμ°|μ½λ‘λ.{0,15}(?:νμ°|μ κ΅)", content) and re.search(r"μ κ΅μ΄?\s*μ΄λ €|μ κ΅.{0,10}λΆκ°", content): | |
| if not re.search(r"κ°μΌλ³|μ½λ‘λ|μ κ΅", task.text): | |
| return "conditional_exception" | |
| return None | |
| def checked_rows(rows: list[dict], task: SearchTask) -> list[dict]: | |
| kept = [] | |
| for row in rows: | |
| if scope_reason(row, task): | |
| continue | |
| m = dict(row.get("metadata") or {}) | |
| m["validated_target"] = task.target.model_dump() | |
| m["query_conditions"] = task.conditions.model_dump(mode="json") | |
| m["request_purpose"] = task.purpose | |
| m["request_query"] = task.text | |
| # This label travels with the evidence into generation; URL alone is not scope. | |
| m["applicability_note"] = "κ³΅ν΅ κ·μ κ³Ό νΉμ λμμ μμΈλ₯Ό ꡬλΆνκ³ , μ μ²μ격μ μ΄μ쑰건μΌλ‘ λ체νμ§ λ§μΈμ. νλ²Β·νλ Β·κ°μΈ μ격μ νμΈνμ§ λͺ»νμΌλ©΄ ν΄λΉ νμμκ² μ μ©λλ€κ³ νμ νμ§ λ§μΈμ." | |
| kept.append({**row, "metadata": m}) | |
| return kept | |