monish563/NU-KIOSK-API
0
1"""Blueprint identifying faculty whose research mentions a given topic."""2 3from __future__ import annotations4 5import re6from typing import Any, Dict, List7 8from .base import AnalysisContext, Blueprint, BlueprintResult, Fact9 10 11class FacultyByTopicBlueprint(Blueprint):12 """Return faculty whose research interests mention a topic."""13 14 name = "faculty_by_topic"15 16 def run(self, context: AnalysisContext, **kwargs: Any) -> BlueprintResult:17 topic = (kwargs.get("topic") or "").strip()18 if not topic:19 return BlueprintResult(self.name, kwargs, facts=[], notes=["No topic provided."])20 21 topic_lower = re.sub(r"[?.!]+$", "", topic.lower())22 tokens = [tok for tok in re.split(r"[^a-z0-9]+", topic_lower) if tok]23 if not tokens:24 return BlueprintResult(self.name, kwargs, facts=[], notes=[f"Topic '{topic}' is too vague."])25 26 search_terms = set()27 search_terms.add(" ".join(tokens))28 search_terms.update(tokens)29 if "ai" in tokens:30 search_terms.add("artificial intelligence")31 if "ml" in tokens:32 search_terms.add("machine learning")33 34 dataset = context.catalog.get("faculty")35 matches: List[Fact] = []36 for row in dataset.records:37 research = (row.get("Research Interests") or "").lower()38 if any(term in research for term in search_terms):39 matches.append(40 Fact(41 subject=row.get("Name", "Unknown"),42 predicate="research_focus",43 value=row.get("Research Interests"),44 source=dataset.origin,45 )46 )47 48 notes: List[str] = []49 if not matches:50 notes.append(f"No faculty rows mention '{topic}'.")51 52 return BlueprintResult(self.name, kwargs, facts=matches, notes=notes)53 