nikeshn commited on
Commit
a9bcb90
Β·
verified Β·
1 Parent(s): 32b8f41

Update src/api/agent.py

Browse files
Files changed (1) hide show
  1. src/api/agent.py +260 -84
src/api/agent.py CHANGED
@@ -1136,6 +1136,244 @@ def _is_database_recommendation_question(question: str) -> bool:
1136
  return bool(DATABASE_RECOMMEND_RE.search(question or ""))
1137
 
1138
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1139
  def _extract_db_topic(question: str) -> str:
1140
  """
1141
  Extract the subject from a database-recommendation question.
@@ -1165,95 +1403,33 @@ def _extract_db_topic(question: str) -> str:
1165
 
1166
  def _database_recommendation_answer(question: str) -> str:
1167
  q = (question or "").lower()
1168
- # FIX: extract clean subject keyword, not the full question
1169
  subject = _extract_db_topic(question)
1170
  topic = _escape(subject)
1171
- dbs = []
1172
- if any(t in q for t in ["computer science", "artificial intelligence", "machine learning", " ai ", "cybersecurity", "computing", "software"]):
1173
- dbs = [
1174
- ("ACM Digital Library", "https://khalifa.idm.oclc.org/login?url=https://dl.acm.org", "Core computer science literature."),
1175
- ("IEEE Xplore", "https://khalifa.idm.oclc.org/login?url=https://ieeexplore.ieee.org", "Strong for AI, systems, and engineering computing."),
1176
- ("Scopus", "https://khalifa.idm.oclc.org/login?url=https://www.scopus.com", "Broad discovery and citation tracking."),
1177
- ]
1178
- elif any(t in q for t in ["medical", "health", "nursing", "clinical", "biomedical", "medicine", "pharmacy"]):
1179
- dbs = [
1180
- ("PubMed@KU", "http://www.ncbi.nlm.nih.gov/pubmed/?otool=iaekustlib", "Core biomedical literature with KU full-text links."),
1181
- ("Embase", "https://khalifa.idm.oclc.org/login?url=https://www.embase.com", "Biomedical and pharmacological literature."),
1182
- ("CINAHL Ultimate", "https://khalifa.idm.oclc.org/login?url=https://search.ebscohost.com/login.aspx?authtype=ip,uid&profile=ehost&defaultdb=cul", "Nursing and allied health literature."),
1183
- ]
1184
- elif any(t in q for t in ["civil", "structural", "construction"]):
1185
- dbs = [
1186
- ("ASCE Library", "https://khalifa.idm.oclc.org/login?url=https://ascelibrary.org", "Core civil engineering journals and conference papers."),
1187
- ("ScienceDirect", "https://khalifa.idm.oclc.org/login?url=https://www.sciencedirect.com", "Strong full text across civil engineering topics."),
1188
- ("Scopus", "https://khalifa.idm.oclc.org/login?url=https://www.scopus.com", "Discovery and citation tracking."),
1189
- ]
1190
- elif any(t in q for t in ["mechanical", "thermodynamics", "fluid"]):
1191
- dbs = [
1192
- ("ASME Digital Library", "https://khalifa.idm.oclc.org/login?url=https://asmedigitalcollection.asme.org", "Core mechanical engineering literature."),
1193
- ("Knovel", "https://khalifa.idm.oclc.org/login?url=https://app.knovel.com/kn", "Engineering reference books and data."),
1194
- ("ScienceDirect", "https://khalifa.idm.oclc.org/login?url=https://www.sciencedirect.com", "Broad full text."),
1195
- ]
1196
- elif any(t in q for t in ["electrical", "electronics", "circuit", "signal"]):
1197
- dbs = [
1198
- ("IEEE Xplore", "https://khalifa.idm.oclc.org/login?url=https://ieeexplore.ieee.org/Xplore/dynhome.jsp", "Core electrical engineering database."),
1199
- ("INSPEC", "https://khalifa.idm.oclc.org/login?url=https://www.theiet.org/publishing/inspec", "Strong indexing for EE and electronics."),
1200
- ("Scopus", "https://khalifa.idm.oclc.org/login?url=https://www.scopus.com", "Discovery and citation tracking."),
1201
- ]
1202
- elif any(t in q for t in ["physics", "quantum", "astrophysics", "optics", "thermodynamics"]):
1203
- dbs = [
1204
- ("APS Journals", "https://khalifa.idm.oclc.org/login?url=https://journals.aps.org", "American Physical Society β€” core physics journals."),
1205
- ("AIP Scitation", "https://khalifa.idm.oclc.org/login?url=https://aip.scitation.org", "American Institute of Physics journals."),
1206
- ("arXiv.org", "https://arxiv.org/", "Open-access preprints in physics, maths, and CS."),
1207
- ("Scopus", "https://khalifa.idm.oclc.org/login?url=https://www.scopus.com", "Broad discovery and citation tracking."),
1208
- ]
1209
- elif any(t in q for t in ["math", "mathematics", "statistics", "algebra", "calculus"]):
1210
- dbs = [
1211
- ("MathSciNet", "https://khalifa.idm.oclc.org/login?url=https://search.ebscohost.com/login.aspx?authtype=ip,uid&profile=ehost&defaultdb=msn", "Mathematics literature and reviews."),
1212
- ("arXiv.org", "https://arxiv.org/", "Open-access preprints in mathematics."),
1213
- ("Scopus", "https://khalifa.idm.oclc.org/login?url=https://www.scopus.com", "Broad discovery and citation tracking."),
1214
- ]
1215
- elif any(t in q for t in ["chemistry", "chemical", "materials"]):
1216
- dbs = [
1217
- ("ACS Publications", "https://khalifa.idm.oclc.org/login?url=https://pubs.acs.org", "Core chemistry and chemical engineering journals."),
1218
- ("SciFinder-n", "https://khalifa.idm.oclc.org/login?url=https://scifinder-n.cas.org", "Chemistry research and reaction searching."),
1219
- ("RSC Journals", "https://khalifa.idm.oclc.org/login?url=https://pubs.rsc.org/", "Royal Society of Chemistry journals."),
1220
- ]
1221
- elif any(t in q for t in ["aerospace", "aeronautics", "aviation", "space"]):
1222
- dbs = [
1223
- ("AIAA", "https://khalifa.idm.oclc.org/login?url=https://arc.aiaa.org/", "Aerospace science and engineering."),
1224
- ("ASME Digital Library", "https://khalifa.idm.oclc.org/login?url=https://asmedigitalcollection.asme.org", "Mechanical and aerospace engineering."),
1225
- ("Scopus", "https://khalifa.idm.oclc.org/login?url=https://www.scopus.com", "Broad discovery and citation tracking."),
1226
- ]
1227
- elif any(t in q for t in ["energy", "petroleum", "oil", "gas", "renewable"]):
1228
- dbs = [
1229
- ("OnePetro", "https://khalifa.idm.oclc.org/login?url=https://www.onepetro.org/", "Oil, gas, and petroleum engineering literature."),
1230
- ("ScienceDirect", "https://khalifa.idm.oclc.org/login?url=https://www.sciencedirect.com", "Energy and petroleum journals."),
1231
- ("Knovel", "https://khalifa.idm.oclc.org/login?url=https://app.knovel.com/kn", "Energy and engineering reference books."),
1232
- ]
1233
- elif any(t in q for t in ["business", "management", "economics", "finance", "accounting"]):
1234
- dbs = [
1235
- ("Business Source Ultimate", "https://khalifa.idm.oclc.org/login?url=https://search.ebscohost.com/login.aspx?authtype=ip,uid&profile=ehost&defaultdb=bsu", "Core business and management database."),
1236
- ("Emerald Insight", "https://khalifa.idm.oclc.org/login?url=https://www.emerald.com/insight", "Management, strategy, and leadership journals."),
1237
- ("Scopus", "https://khalifa.idm.oclc.org/login?url=https://www.scopus.com", "Broad discovery and citation tracking."),
1238
- ]
1239
- elif any(t in q for t in ["environment", "environmental", "ecology", "sustainability", "climate"]):
1240
- dbs = [
1241
- ("ScienceDirect", "https://khalifa.idm.oclc.org/login?url=https://www.sciencedirect.com", "Broad environmental and sustainability journals."),
1242
- ("Web of Science", "https://khalifa.idm.oclc.org/login?url=https://www.webofscience.com", "Multidisciplinary citation searching."),
1243
- ("Scopus", "https://khalifa.idm.oclc.org/login?url=https://www.scopus.com", "Discovery and citation tracking."),
1244
- ]
1245
- else:
1246
- # Generic fallback β€” all URLs go through IDM proxy to avoid frontend auto-linkify breaking HTML
1247
- dbs = [
1248
- ("PRIMO (KU Library Discovery)", "https://khalifa.primo.exlibrisgroup.com/discovery/search?vid=971KUOSTAR_INST:KU", "Best starting point for KU-accessible books, articles, and journals."),
1249
- ("Scopus", "https://khalifa.idm.oclc.org/login?url=https://www.scopus.com", "Broad multidisciplinary discovery and citation tracking."),
1250
- ("Web of Science", "https://khalifa.idm.oclc.org/login?url=https://www.webofscience.com", "Multidisciplinary citation searching and impact metrics."),
1251
- ]
1252
  answer = f"<strong>Recommended databases for {topic}</strong><br><br>"
1253
- answer += "Here are the strongest starting points for this subject at KU:<br>"
1254
  for name, url, why in dbs:
1255
  answer += f'<br>β€’ <a href="{url}" target="_blank"><strong>{_escape(name)}</strong></a> β€” {_escape(why)}'
1256
- answer += "<br><br>Want me to search any of these directly for a specific topic?"
 
 
 
 
 
 
 
 
 
 
 
 
1257
  return answer
1258
 
1259
 
 
1136
  return bool(DATABASE_RECOMMEND_RE.search(question or ""))
1137
 
1138
 
1139
+ def _safe_db_url(url: str) -> str:
1140
+ """
1141
+ Encode the inner URL in IDM proxy links so the frontend fmt() auto-linkify
1142
+ regex cannot match a bare https:// inside an href attribute and break the HTML.
1143
+ e.g. href="...?url=https://foo.com" β†’ href="...?url=https%3A%2F%2Ffoo.com"
1144
+ """
1145
+ from urllib.parse import quote as _quote
1146
+ if "?url=" in url:
1147
+ base, inner = url.split("?url=", 1)
1148
+ return base + "?url=" + _quote(inner, safe="")
1149
+ return url
1150
+
1151
+
1152
+ # ── Subject β†’ database mapping sourced from KU KB ────────────────────────────
1153
+ # All IDM proxy URLs are encoded via _safe_db_url to prevent HTML breakage.
1154
+ _DB_SUBJECTS: Dict[str, List[Tuple[str, str, str]]] = {
1155
+ "physics": [
1156
+ ("APS Journals", _safe_db_url("https://khalifa.idm.oclc.org/login?url=https://journals.aps.org"), "American Physical Society β€” core physics journals."),
1157
+ ("AIP Publishing", _safe_db_url("https://khalifa.idm.oclc.org/login?url=https://pubs.aip.org"), "Core physics and applied physics literature."),
1158
+ ("IOPScience", _safe_db_url("https://khalifa.idm.oclc.org/login?url=https://iopscience.iop.org"), "Core physics journal platform."),
1159
+ ("arXiv", "https://arxiv.org", "Open-access preprints in physics and maths."),
1160
+ ("ScienceDirect", _safe_db_url("https://khalifa.idm.oclc.org/login?url=https://www.sciencedirect.com"), "Full text across applied and interdisciplinary physics."),
1161
+ ("Scopus", _safe_db_url("https://khalifa.idm.oclc.org/login?url=https://www.scopus.com"), "Discovery and citation tracking."),
1162
+ ],
1163
+ "chemistry": [
1164
+ ("ACS Publications", _safe_db_url("https://khalifa.idm.oclc.org/login?url=https://pubs.acs.org"), "Core chemistry journals from the American Chemical Society."),
1165
+ ("SciFinder-n", _safe_db_url("https://khalifa.idm.oclc.org/login?url=https://scifinder-n.cas.org"), "Substance, reaction, reference, and patent searching."),
1166
+ ("Reaxys", _safe_db_url("https://khalifa.idm.oclc.org/login?url=https://www.reaxys.com"), "Reaction and property searching."),
1167
+ ("RSC Journals", _safe_db_url("https://khalifa.idm.oclc.org/login?url=https://pubs.rsc.org"), "Royal Society of Chemistry journals."),
1168
+ ("ScienceDirect", _safe_db_url("https://khalifa.idm.oclc.org/login?url=https://www.sciencedirect.com"), "Full text chemistry journals and books."),
1169
+ ("Scopus", _safe_db_url("https://khalifa.idm.oclc.org/login?url=https://www.scopus.com"), "Discovery and citation tracking."),
1170
+ ],
1171
+ "chemical_engineering": [
1172
+ ("ScienceDirect", _safe_db_url("https://khalifa.idm.oclc.org/login?url=https://www.sciencedirect.com"), "Strong full text for chemical engineering journals and books."),
1173
+ ("ACS Publications", _safe_db_url("https://khalifa.idm.oclc.org/login?url=https://pubs.acs.org"), "Core chemistry and chemical engineering coverage."),
1174
+ ("SciFinder-n", _safe_db_url("https://khalifa.idm.oclc.org/login?url=https://scifinder-n.cas.org"), "Chemical substances, reactions, references, and patents."),
1175
+ ("Reaxys", _safe_db_url("https://khalifa.idm.oclc.org/login?url=https://www.reaxys.com"), "Reaction and property searching."),
1176
+ ("Knovel", _safe_db_url("https://khalifa.idm.oclc.org/login?url=https://app.knovel.com"), "Engineering handbooks and technical reference data."),
1177
+ ("Scopus", _safe_db_url("https://khalifa.idm.oclc.org/login?url=https://www.scopus.com"), "Broad discovery and citation tracking."),
1178
+ ],
1179
+ "computer_science": [
1180
+ ("ACM Digital Library", _safe_db_url("https://khalifa.idm.oclc.org/login?url=https://dl.acm.org"), "Core computer science literature."),
1181
+ ("IEEE Xplore", _safe_db_url("https://khalifa.idm.oclc.org/login?url=https://ieeexplore.ieee.org"), "Strong for CS, AI, networks, and systems."),
1182
+ ("arXiv", "https://arxiv.org", "Important preprint source for CS, AI, and ML."),
1183
+ ("INSPEC", _safe_db_url("https://khalifa.idm.oclc.org/login?url=https://www.theiet.org/publishing/inspec"), "Indexing for computing and engineering topics."),
1184
+ ("ScienceDirect", _safe_db_url("https://khalifa.idm.oclc.org/login?url=https://www.sciencedirect.com"), "Full text across CS and applied computing."),
1185
+ ("Scopus", _safe_db_url("https://khalifa.idm.oclc.org/login?url=https://www.scopus.com"), "Broad discovery and citation tracking."),
1186
+ ],
1187
+ "robotics": [
1188
+ ("IEEE Xplore", _safe_db_url("https://khalifa.idm.oclc.org/login?url=https://ieeexplore.ieee.org"), "Core source for robotics, AI, automation, and intelligent systems."),
1189
+ ("ACM Digital Library", _safe_db_url("https://khalifa.idm.oclc.org/login?url=https://dl.acm.org"), "AI, ML, and computing research."),
1190
+ ("arXiv", "https://arxiv.org", "Essential for current AI, robotics, and ML preprints."),
1191
+ ("INSPEC", _safe_db_url("https://khalifa.idm.oclc.org/login?url=https://www.theiet.org/publishing/inspec"), "Indexing for robotics, computing, electronics, and control."),
1192
+ ("ScienceDirect", _safe_db_url("https://khalifa.idm.oclc.org/login?url=https://www.sciencedirect.com"), "Full text for AI, robotics, and applied engineering."),
1193
+ ],
1194
+ "electrical_engineering": [
1195
+ ("IEEE Xplore", _safe_db_url("https://khalifa.idm.oclc.org/login?url=https://ieeexplore.ieee.org"), "Core electrical engineering database."),
1196
+ ("INSPEC", _safe_db_url("https://khalifa.idm.oclc.org/login?url=https://www.theiet.org/publishing/inspec"), "Strong indexing for EE, electronics, and physics."),
1197
+ ("ScienceDirect", _safe_db_url("https://khalifa.idm.oclc.org/login?url=https://www.sciencedirect.com"), "Full text for electrical engineering and control."),
1198
+ ("ASTM Compass", _safe_db_url("https://khalifa.idm.oclc.org/login?url=https://compass.astm.org"), "Standards and testing methods."),
1199
+ ("Scopus", _safe_db_url("https://khalifa.idm.oclc.org/login?url=https://www.scopus.com"), "Broad discovery and citation tracking."),
1200
+ ],
1201
+ "mechanical_engineering": [
1202
+ ("ASME Digital Library", _safe_db_url("https://khalifa.idm.oclc.org/login?url=https://asmedigitalcollection.asme.org"), "Core mechanical engineering literature."),
1203
+ ("ScienceDirect", _safe_db_url("https://khalifa.idm.oclc.org/login?url=https://www.sciencedirect.com"), "Full text across mechanics, thermal, and manufacturing topics."),
1204
+ ("Knovel", _safe_db_url("https://khalifa.idm.oclc.org/login?url=https://app.knovel.com"), "Engineering reference data and handbooks."),
1205
+ ("ASTM Compass", _safe_db_url("https://khalifa.idm.oclc.org/login?url=https://compass.astm.org"), "Standards and materials testing information."),
1206
+ ("Scopus", _safe_db_url("https://khalifa.idm.oclc.org/login?url=https://www.scopus.com"), "Broad discovery and citation tracking."),
1207
+ ],
1208
+ "civil_engineering": [
1209
+ ("ASCE Library", _safe_db_url("https://khalifa.idm.oclc.org/login?url=https://ascelibrary.org"), "Core civil and structural engineering journals and conference papers."),
1210
+ ("ICE Virtual Library", _safe_db_url("https://khalifa.idm.oclc.org/login?url=https://www.icevirtuallibrary.com"), "Civil engineering literature and proceedings."),
1211
+ ("ASTM Compass", _safe_db_url("https://khalifa.idm.oclc.org/login?url=https://compass.astm.org"), "Standards and testing methods for materials and engineering."),
1212
+ ("ScienceDirect", _safe_db_url("https://khalifa.idm.oclc.org/login?url=https://www.sciencedirect.com"), "Full text across civil engineering areas."),
1213
+ ("Knovel", _safe_db_url("https://khalifa.idm.oclc.org/login?url=https://app.knovel.com"), "Handbooks and design reference data."),
1214
+ ],
1215
+ "aerospace": [
1216
+ ("AIAA", _safe_db_url("https://khalifa.idm.oclc.org/login?url=https://arc.aiaa.org/"), "Core aerospace journals, conference papers, and standards."),
1217
+ ("ASME Digital Library", _safe_db_url("https://khalifa.idm.oclc.org/login?url=https://asmedigitalcollection.asme.org"), "Thermal, mechanical, and fluid systems relevant to aerospace."),
1218
+ ("ScienceDirect", _safe_db_url("https://khalifa.idm.oclc.org/login?url=https://www.sciencedirect.com"), "Strong full text for aerospace, mechanics, and materials."),
1219
+ ("IEEE Xplore", _safe_db_url("https://khalifa.idm.oclc.org/login?url=https://ieeexplore.ieee.org"), "Avionics, control systems, and guidance."),
1220
+ ("Scopus", _safe_db_url("https://khalifa.idm.oclc.org/login?url=https://www.scopus.com"), "Broad discovery and citation tracking."),
1221
+ ],
1222
+ "petroleum": [
1223
+ ("OnePetro", _safe_db_url("https://khalifa.idm.oclc.org/login?url=https://onepetro.org"), "Core petroleum engineering and oil and gas literature."),
1224
+ ("EarthDoc", _safe_db_url("https://khalifa.idm.oclc.org/login?url=https://www.earthdoc.org"), "Geoscience and petroleum-related literature."),
1225
+ ("ScienceDirect", _safe_db_url("https://khalifa.idm.oclc.org/login?url=https://www.sciencedirect.com"), "Full text for petroleum, energy, and engineering topics."),
1226
+ ("Knovel", _safe_db_url("https://khalifa.idm.oclc.org/login?url=https://app.knovel.com"), "Engineering handbooks and oil/gas reference material."),
1227
+ ("Scopus", _safe_db_url("https://khalifa.idm.oclc.org/login?url=https://www.scopus.com"), "Broad discovery and citation tracking."),
1228
+ ],
1229
+ "energy": [
1230
+ ("ScienceDirect", _safe_db_url("https://khalifa.idm.oclc.org/login?url=https://www.sciencedirect.com"), "Strong energy engineering and sustainability full text."),
1231
+ ("OnePetro", _safe_db_url("https://khalifa.idm.oclc.org/login?url=https://onepetro.org"), "Energy sector and petroleum crossover topics."),
1232
+ ("IEEE Xplore", _safe_db_url("https://khalifa.idm.oclc.org/login?url=https://ieeexplore.ieee.org"), "Power systems, electrical energy, and smart grids."),
1233
+ ("Knovel", _safe_db_url("https://khalifa.idm.oclc.org/login?url=https://app.knovel.com"), "Engineering handbooks and energy reference content."),
1234
+ ("Scopus", _safe_db_url("https://khalifa.idm.oclc.org/login?url=https://www.scopus.com"), "Broad discovery and citation tracking."),
1235
+ ],
1236
+ "mathematics": [
1237
+ ("MathSciNet", "https://mathscinet.ams.org", "Core mathematics review and discovery database."),
1238
+ ("SpringerLink", _safe_db_url("https://khalifa.idm.oclc.org/login?url=https://link.springer.com"), "Strong mathematics and statistics book and journal coverage."),
1239
+ ("ScienceDirect", _safe_db_url("https://khalifa.idm.oclc.org/login?url=https://www.sciencedirect.com"), "Full text for mathematics, statistics, and data science."),
1240
+ ("JSTOR", _safe_db_url("https://khalifa.idm.oclc.org/login?url=https://www.jstor.org"), "Foundational mathematics and statistics literature."),
1241
+ ("arXiv", "https://arxiv.org", "Preprints in mathematics, statistics, and data science."),
1242
+ ("Scopus", _safe_db_url("https://khalifa.idm.oclc.org/login?url=https://www.scopus.com"), "Broad discovery and citation tracking."),
1243
+ ],
1244
+ "materials": [
1245
+ ("ScienceDirect", _safe_db_url("https://khalifa.idm.oclc.org/login?url=https://www.sciencedirect.com"), "Strong materials science full text."),
1246
+ ("SpringerLink", _safe_db_url("https://khalifa.idm.oclc.org/login?url=https://link.springer.com"), "Strong materials and nanoscience coverage."),
1247
+ ("AIP Publishing", _safe_db_url("https://khalifa.idm.oclc.org/login?url=https://pubs.aip.org"), "Applied materials and physics crossover."),
1248
+ ("IOPScience", _safe_db_url("https://khalifa.idm.oclc.org/login?url=https://iopscience.iop.org"), "Materials and physics crossover."),
1249
+ ("Knovel", _safe_db_url("https://khalifa.idm.oclc.org/login?url=https://app.knovel.com"), "Reference data and engineering materials handbooks."),
1250
+ ("Scopus", _safe_db_url("https://khalifa.idm.oclc.org/login?url=https://www.scopus.com"), "Broad discovery and citation tracking."),
1251
+ ],
1252
+ "nuclear": [
1253
+ ("ScienceDirect", _safe_db_url("https://khalifa.idm.oclc.org/login?url=https://www.sciencedirect.com"), "Strong nuclear engineering and energy full text."),
1254
+ ("AIP Publishing", _safe_db_url("https://khalifa.idm.oclc.org/login?url=https://pubs.aip.org"), "Nuclear and applied physics crossover."),
1255
+ ("Knovel", _safe_db_url("https://khalifa.idm.oclc.org/login?url=https://app.knovel.com"), "Engineering reference and handbooks."),
1256
+ ("Scopus", _safe_db_url("https://khalifa.idm.oclc.org/login?url=https://www.scopus.com"), "Discovery and citation tracking."),
1257
+ ("Web of Science", _safe_db_url("https://khalifa.idm.oclc.org/login?url=https://www.webofscience.com"), "Broad citation discovery."),
1258
+ ],
1259
+ "quantum_computing": [
1260
+ ("IEEE Xplore", _safe_db_url("https://khalifa.idm.oclc.org/login?url=https://ieeexplore.ieee.org"), "Quantum computing engineering implementations."),
1261
+ ("ACM Digital Library", _safe_db_url("https://khalifa.idm.oclc.org/login?url=https://dl.acm.org"), "Algorithms, theory, and computing literature."),
1262
+ ("APS Journals", _safe_db_url("https://khalifa.idm.oclc.org/login?url=https://journals.aps.org"), "Quantum information and foundational physics."),
1263
+ ("arXiv", "https://arxiv.org", "Essential for current quantum computing preprints."),
1264
+ ("Scopus", _safe_db_url("https://khalifa.idm.oclc.org/login?url=https://www.scopus.com"), "Discovery and citation tracking."),
1265
+ ],
1266
+ "biomedical": [
1267
+ ("PubMed", _safe_db_url("https://khalifa.idm.oclc.org/login?url=https://pubmed.ncbi.nlm.nih.gov"), "Core biomedical and life sciences literature."),
1268
+ ("IEEE Xplore", _safe_db_url("https://khalifa.idm.oclc.org/login?url=https://ieeexplore.ieee.org"), "Biomedical engineering and medical devices."),
1269
+ ("Embase", _safe_db_url("https://khalifa.idm.oclc.org/login?url=https://www.embase.com"), "Biomedical and pharmacological literature."),
1270
+ ("ScienceDirect", _safe_db_url("https://khalifa.idm.oclc.org/login?url=https://www.sciencedirect.com"), "Full text for biomedical engineering and applied sciences."),
1271
+ ("Scopus", _safe_db_url("https://khalifa.idm.oclc.org/login?url=https://www.scopus.com"), "Discovery and citation tracking."),
1272
+ ],
1273
+ "biology": [
1274
+ ("PubMed", _safe_db_url("https://khalifa.idm.oclc.org/login?url=https://pubmed.ncbi.nlm.nih.gov"), "Core life sciences and biomedical literature."),
1275
+ ("ScienceDirect", _safe_db_url("https://khalifa.idm.oclc.org/login?url=https://www.sciencedirect.com"), "Full text biology and molecular sciences content."),
1276
+ ("Embase", _safe_db_url("https://khalifa.idm.oclc.org/login?url=https://www.embase.com"), "Biomedical crossover topics."),
1277
+ ("PubMed Central", "https://www.ncbi.nlm.nih.gov/pmc", "Open full-text biomedical articles β€” no login required."),
1278
+ ("Scopus", _safe_db_url("https://khalifa.idm.oclc.org/login?url=https://www.scopus.com"), "Discovery and citation tracking."),
1279
+ ],
1280
+ "medical": [
1281
+ ("PubMed @ KU", "http://www.ncbi.nlm.nih.gov/pubmed/?otool=iaekustlib", "Core medical literature β€” use this KU link for full-text access."),
1282
+ ("Embase", _safe_db_url("https://khalifa.idm.oclc.org/login?url=https://www.embase.com"), "Biomedical and pharmacological literature."),
1283
+ ("CINAHL Ultimate", _safe_db_url("https://khalifa.idm.oclc.org/login?url=https://search.ebscohost.com/login.aspx?authtype=ip,uid&profile=ehost&defaultdb=cul"), "Nursing and allied health literature."),
1284
+ ("Cochrane Library", _safe_db_url("https://khalifa.idm.oclc.org/login?url=https://www.cochranelibrary.com"), "Evidence synthesis and systematic reviews β€” gold standard."),
1285
+ ("UpToDate", _safe_db_url("https://khalifa.idm.oclc.org/login?url=https://www.uptodate.com"), "Point-of-care clinical decision support."),
1286
+ ("ClinicalKey Student", _safe_db_url("https://khalifa.idm.oclc.org/login?url=https://www.clinicalkey.com/student/login"), "Clinical reference and learning resource."),
1287
+ ],
1288
+ "business": [
1289
+ ("Business Source Complete", _safe_db_url("https://khalifa.idm.oclc.org/login?url=https://search.ebscohost.com"), "Core business and management literature."),
1290
+ ("Emerald Insight", _safe_db_url("https://khalifa.idm.oclc.org/login?url=https://www.emerald.com/insight"), "Management, strategy, and leadership journals."),
1291
+ ("ProQuest Databases", _safe_db_url("https://khalifa.idm.oclc.org/login?url=https://search.proquest.com"), "Broad business and interdisciplinary discovery."),
1292
+ ("JSTOR", _safe_db_url("https://khalifa.idm.oclc.org/login?url=https://www.jstor.org"), "Business, economics, and policy literature."),
1293
+ ("Scopus", _safe_db_url("https://khalifa.idm.oclc.org/login?url=https://www.scopus.com"), "Broad discovery and citation tracking."),
1294
+ ],
1295
+ "earth_science": [
1296
+ ("GeoscienceWorld", _safe_db_url("https://khalifa.idm.oclc.org/login?url=https://pubs.geoscienceworld.org"), "Core earth science journals and conference content."),
1297
+ ("EarthDoc", _safe_db_url("https://khalifa.idm.oclc.org/login?url=https://www.earthdoc.org"), "Geoscience and subsurface literature."),
1298
+ ("ScienceDirect", _safe_db_url("https://khalifa.idm.oclc.org/login?url=https://www.sciencedirect.com"), "Full text for geoscience and planetary topics."),
1299
+ ("Scopus", _safe_db_url("https://khalifa.idm.oclc.org/login?url=https://www.scopus.com"), "Discovery and citation tracking."),
1300
+ ("Web of Science", _safe_db_url("https://khalifa.idm.oclc.org/login?url=https://www.webofscience.com"), "Broad citation discovery."),
1301
+ ],
1302
+ "environment": [
1303
+ ("ScienceDirect", _safe_db_url("https://khalifa.idm.oclc.org/login?url=https://www.sciencedirect.com"), "Strong sustainability and environmental full text."),
1304
+ ("ProQuest Databases", _safe_db_url("https://khalifa.idm.oclc.org/login?url=https://search.proquest.com"), "Environmental policy and interdisciplinary discovery."),
1305
+ ("Web of Science", _safe_db_url("https://khalifa.idm.oclc.org/login?url=https://www.webofscience.com"), "Broad citation discovery."),
1306
+ ("Scopus", _safe_db_url("https://khalifa.idm.oclc.org/login?url=https://www.scopus.com"), "Broad discovery and citation tracking."),
1307
+ ("Business Source Complete", _safe_db_url("https://khalifa.idm.oclc.org/login?url=https://search.ebscohost.com"), "Sustainability, policy, and corporate responsibility."),
1308
+ ],
1309
+ "engineering_management": [
1310
+ ("Scopus", _safe_db_url("https://khalifa.idm.oclc.org/login?url=https://www.scopus.com"), "Broad discovery across engineering and management research."),
1311
+ ("Emerald Insight", _safe_db_url("https://khalifa.idm.oclc.org/login?url=https://www.emerald.com/insight"), "Management, operations, and leadership literature."),
1312
+ ("ScienceDirect", _safe_db_url("https://khalifa.idm.oclc.org/login?url=https://www.sciencedirect.com"), "Engineering and management full text."),
1313
+ ("Business Source Complete", _safe_db_url("https://khalifa.idm.oclc.org/login?url=https://search.ebscohost.com"), "Business, management, and strategy literature."),
1314
+ ("ProQuest Databases", _safe_db_url("https://khalifa.idm.oclc.org/login?url=https://search.proquest.com"), "Broad business and interdisciplinary discovery."),
1315
+ ],
1316
+ "metrics": [
1317
+ ("Journal Citation Reports", _safe_db_url("https://khalifa.idm.oclc.org/login?url=https://jcr.clarivate.com"), "Impact factors and journal rankings (Web of Science)."),
1318
+ ("Scopus / CiteScore", _safe_db_url("https://khalifa.idm.oclc.org/login?url=https://www.scopus.com"), "CiteScore and source-level metrics."),
1319
+ ("SciVal", _safe_db_url("https://khalifa.idm.oclc.org/login?url=https://www.scival.com"), "Research performance analytics."),
1320
+ ("Web of Science", _safe_db_url("https://khalifa.idm.oclc.org/login?url=https://www.webofscience.com"), "Citation discovery and journal evaluation context."),
1321
+ ],
1322
+ "theses": [
1323
+ ("ProQuest Dissertations & Theses Global", _safe_db_url("https://khalifa.idm.oclc.org/login?url=https://www.proquest.com/pqdtglobal"), "International dissertations and theses."),
1324
+ ("Khazna", "https://khazna.ku.ac.ae/", "KU institutional repository and research outputs."),
1325
+ ("PRIMO", "https://khalifa.primo.exlibrisgroup.com/discovery/search?vid=971KUOSTAR_INST:KU", "Discovery for dissertations, theses, and KU holdings."),
1326
+ ],
1327
+ }
1328
+
1329
+ # General / fallback databases (used when no subject match found)
1330
+ _DB_GENERAL: List[Tuple[str, str, str]] = [
1331
+ ("PRIMO (KU Library Discovery)", "https://khalifa.primo.exlibrisgroup.com/discovery/search?vid=971KUOSTAR_INST:KU", "Best starting point β€” search all KU books, articles, and journals."),
1332
+ ("Scopus", _safe_db_url("https://khalifa.idm.oclc.org/login?url=https://www.scopus.com"), "Largest abstract and citation database β€” all disciplines."),
1333
+ ("Web of Science", _safe_db_url("https://khalifa.idm.oclc.org/login?url=https://www.webofscience.com"), "Premier citation index β€” all disciplines."),
1334
+ ("ScienceDirect", _safe_db_url("https://khalifa.idm.oclc.org/login?url=https://www.sciencedirect.com"), "Large full-text platform across science, engineering, and medicine."),
1335
+ ("SpringerLink", _safe_db_url("https://khalifa.idm.oclc.org/login?url=https://link.springer.com"), "Springer journals and books across all disciplines."),
1336
+ ("ProQuest Databases", _safe_db_url("https://khalifa.idm.oclc.org/login?url=https://search.proquest.com"), "Broad multidisciplinary discovery."),
1337
+ ("Emerald Insight", _safe_db_url("https://khalifa.idm.oclc.org/login?url=https://www.emerald.com/insight"), "Social sciences, management, and interdisciplinary research."),
1338
+ ("JSTOR", _safe_db_url("https://khalifa.idm.oclc.org/login?url=https://www.jstor.org"), "Foundational and archival literature across many fields."),
1339
+ ]
1340
+
1341
+
1342
+ def _match_db_subject(q: str) -> Optional[str]:
1343
+ """Map lowercased question to a _DB_SUBJECTS key using keyword matching."""
1344
+ checks = [
1345
+ (["quantum computing", "quantum algorithm", "quantum cryptography", "quantum information"], "quantum_computing"),
1346
+ (["robotics", "autonomous system", "intelligent system"], "robotics"),
1347
+ (["artificial intelligence", "machine learning", "deep learning", "nlp", "natural language processing", "neural network", "cybersecurity", "computer science", "software engineering", "algorithm", "data structure"], "computer_science"),
1348
+ (["computer engineering", "embedded system", "computer architecture", "networking", "digital system"], "computer_science"),
1349
+ (["aerospace", "aerodynamic", "propulsion", "aircraft", "aviation", "flight system"], "aerospace"),
1350
+ (["biomedical engineering", "medical device", "biomechanic", "bioinstrument", "tissue engineering", "biomedical imaging"], "biomedical"),
1351
+ (["chemical engineering", "process engineering", "reaction engineering", "separation", "catalysis"], "chemical_engineering"),
1352
+ (["chemistry", "organic chemistry", "analytical chemistry", "inorganic", "chemical", "chemical properties"], "chemistry"),
1353
+ (["civil engineering", "structural engineering", "geotechnical", "transportation engineering", "water resources", "construction"], "civil_engineering"),
1354
+ (["electrical engineering", "circuit", "power system", "signal processing", "control system", "communication system", "electronics"], "electrical_engineering"),
1355
+ (["energy engineering", "renewable energy", "sustainable energy", "clean energy", "energy transition", "solar", "wind power"], "energy"),
1356
+ (["engineering management", "engineering systems", "systems engineering", "operations management", "innovation management"], "engineering_management"),
1357
+ (["mechanical engineering", "thermofluid", "thermodynamics", "fluid mechanics", "manufacturing", "mechatronics"], "mechanical_engineering"),
1358
+ (["nuclear engineering", "reactor", "radiation", "nuclear material", "nuclear safety"], "nuclear"),
1359
+ (["petroleum engineering", "oil and gas", "drilling", "reservoir", "subsurface", "petroleum"], "petroleum"),
1360
+ (["earth science", "geology", "geophysics", "geochemistry", "planetary science", "geoscience"], "earth_science"),
1361
+ (["environment", "sustainability", "climate change", "sustainable development", "conservation", "environmental policy", "ecology"], "environment"),
1362
+ (["materials science", "nanotechnology", "nanomaterial", "biomaterial", "advanced material", "functional material"], "materials"),
1363
+ (["cell biology", "molecular biology", "genetics", "genomics", "biotechnology", "bioinformatics", "microbiology", "virology"], "biology"),
1364
+ (["medicine", "medical", "health science", "clinical", "nursing", "pharmacy", "biomedical literature", "pharmacology", "clinical trial", "systematic review", "evidence-based", "patient care"], "medical"),
1365
+ (["business", "management", "finance", "economics", "accounting", "entrepreneurship", "marketing", "strategy", "leadership", "international relations", "political science", "social science", "humanities", "law", "education", "psychology", "sociology"], "business"),
1366
+ (["physics", "optics", "photonics", "quantum physics", "condensed matter", "theoretical physics", "astrophysics"], "physics"),
1367
+ (["mathematics", "statistics", "data science", "calculus", "algebra", "optimization", "probability", "mathematical model"], "mathematics"),
1368
+ (["impact factor", "journal metrics", "citescore", "jcr", "quartile", "research impact", "scival", "bibliometric"], "metrics"),
1369
+ (["dissertation", "thesis", "doctoral", "master thesis", "repository", "phd thesis"], "theses"),
1370
+ ]
1371
+ for keywords, key in checks:
1372
+ if any(kw in q for kw in keywords):
1373
+ return key
1374
+ return None
1375
+
1376
+
1377
  def _extract_db_topic(question: str) -> str:
1378
  """
1379
  Extract the subject from a database-recommendation question.
 
1403
 
1404
  def _database_recommendation_answer(question: str) -> str:
1405
  q = (question or "").lower()
 
1406
  subject = _extract_db_topic(question)
1407
  topic = _escape(subject)
1408
+
1409
+ # Match to a specific subject list; fall back to general KU databases
1410
+ subject_key = _match_db_subject(q)
1411
+ dbs = _DB_SUBJECTS.get(subject_key, _DB_GENERAL) if subject_key else _DB_GENERAL
1412
+
1413
+ # Cap at 5 results for readability
1414
+ dbs = list(dbs)[:5]
1415
+
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1416
  answer = f"<strong>Recommended databases for {topic}</strong><br><br>"
1417
+ answer += "Here are the strongest KU-subscribed starting points:<br>"
1418
  for name, url, why in dbs:
1419
  answer += f'<br>β€’ <a href="{url}" target="_blank"><strong>{_escape(name)}</strong></a> β€” {_escape(why)}'
1420
+
1421
+ if not subject_key:
1422
+ # Generic topic β€” add a note that subject-specific guides exist
1423
+ answer += (
1424
+ "<br><br>πŸ’‘ For subject-specific databases, ask me: "
1425
+ "<em>\"best databases for [your subject]\"</em> β€” e.g. physics, chemistry, medicine, civil engineering."
1426
+ )
1427
+
1428
+ answer += (
1429
+ f'<br><br>Browse all 50+ KU databases: '
1430
+ f'<a href="https://library.ku.ac.ae/eresources" target="_blank">library.ku.ac.ae/eresources</a>'
1431
+ )
1432
+ answer += "<br><br>Want me to run a direct search on any of these for a specific topic?"
1433
  return answer
1434
 
1435