author_query_match.py 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738
  1. """作者匹配工具 —— 姓名标准化、模糊匹配与作者去重."""
  2. from __future__ import annotations
  3. from .common import text_has_cjk
  4. def normalize_author_names(raw: list[str | None]) -> list[str]:
  5. """Deduplicate parsed author names."""
  6. seen: set[str] = set()
  7. out: list[str] = []
  8. for x in raw or []:
  9. s = str(x or "").strip()
  10. if not s or s.lower() in seen:
  11. continue
  12. seen.add(s.lower())
  13. out.append(s)
  14. if len(out) >= 8:
  15. break
  16. return out
  17. def pick_primary_english_author_for_query(authors: list[str]) -> str | None:
  18. """Pick the first Latin-script author for API queries."""
  19. for a in authors:
  20. s = str(a).strip()
  21. if s and not text_has_cjk(s):
  22. return s
  23. return None
  24. def is_author_centric_search(query: str, authors: list[str | None]) -> bool:
  25. """Fast check for author-only searches."""
  26. auth = normalize_author_names([str(x) for x in (authors or []) if str(x).strip()])
  27. return bool(auth) and not bool((query or "").strip())
  28. def author_phrase_matches_canonical_line(line: str, phrase: str) -> bool:
  29. """Match a normalized author phrase against a paper author list."""
  30. pl = (line or "").strip().lower()
  31. ph = (phrase or "").strip().lower()
  32. return bool(pl and ph) and ph in pl