File size: 19,373 Bytes
46df5f0 39b06be 46df5f0 39b06be 46df5f0 39b06be 46df5f0 39b06be 46df5f0 39b06be 46df5f0 39b06be 46df5f0 39b06be 46df5f0 39b06be 46df5f0 39b06be 46df5f0 39b06be 46df5f0 39b06be 46df5f0 39b06be 46df5f0 39b06be 46df5f0 39b06be 46df5f0 39b06be 46df5f0 39b06be 46df5f0 39b06be 46df5f0 39b06be 46df5f0 |
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 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 |
"""
Metadata comparison between bib entries and fetched metadata.
"""
from dataclasses import dataclass
from typing import Optional
from ..parsers.bib_parser import BibEntry
from ..fetchers.arxiv_fetcher import ArxivMetadata
from ..fetchers.scholar_fetcher import ScholarResult
from ..fetchers.crossref_fetcher import CrossRefResult
from ..fetchers.semantic_scholar_fetcher import SemanticScholarResult
from ..fetchers.openalex_fetcher import OpenAlexResult
from ..fetchers.dblp_fetcher import DBLPResult
from ..utils.normalizer import TextNormalizer
@dataclass
class ComparisonResult:
"""Result of comparing bib entry with fetched metadata."""
entry_key: str
# Title comparison
title_match: bool
title_similarity: float
bib_title: str
fetched_title: str
# Author comparison
author_match: bool
author_similarity: float
bib_authors: list[str]
fetched_authors: list[str]
# Year comparison
year_match: bool
bib_year: str
fetched_year: str
# Overall assessment
is_match: bool
confidence: float
issues: list[str]
source: str # 'arxiv', 'crossref', 'scholar', 'semantic_scholar', 'openalex', 'dblp', or 'unable'
@property
def has_issues(self) -> bool:
return len(self.issues) > 0
class MetadataComparator:
"""Compares bibliography entries with fetched metadata."""
# Thresholds for matching
TITLE_THRESHOLD = 0.8
AUTHOR_THRESHOLD = 0.6
def __init__(self):
self.normalizer = TextNormalizer
def compare_with_arxiv(self, bib_entry: BibEntry, arxiv_meta: ArxivMetadata) -> ComparisonResult:
"""Compare bib entry with arXiv metadata."""
issues = []
# Compare titles
bib_title_norm = self.normalizer.normalize_for_comparison(bib_entry.title)
arxiv_title_norm = self.normalizer.normalize_for_comparison(arxiv_meta.title)
title_similarity = self.normalizer.similarity_ratio(bib_title_norm, arxiv_title_norm)
# Also try Levenshtein for short titles
if len(bib_title_norm) < 100:
lev_sim = self.normalizer.levenshtein_similarity(bib_title_norm, arxiv_title_norm)
title_similarity = max(title_similarity, lev_sim)
title_match = title_similarity >= self.TITLE_THRESHOLD
if not title_match:
issues.append(f"Title mismatch. Bib: '{bib_entry.title}', Retrieved: '{arxiv_meta.title}'")
# Compare authors
bib_authors = self.normalizer.normalize_author_list(bib_entry.author)
arxiv_authors = [self.normalizer.normalize_author_name(a) for a in arxiv_meta.authors]
author_similarity = self._compare_author_lists(bib_authors, arxiv_authors)
author_match = author_similarity >= self.AUTHOR_THRESHOLD
if not author_match:
issues.append(f"Author mismatch. Bib: {', '.join(bib_authors)}, Retrieved: {', '.join(arxiv_authors)}")
# Compare years
bib_year = bib_entry.year.strip()
arxiv_year = arxiv_meta.year
year_match = bib_year == arxiv_year
if not year_match and bib_year and arxiv_year:
issues.append(f"Year mismatch. Bib: {bib_year}, Retrieved: {arxiv_year}")
# Overall assessment
is_match = title_match and author_match
confidence = (title_similarity * 0.5 + author_similarity * 0.3 + (1.0 if year_match else 0.5) * 0.2)
return ComparisonResult(
entry_key=bib_entry.key,
title_match=title_match,
title_similarity=title_similarity,
bib_title=bib_entry.title,
fetched_title=arxiv_meta.title,
author_match=author_match,
author_similarity=author_similarity,
bib_authors=bib_authors,
fetched_authors=arxiv_authors,
year_match=year_match,
bib_year=bib_year,
fetched_year=arxiv_year,
is_match=is_match,
confidence=confidence,
issues=issues,
source="arxiv"
)
def compare_with_scholar(self, bib_entry: BibEntry, scholar_result: ScholarResult) -> ComparisonResult:
"""Compare bib entry with Scholar search result."""
issues = []
# Compare titles
bib_title_norm = self.normalizer.normalize_for_comparison(bib_entry.title)
scholar_title_norm = self.normalizer.normalize_for_comparison(scholar_result.title)
title_similarity = self.normalizer.similarity_ratio(bib_title_norm, scholar_title_norm)
if len(bib_title_norm) < 100:
lev_sim = self.normalizer.levenshtein_similarity(bib_title_norm, scholar_title_norm)
title_similarity = max(title_similarity, lev_sim)
title_match = title_similarity >= self.TITLE_THRESHOLD
if not title_match:
issues.append(f"Title mismatch. Bib: '{bib_entry.title}', Retrieved: '{scholar_result.title}'")
# Compare authors (Scholar format is less structured)
bib_authors = self.normalizer.normalize_author_list(bib_entry.author)
# Scholar authors are comma-separated
scholar_authors_raw = scholar_result.authors.split(',')
scholar_authors = [self.normalizer.normalize_author_name(a.strip()) for a in scholar_authors_raw]
author_similarity = self._compare_author_lists(bib_authors, scholar_authors)
author_match = author_similarity >= self.AUTHOR_THRESHOLD
if not author_match:
issues.append(f"Author mismatch. Bib: {', '.join(bib_authors)}, Retrieved: {', '.join(scholar_authors)}")
# Compare years
bib_year = bib_entry.year.strip()
scholar_year = scholar_result.year
year_match = bib_year == scholar_year
if not year_match and bib_year and scholar_year:
issues.append(f"Year mismatch. Bib: {bib_year}, Retrieved: {scholar_year}")
# Overall assessment
is_match = title_match and author_match
confidence = (title_similarity * 0.5 + author_similarity * 0.3 + (1.0 if year_match else 0.5) * 0.2)
return ComparisonResult(
entry_key=bib_entry.key,
title_match=title_match,
title_similarity=title_similarity,
bib_title=bib_entry.title,
fetched_title=scholar_result.title,
author_match=author_match,
author_similarity=author_similarity,
bib_authors=bib_authors,
fetched_authors=scholar_authors,
year_match=year_match,
bib_year=bib_year,
fetched_year=scholar_year,
is_match=is_match,
confidence=confidence,
issues=issues,
source="scholar"
)
def compare_with_crossref(self, bib_entry: BibEntry, crossref_result: CrossRefResult) -> ComparisonResult:
"""Compare bib entry with CrossRef search result."""
issues = []
# Compare titles
bib_title_norm = self.normalizer.normalize_for_comparison(bib_entry.title)
crossref_title_norm = self.normalizer.normalize_for_comparison(crossref_result.title)
title_similarity = self.normalizer.similarity_ratio(bib_title_norm, crossref_title_norm)
if len(bib_title_norm) < 100:
lev_sim = self.normalizer.levenshtein_similarity(bib_title_norm, crossref_title_norm)
title_similarity = max(title_similarity, lev_sim)
title_match = title_similarity >= self.TITLE_THRESHOLD
if not title_match:
issues.append(f"Title mismatch. Bib: '{bib_entry.title}', Retrieved: '{crossref_result.title}'")
# Compare authors
bib_authors = self.normalizer.normalize_author_list(bib_entry.author)
crossref_authors = [self.normalizer.normalize_author_name(a) for a in crossref_result.authors]
author_similarity = self._compare_author_lists(bib_authors, crossref_authors)
author_match = author_similarity >= self.AUTHOR_THRESHOLD
if not author_match:
issues.append(f"Author mismatch. Bib: {', '.join(bib_authors)}, Retrieved: {', '.join(crossref_authors)}")
# Compare years
bib_year = bib_entry.year.strip()
crossref_year = crossref_result.year
year_match = bib_year == crossref_year
if not year_match and bib_year and crossref_year:
issues.append(f"Year mismatch. Bib: {bib_year}, Retrieved: {crossref_year}")
# Overall assessment
is_match = title_match and author_match
confidence = (title_similarity * 0.5 + author_similarity * 0.3 + (1.0 if year_match else 0.5) * 0.2)
return ComparisonResult(
entry_key=bib_entry.key,
title_match=title_match,
title_similarity=title_similarity,
bib_title=bib_entry.title,
fetched_title=crossref_result.title,
author_match=author_match,
author_similarity=author_similarity,
bib_authors=bib_authors,
fetched_authors=crossref_authors,
year_match=year_match,
bib_year=bib_year,
fetched_year=crossref_year,
is_match=is_match,
confidence=confidence,
issues=issues,
source="crossref"
)
def create_unable_result(self, bib_entry: BibEntry, reason: str = "Unable to fetch metadata") -> ComparisonResult:
"""Create result when metadata couldn't be fetched."""
return ComparisonResult(
entry_key=bib_entry.key,
title_match=False,
title_similarity=0.0,
bib_title=bib_entry.title,
fetched_title="",
author_match=False,
author_similarity=0.0,
bib_authors=self.normalizer.normalize_author_list(bib_entry.author),
fetched_authors=[],
year_match=False,
bib_year=bib_entry.year,
fetched_year="",
is_match=False,
confidence=0.0,
issues=[reason],
source="unable"
)
def _compare_author_lists(self, list1: list[str], list2: list[str]) -> float:
"""Compare two author lists."""
if not list1 and not list2:
return 1.0
if not list1 or not list2:
return 0.0
# Find best matches for each author in list1
total_similarity = 0.0
for author1 in list1:
best_match = 0.0
for author2 in list2:
# Check if one name contains the other (handle abbreviated names)
if self._names_match(author1, author2):
best_match = 1.0
break
sim = self.normalizer.similarity_ratio(author1, author2)
best_match = max(best_match, sim)
total_similarity += best_match
return total_similarity / len(list1)
def _names_match(self, name1: str, name2: str) -> bool:
"""Check if two names match (handles abbreviated names)."""
words1 = name1.split()
words2 = name2.split()
if not words1 or not words2:
return False
# Check if last names match
if words1[-1] != words2[-1]:
# Try first word as last name too
if words1[0] != words2[-1] and words1[-1] != words2[0]:
return False
return True
def compare_with_semantic_scholar(self, bib_entry: BibEntry, ss_result: SemanticScholarResult) -> ComparisonResult:
"""Compare bib entry with Semantic Scholar result."""
issues = []
# Compare titles
bib_title_norm = self.normalizer.normalize_for_comparison(bib_entry.title)
ss_title_norm = self.normalizer.normalize_for_comparison(ss_result.title)
title_similarity = self.normalizer.similarity_ratio(bib_title_norm, ss_title_norm)
if len(bib_title_norm) < 100:
lev_sim = self.normalizer.levenshtein_similarity(bib_title_norm, ss_title_norm)
title_similarity = max(title_similarity, lev_sim)
title_match = title_similarity >= self.TITLE_THRESHOLD
if not title_match:
issues.append(f"Title mismatch. Bib: '{bib_entry.title}', Retrieved: '{ss_result.title}'")
# Compare authors
bib_authors = self.normalizer.normalize_author_list(bib_entry.author)
ss_authors = [self.normalizer.normalize_author_name(a) for a in ss_result.authors]
author_similarity = self._compare_author_lists(bib_authors, ss_authors)
author_match = author_similarity >= self.AUTHOR_THRESHOLD
if not author_match:
issues.append(f"Author mismatch. Bib: {', '.join(bib_authors)}, Retrieved: {', '.join(ss_authors)}")
# Compare years
bib_year = bib_entry.year.strip()
ss_year = ss_result.year
year_match = bib_year == ss_year
if not year_match and bib_year and ss_year:
issues.append(f"Year mismatch. Bib: {bib_year}, Retrieved: {ss_year}")
# Overall assessment
is_match = title_match and author_match
confidence = (title_similarity * 0.5 + author_similarity * 0.3 + (1.0 if year_match else 0.5) * 0.2)
return ComparisonResult(
entry_key=bib_entry.key,
title_match=title_match,
title_similarity=title_similarity,
bib_title=bib_entry.title,
fetched_title=ss_result.title,
author_match=author_match,
author_similarity=author_similarity,
bib_authors=bib_authors,
fetched_authors=ss_authors,
year_match=year_match,
bib_year=bib_year,
fetched_year=ss_year,
is_match=is_match,
confidence=confidence,
issues=issues,
source="semantic_scholar"
)
def compare_with_openalex(self, bib_entry: BibEntry, oa_result: OpenAlexResult) -> ComparisonResult:
"""Compare bib entry with OpenAlex result."""
issues = []
# Compare titles
bib_title_norm = self.normalizer.normalize_for_comparison(bib_entry.title)
oa_title_norm = self.normalizer.normalize_for_comparison(oa_result.title)
title_similarity = self.normalizer.similarity_ratio(bib_title_norm, oa_title_norm)
if len(bib_title_norm) < 100:
lev_sim = self.normalizer.levenshtein_similarity(bib_title_norm, oa_title_norm)
title_similarity = max(title_similarity, lev_sim)
title_match = title_similarity >= self.TITLE_THRESHOLD
if not title_match:
issues.append(f"Title mismatch. Bib: '{bib_entry.title}', Retrieved: '{oa_result.title}'")
# Compare authors
bib_authors = self.normalizer.normalize_author_list(bib_entry.author)
oa_authors = [self.normalizer.normalize_author_name(a) for a in oa_result.authors]
author_similarity = self._compare_author_lists(bib_authors, oa_authors)
author_match = author_similarity >= self.AUTHOR_THRESHOLD
if not author_match:
issues.append(f"Author mismatch. Bib: {', '.join(bib_authors)}, Retrieved: {', '.join(oa_authors)}")
# Compare years
bib_year = bib_entry.year.strip()
oa_year = oa_result.year
year_match = bib_year == oa_year
if not year_match and bib_year and oa_year:
issues.append(f"Year mismatch. Bib: {bib_year}, Retrieved: {oa_year}")
# Overall assessment
is_match = title_match and author_match
confidence = (title_similarity * 0.5 + author_similarity * 0.3 + (1.0 if year_match else 0.5) * 0.2)
return ComparisonResult(
entry_key=bib_entry.key,
title_match=title_match,
title_similarity=title_similarity,
bib_title=bib_entry.title,
fetched_title=oa_result.title,
author_match=author_match,
author_similarity=author_similarity,
bib_authors=bib_authors,
fetched_authors=oa_authors,
year_match=year_match,
bib_year=bib_year,
fetched_year=oa_year,
is_match=is_match,
confidence=confidence,
issues=issues,
source="openalex"
)
def compare_with_dblp(self, bib_entry: BibEntry, dblp_result: DBLPResult) -> ComparisonResult:
"""Compare bib entry with DBLP result."""
issues = []
# Compare titles
bib_title_norm = self.normalizer.normalize_for_comparison(bib_entry.title)
dblp_title_norm = self.normalizer.normalize_for_comparison(dblp_result.title)
title_similarity = self.normalizer.similarity_ratio(bib_title_norm, dblp_title_norm)
if len(bib_title_norm) < 100:
lev_sim = self.normalizer.levenshtein_similarity(bib_title_norm, dblp_title_norm)
title_similarity = max(title_similarity, lev_sim)
title_match = title_similarity >= self.TITLE_THRESHOLD
if not title_match:
issues.append(f"Title mismatch. Bib: '{bib_entry.title}', Retrieved: '{dblp_result.title}'")
# Compare authors
bib_authors = self.normalizer.normalize_author_list(bib_entry.author)
dblp_authors = [self.normalizer.normalize_author_name(a) for a in dblp_result.authors]
author_similarity = self._compare_author_lists(bib_authors, dblp_authors)
author_match = author_similarity >= self.AUTHOR_THRESHOLD
if not author_match:
issues.append(f"Author mismatch. Bib: {', '.join(bib_authors)}, Retrieved: {', '.join(dblp_authors)}")
# Compare years
bib_year = bib_entry.year.strip()
dblp_year = dblp_result.year
year_match = bib_year == dblp_year
if not year_match and bib_year and dblp_year:
issues.append(f"Year mismatch. Bib: {bib_year}, Retrieved: {dblp_year}")
# Overall assessment
is_match = title_match and author_match
confidence = (title_similarity * 0.5 + author_similarity * 0.3 + (1.0 if year_match else 0.5) * 0.2)
return ComparisonResult(
entry_key=bib_entry.key,
title_match=title_match,
title_similarity=title_similarity,
bib_title=bib_entry.title,
fetched_title=dblp_result.title,
author_match=author_match,
author_similarity=author_similarity,
bib_authors=bib_authors,
fetched_authors=dblp_authors,
year_match=year_match,
bib_year=bib_year,
fetched_year=dblp_year,
is_match=is_match,
confidence=confidence,
issues=issues,
source="dblp"
)
|