web-source-evidence.ts 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. const MAX_EVIDENCE_CHARS = 200_000;
  2. /**
  3. * Find demonstrable reuse, not semantic relevance. Match substantive lines
  4. * and complete clauses after excluding code, links and standalone values.
  5. * Index all competing evidence once so shared text cannot attribute every hit.
  6. * Callers only use these IDs for Web sources; App/File policies stay separate.
  7. */
  8. export function collectUsedWebEvidenceIds(
  9. answer: string,
  10. evidenceById: ReadonlyMap<string, string>,
  11. ): ReadonlySet<string> {
  12. const lineOwners = new Map<string, string | null>();
  13. const clauseOwners = new Map<string, string | null>();
  14. for (const [id, evidence] of evidenceById) {
  15. const bounded = comparableProse(evidence);
  16. for (const line of bounded.split(/\r?\n/u)) {
  17. const candidate = line.trim();
  18. if (isSubstantiveText(candidate)) addOwner(lineOwners, candidate, id);
  19. }
  20. for (const clause of comparableClauses(bounded)) addOwner(clauseOwners, clause, id);
  21. }
  22. const used = new Set<string>();
  23. const answerProse = comparableProse(answer);
  24. for (const [line, owner] of lineOwners) {
  25. if (owner && answerProse.includes(line)) used.add(owner);
  26. }
  27. for (const clause of comparableClauses(answerProse)) {
  28. const owner = clauseOwners.get(clause);
  29. if (owner) used.add(owner);
  30. }
  31. return used;
  32. }
  33. function addOwner(owners: Map<string, string | null>, text: string, id: string): void {
  34. if (!owners.has(text)) owners.set(text, id);
  35. else if (owners.get(text) !== id) owners.set(text, null);
  36. }
  37. function comparableProse(text: string): string {
  38. return (
  39. text
  40. .slice(0, MAX_EVIDENCE_CHARS)
  41. .normalize('NFKC')
  42. .replace(/```[\s\S]*?```|~~~[\s\S]*?~~~/gu, '\n')
  43. .replace(/<(script|style)\b[^>]*>[\s\S]*?<\/\1\s*>/giu, '\n')
  44. .replace(/<\/?(?:p|div|li|br|h[1-6])\b[^>]*>/giu, '\n')
  45. .replace(/<[^>]*>/gu, '')
  46. // Link labels/navigation and URL substrings are not evidence of adoption.
  47. .replace(/!?\[[^\]\n]*\]\([^\n)]*\)/gu, '')
  48. .replace(/https?:\/\/[^\s<>]+/giu, '')
  49. );
  50. }
  51. function comparableClauses(text: string): string[] {
  52. return (
  53. text
  54. .split(/[\n。!?;,!?;]+|,(?!\d)|\.(?=\s|$)/u)
  55. .map((clause) => clause.trim().replace(/^(?:[-+*]|\d+[.)]|#{1,6}|>)\s+/u, ''))
  56. .map((clause) => clause.replace(/[*_`]/gu, '').trim())
  57. .map((clause) =>
  58. /\p{Script=Han}/u.test(clause) ? clause.replace(/\s+/gu, '') : clause.replace(/\s+/gu, ' '),
  59. )
  60. // Never infer adoption from a bare temperature/date, a short topic word,
  61. // or an arbitrary substring. Negation and wording are kept intact.
  62. .filter(isSubstantiveText)
  63. );
  64. }
  65. function isSubstantiveText(text: string): boolean {
  66. const hanCount = text.match(/\p{Script=Han}/gu)?.length ?? 0;
  67. return (
  68. text.length <= 500 &&
  69. (hanCount >= 7 ||
  70. (hanCount === 0 && text.length >= 24 && (text.match(/\p{L}{2,}/gu)?.length ?? 0) >= 4))
  71. );
  72. }