Spaces:
Runtime error
Runtime error
File size: 12,112 Bytes
5095870 739d04b 5095870 4e6aab3 5095870 739d04b 4e6aab3 739d04b c9f6853 4e6aab3 c9f6853 5095870 4e6aab3 5095870 4e6aab3 5095870 | 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 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 | """
Citation formatters: Vancouver, APA 7th edition, and AMA style.
Vancouver is the standard for UK nursing and medical publications.
APA 7th is used by some UK university nursing programmes.
"""
# ---------------------------------------------------------------------------
# APA 7th Edition
# ---------------------------------------------------------------------------
def format_apa7(article: dict) -> str:
"""
APA 7th edition journal article format:
Author, F. M., & Author, F. M. (Year). Title of article.
*Journal Name*, *Volume*(Issue), Pages. https://doi.org/xxxxx
"""
authors_str = _apa_authors(article.get("authors", []))
year = article.get("year") or "n.d."
title = _clean_title(article.get("title", ""))
journal = article.get("journal", "")
volume = article.get("volume", "")
issue = article.get("issue", "")
pages = article.get("pages", "")
doi = _clean_doi(article.get("doi", ""))
url = article.get("url", "")
citation = f"{authors_str} ({year}). {title}."
if journal:
citation += f" *{journal}*"
if volume:
citation += f", *{volume}*"
if issue:
citation += f"({issue})"
if pages:
citation += f", {pages}"
citation += "."
if doi:
citation += f" https://doi.org/{doi}"
elif url:
citation += f" {url}"
return citation
def _apa_authors(authors: list[str]) -> str:
if not authors:
return "Unknown Author"
formatted = [_apa_single(a) for a in authors[:20]]
# APA 7: 21+ authors → first 19, ellipsis, last author
if len(authors) > 20:
last = _apa_single(authors[-1])
return ", ".join(formatted[:19]) + ", . . . " + last
if len(formatted) == 1:
return formatted[0]
if len(formatted) == 2:
return f"{formatted[0]}, & {formatted[1]}"
return ", ".join(formatted[:-1]) + f", & {formatted[-1]}"
def _apa_single(name: str) -> str:
name = name.strip()
if name.lower() in ("et al.", "et al"):
return "et al."
if "," in name:
# "Last, First M." → keep as-is, just ensure initials
last, *rest = name.split(",", 1)
initials = _initials(" ".join(rest))
return f"{last.strip()}, {initials}"
parts = name.split()
if len(parts) >= 2:
# Detect PubMed/Europe PMC "Surname JK" format
if _is_initials_token(parts[-1]):
last = " ".join(parts[:-1]) # handles "van der Berg JK"
initials = _initials_from_str(parts[-1])
return f"{last}, {initials}"
last = parts[-1]
initials = _initials(" ".join(parts[:-1]))
return f"{last}, {initials}"
return name
# ---------------------------------------------------------------------------
# Vancouver Style (UK nursing / medical standard)
# ---------------------------------------------------------------------------
def format_vancouver(article: dict) -> str:
"""
Vancouver citation format — standard for UK nursing and medical journals
(Nursing Standard, BJN, JAN, BMJ, Lancet, NEJM etc.)
Format: Surname Initial(s), Surname Initial(s). Title. Journal Abbrev.
Year;Volume(Issue):Pages. doi:xxxxx
Up to 6 authors; if more, list first 6 then 'et al.'
"""
authors_str = _vancouver_authors(article.get("authors", []))
year = article.get("year", "")
title = _clean_title(article.get("title", ""))
journal = article.get("journal", "")
volume = article.get("volume", "")
issue = article.get("issue", "")
pages = article.get("pages", "")
doi = _clean_doi(article.get("doi", ""))
url = article.get("url", "")
# Vancouver: no period after title if journal follows immediately
citation = f"{authors_str}. {title}."
if journal:
citation += f" {journal}."
if year:
citation += f" {year}"
if volume:
citation += f";{volume}"
if issue:
citation += f"({issue})"
if pages:
citation += f":{pages}"
citation += "."
if doi:
citation += f" doi:{doi}"
elif url:
citation += f" Available from: {url}"
return citation
def _vancouver_authors(authors: list[str]) -> str:
if not authors:
return "Unknown Author"
formatted = [_vancouver_single(a) for a in authors[:6]]
if len(authors) > 6:
formatted.append("et al")
return ", ".join(formatted)
def _vancouver_single(name: str) -> str:
"""Convert name to Vancouver format: Surname AB"""
name = name.strip()
if name.lower() in ("et al.", "et al"):
return "et al"
if "," in name:
last, *rest = name.split(",", 1)
initials = "".join(p[0].upper() for p in " ".join(rest).split() if p)
return f"{last.strip()} {initials}"
parts = name.split()
if len(parts) >= 2:
# Detect PubMed/Europe PMC "Surname JK" format
if _is_initials_token(parts[-1]):
last = " ".join(parts[:-1])
initials = parts[-1].upper() # already run-together: "JK" stays "JK"
return f"{last} {initials}"
last = parts[-1]
initials = "".join(p[0].upper() for p in parts[:-1] if p)
return f"{last} {initials}"
return name
# ---------------------------------------------------------------------------
# Harvard Style (Cite Them Right — common UK university nursing standard)
# ---------------------------------------------------------------------------
def format_harvard(article: dict) -> str:
"""
Harvard citation format (Cite Them Right 12th ed.) — used by many UK
universities for nursing essays where Vancouver is not mandated.
Format: Surname, Initial(s). and Surname, Initial(s). (Year) 'Title of
article', *Journal Name*, Volume(Issue), pp. Pages.
Up to 3 authors listed; 4+ → first author et al.
"""
authors_str = _harvard_authors(article.get("authors", []))
year = article.get("year") or "n.d."
title = _clean_title(article.get("title", ""))
journal = article.get("journal", "")
volume = article.get("volume", "")
issue = article.get("issue", "")
pages = article.get("pages", "")
doi = _clean_doi(article.get("doi", ""))
url = article.get("url", "")
citation = f"{authors_str} ({year}) '{title}',"
if journal:
citation += f" *{journal}*,"
if volume:
citation += f" {volume}"
if issue:
citation += f"({issue})"
citation += ","
if pages:
citation += f" pp. {pages}."
else:
citation = citation.rstrip(",") + "."
else:
citation = citation.rstrip(",") + "."
if doi:
citation += f" doi:{doi}"
elif url:
citation += f" Available at: {url} (Accessed: {_access_year(year)})"
return citation
def _harvard_authors(authors: list[str]) -> str:
if not authors:
return "Anon."
if len(authors) > 3:
return _harvard_single(authors[0]) + " et al."
formatted = [_harvard_single(a) for a in authors]
if len(formatted) == 1:
return formatted[0]
return ", ".join(formatted[:-1]) + " and " + formatted[-1]
def _harvard_single(name: str) -> str:
"""Convert to Harvard format: Surname, A.B."""
name = name.strip()
if name.lower() in ("et al.", "et al"):
return "et al."
if "," in name:
last, *rest = name.split(",", 1)
initials = _initials(" ".join(rest))
return f"{last.strip()}, {initials}"
parts = name.split()
if len(parts) >= 2:
# Detect PubMed/Europe PMC "Surname JK" format (e.g. "Smith JK")
# Without this check, "Smith JK" → last="JK", initials="S." → "JK, S." (wrong)
if _is_initials_token(parts[-1]):
last = " ".join(parts[:-1]) # handles multi-word surnames too
initials = _initials_from_str(parts[-1]) # "JK" → "J. K."
return f"{last}, {initials}"
last = parts[-1]
initials = _initials(" ".join(parts[:-1]))
return f"{last}, {initials}"
return name
def _access_year(year: str) -> str:
"""Return a sensible access date placeholder."""
return "date accessed"
# ---------------------------------------------------------------------------
# AMA Style
# ---------------------------------------------------------------------------
def format_ama(article: dict) -> str:
"""
AMA citation format (used in some nursing / medical journals):
Last FM, Last FM. Title. Journal. Year;Vol(Issue):Pages. doi:xxxxx
"""
authors_str = _ama_authors(article.get("authors", []))
year = article.get("year", "")
title = _clean_title(article.get("title", ""))
journal = article.get("journal", "")
volume = article.get("volume", "")
issue = article.get("issue", "")
pages = article.get("pages", "")
doi = _clean_doi(article.get("doi", ""))
citation = f"{authors_str}. {title}. *{journal}*."
if year:
citation += f" {year}"
if volume:
citation += f";{volume}"
if issue:
citation += f"({issue})"
if pages:
citation += f":{pages}"
citation += "."
if doi:
citation += f" doi:{doi}"
return citation
def _ama_authors(authors: list[str]) -> str:
if not authors:
return "Unknown Author"
formatted = [_ama_single(a) for a in authors[:6]]
if len(authors) > 6:
formatted.append("et al")
return ", ".join(formatted)
def _ama_single(name: str) -> str:
name = name.strip()
if name.lower() in ("et al.", "et al", "et al."):
return "et al"
if "," in name:
last, *rest = name.split(",", 1)
initials = "".join(p[0].upper() for p in " ".join(rest).split() if p)
return f"{last.strip()} {initials}"
parts = name.split()
if len(parts) >= 2:
# Detect PubMed/Europe PMC "Surname JK" format
if _is_initials_token(parts[-1]):
last = " ".join(parts[:-1])
initials = parts[-1].upper() # AMA uses run-together: "Smith JK"
return f"{last} {initials}"
last = parts[-1]
initials = "".join(p[0].upper() for p in parts[:-1] if p)
return f"{last} {initials}"
return name
# ---------------------------------------------------------------------------
# Shared helpers
# ---------------------------------------------------------------------------
def _initials(first_middle: str) -> str:
"""Convert first/middle name string to initials: "John Paul" → "J. P." """
parts = first_middle.strip().split()
return " ".join(p[0].upper() + "." for p in parts if p)
def _initials_from_str(token: str) -> str:
"""
Expand a run-together initials token from PubMed/Europe PMC into dotted
initials: "JK" → "J. K." | "AB" → "A. B." | "J" → "J."
"""
return " ".join(c.upper() + "." for c in token if c.isalpha())
def _is_initials_token(token: str) -> bool:
"""
Return True when a name token looks like run-together initials rather than
a proper word — i.e. it is 1–4 uppercase letters with no vowels OR is
entirely uppercase and short.
Examples that return True : "JK", "AB", "J", "JKL", "ABCD"
Examples that return False: "Smith", "John", "van", "De", "O'Brien"
"""
if not token or not token.isalpha():
return False
if len(token) > 4:
return False
# Single letter is always an initial
if len(token) == 1:
return token.isupper()
# Multi-char: must be all-uppercase (PubMed stores "Smith JK", not "Smith jk")
return token.isupper()
def _clean_title(title: str) -> str:
return title.strip().rstrip(".")
def _clean_doi(doi: str) -> str:
doi = doi.strip()
for prefix in ("https://doi.org/", "http://doi.org/", "doi:", "doi: "):
if doi.lower().startswith(prefix):
doi = doi[len(prefix):]
return doi
|